using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
namespace HTEXLib
{
///
/// 类 名:ColorHelper
/// 功 能:提供从RGB到HSV/HSL色彩空间的相互转换
///
public static class ColorHelper
{
///
/// RGB转换HSV
///
///
///
public static ColorHSV RgbToHsv(this ColorRGB rgb)
{
double min, max, tmp, H, S, V;
double R = rgb.R * 1.0f / 255, G = rgb.G * 1.0f / 255, B = rgb.B * 1.0f / 255;
tmp = System.Math.Min(R, G);
min = System.Math.Min(tmp, B);
tmp = System.Math.Max(R, G);
max = System.Math.Max(tmp, B);
// H
H = 0;
if (max == min)
{
H = 0;
}
else if (max == R && G > B)
{
H = 60 * (G - B) * 1.0f / (max - min) + 0;
}
else if (max == R && G < B)
{
H = 60 * (G - B) * 1.0f / (max - min) + 360;
}
else if (max == G)
{
H = H = 60 * (B - R) * 1.0f / (max - min) + 120;
}
else if (max == B)
{
H = H = 60 * (R - G) * 1.0f / (max - min) + 240;
}
// S
if (max == 0)
{
S = 0;
}
else
{
S = (max - min) * 1.0f / max;
}
// V
V = max;
return new ColorHSV((int)H, (int)(S * 255), (int)(V * 255));
}
///
/// HSV转换RGB
///
///
///
public static ColorRGB HsvToRgb( this ColorHSV hsv)
{
if (hsv.H == 360) hsv.H = 359; // 360为全黑,原因不明
double R = 0f, G = 0f, B = 0f;
if (hsv.S == 0)
{
return new ColorRGB(hsv.V, hsv.V, hsv.V);
}
double S = hsv.S * 1.0f / 255, V = hsv.V * 1.0f / 255;
int H1 = (int)(hsv.H * 1.0f / 60), H = hsv.H;
double F = H * 1.0f / 60 - H1;
double P = V * (1.0f - S);
double Q = V * (1.0f - F * S);
double T = V * (1.0f - (1.0f - F) * S);
switch (H1)
{
case 0: R = V; G = T; B = P; break;
case 1: R = Q; G = V; B = P; break;
case 2: R = P; G = V; B = T; break;
case 3: R = P; G = Q; B = V; break;
case 4: R = T; G = P; B = V; break;
case 5: R = V; G = P; B = Q; break;
}
R = R * 255;
G = G * 255;
B = B * 255;
while (R > 255) R -= 255;
while (R < 0) R += 255;
while (G > 255) G -= 255;
while (G < 0) G += 255;
while (B > 255) B -= 255;
while (B < 0) B += 255;
return new ColorRGB((int)R, (int)G, (int)B);
}
///
/// RGB转换HSL
///
///
///
public static ColorHSL RgbToHsl( this ColorRGB rgb)
{
double min, max, tmp, H = 0, S = 0, L = 0;
double R = rgb.R * 1.0f / 255, G = rgb.G * 1.0f / 255, B = rgb.B * 1.0f / 255;
tmp = System.Math.Min(R, G);
min = System.Math.Min(tmp, B);
tmp = System.Math.Max(R, G);
max = System.Math.Max(tmp, B);
L = (max + min) / 2.0d;
if (min == max)
{
H = S = 0;
}
else
{
double d = max - min;
S = L > 0.5 ? d / (2 - max - min) : d / (max + min);
if (max == R)
{
H = (G - B) / d + (G < B ? 6 : 0);
}
else if (max == G)
{
H = (G - R) / d + 2;
}
else if (max == B)
{
H = (R - G) / d + 4;
}
}
H = H / 6.0F;
return new ColorHSL() { H = H, S = S, L = L };
}
public static double hue2rgb(double p, double q, double t)
{
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1.0 / 6.0d) return p + (q - p) * 6 * t;
if (t < 1.0 / 2.0d) return q;
if (t < 2.0 / 3.0d) return p + (q - p) * (2.0d / 3.0d - t) * 6.0d;
return p;
}
///
/// HSL转换RGB
///
///
///
public static ColorRGB HslToRgb( this ColorHSL hsl)
{
var l = hsl.L;
var s = hsl.S;
var h = hsl.H;
double R = 0, G = 0, B = 0;
if (hsl.S == 0)
{
R = G = B = hsl.L;
}
else
{
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
R = hue2rgb(p, q, h + 1.0d / 3.0d);
G = hue2rgb(p, q, h);
B = hue2rgb(p, q, h - 1.0d / 3.0d);
}
R = R * 255; G = G * 255; B = B * 255;
return new ColorRGB(int.Parse(System.Math.Round(R, 0) + ""), int.Parse(System.Math.Round(G, 0) + ""), int.Parse(System.Math.Round(B, 0) + ""));
}
private static double HueToRGB(double p, double q, double h)
{
if (h < 0) h += 1;
if (h > 1) h -= 1;
if (6 * h < 1)
{
return p + ((q - p) * 6 * h);
}
if (2 * h < 1)
{
return q;
}
if (3 * h < 2)
{
return p + ((q - p) * 6 * ((2.0f / 3.0f) - h));
}
return p;
}
///
/// 处理图像饱和度
///
///
///
///
///
public static string GetColorLumModAndLumOff(Color color, int lumMod = 0, int lumOff = 0)
{
ColorHSL colorHSL = RgbToHsl(new ColorRGB(color.R, color.G, color.B));
colorHSL.L = colorHSL.L * lumMod / 100_000.0 + lumOff / 100_000.0;
ColorRGB colorRGB = HslToRgb(colorHSL);
return ColorTranslator.ToHtml(Color.FromArgb(colorRGB.R, colorRGB.G, colorRGB.B)).Replace("#","");
}
///
///
///
/// 原色RGB
/// 混合原色占百分比/100%
/// Shade 与黑色混合 Tint与白色混合
///
public static string GetShadeOrTintColor(Color color, double val, string Type)
{
ColorConverter converter = new ColorConverter();
if (Type.Equals("Shade"))
{
return converter.SetShade(color, val);
}
else if (Type.Equals("Tint"))
{
return converter.SetTint(color, val);
}
else { return ColorTranslator.ToHtml(color).Replace("#",""); }
}
}
}