123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Text;
- namespace HTEXLib
- {
- /// <summary>
- /// 类 名:ColorHelper
- /// 功 能:提供从RGB到HSV/HSL色彩空间的相互转换
- /// </summary>
- public static class ColorHelper
- {
- /// <summary>
- /// RGB转换HSV
- /// </summary>
- /// <param name="rgb"></param>
- /// <returns></returns>
- 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));
- }
- /// <summary>
- /// HSV转换RGB
- /// </summary>
- /// <param name="hsv"></param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// RGB转换HSL
- /// </summary>
- /// <param name="rgb"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// HSL转换RGB
- /// </summary>
- /// <param name="hsl"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 处理图像饱和度
- /// </summary>
- /// <param name="color"></param>
- /// <param name="lumMod"></param>
- /// <param name="lumOff"></param>
- /// <returns></returns>
- 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("#","");
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="color">原色RGB</param>
- /// <param name="pa">混合原色占百分比/100% </param>
- /// <param name="Type">Shade 与黑色混合 Tint与白色混合</param>
- /// <returns></returns>
- 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("#",""); }
- }
- }
- }
|