ColorConverter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. using System;
  2. using System.Drawing;
  3. namespace ConsoleApplication
  4. {
  5. public class ColorConverter
  6. {
  7. public ColorConverter(){}
  8. public string SetLuminanceMod(string s, double luminance)
  9. {
  10. Color c = ColorTranslator.FromHtml("#" + s);
  11. return SetLuminanceMod(c, luminance);
  12. }
  13. public string SetLuminanceOff(string s, double luminance)
  14. {
  15. Color c = ColorTranslator.FromHtml("#" + s);
  16. return SetLuminanceOff(c, luminance);
  17. }
  18. public string SetTint(string s, double tint)
  19. {
  20. Color c = ColorTranslator.FromHtml("#" + s);
  21. return SetTint(c,tint);
  22. }
  23. public string SetShade(string s, double shade)
  24. {
  25. Color c = ColorTranslator.FromHtml("#" + s);
  26. return SetShade(c, shade);
  27. }
  28. public string SetSaturationMod(string s, double saturation)
  29. {
  30. Color c = ColorTranslator.FromHtml("#" + s);
  31. return SetSaturationMod(c, saturation);
  32. }
  33. public string SetSaturationOff(string s, double saturation)
  34. {
  35. Color c = ColorTranslator.FromHtml("#" + s);
  36. return SetSaturationOff(c, saturation);
  37. }
  38. public string SetBrightness(string s, double luminance)
  39. {
  40. Color c = ColorTranslator.FromHtml("#" + s);
  41. return SetBrightness(c, luminance);
  42. }
  43. public string SetHueMod(string s, double hueMod)
  44. {
  45. Color c = ColorTranslator.FromHtml("#" + s);
  46. return SetHueMod(c, hueMod);
  47. }
  48. private double RGB_to_linearRGB(double val){
  49. if (val < 0.0)
  50. return 0.0;
  51. if (val <= 0.04045)
  52. return val / 12.92;
  53. if (val <= 1.0)
  54. return (double) Math.Pow(((val + 0.055) / 1.055),2.4);
  55. return 1.0;
  56. }
  57. private double linearRGB_to_RGB(double val)
  58. {
  59. if (val < 0.0)
  60. return 0.0;
  61. if (val <= 0.0031308)
  62. return val * 12.92;
  63. if (val < 1.0)
  64. return (1.055 * Math.Pow(val, (1.0 / 2.4))) - 0.055;
  65. return 1.0;
  66. /*
  67. Public Function linearRGB_to_sRGB(ByVal value As Double) As Double
  68. If value < 0.0# Then Return 0.0#
  69. If value <= 0.0031308# Then Return value * 12.92
  70. If value < 1.0# Then Return 1.055 * (value ^ (1.0# / 2.4)) - 0.055
  71. Return 1.0#
  72. End Function
  73. */
  74. }
  75. public string SetShade(Color c, double shade)
  76. {
  77. //Console.WriteLine("Shade: " + shade);
  78. double convShade = (shade / 1000) * 0.01;
  79. double r = (double) c.R / 255;
  80. double g = (double) c.G / 255;
  81. double b = (double) c.B / 255;
  82. double rLin = RGB_to_linearRGB(r);
  83. double gLin = RGB_to_linearRGB(g);
  84. double bLin = RGB_to_linearRGB(b);
  85. //Console.WriteLine("Linear R: " + rLin + "\nLinear G: " + gLin + "\nLinear B: " + bLin);
  86. //SHADE
  87. if ((rLin * convShade) < 0)
  88. rLin = 0;
  89. if ((rLin * convShade) > 1)
  90. rLin = 0;
  91. else
  92. rLin *= convShade;
  93. if ((gLin * convShade) < 0)
  94. gLin = 0;
  95. if ((gLin * convShade) > 1)
  96. gLin = 0;
  97. else
  98. gLin *= convShade;
  99. if ((bLin * convShade) < 0)
  100. bLin = 0;
  101. if ((bLin * convShade) > 1)
  102. bLin = 0;
  103. else
  104. bLin *= convShade;
  105. //SHADEEND
  106. r = linearRGB_to_RGB(rLin);
  107. g = linearRGB_to_RGB(gLin);
  108. b = linearRGB_to_RGB(bLin);
  109. Color outColor = Color.FromArgb((int)Math.Round(r * 255), (int)Math.Round(g * 255), (int)Math.Round(b * 255));
  110. return outColor.R.ToString("X2") + outColor.G.ToString("X2") + outColor.B.ToString("X2");
  111. }
  112. public string SetTint(Color c, double tint)
  113. {
  114. double tintConv = (tint / 1000)*0.01;
  115. double r = (double)c.R / 255;
  116. double g = (double)c.G / 255;
  117. double b = (double)c.B / 255;
  118. double rLin = RGB_to_linearRGB(r);
  119. double gLin = RGB_to_linearRGB(g);
  120. double bLin = RGB_to_linearRGB(b);
  121. /**TINT**/
  122. if (tintConv > 0)
  123. rLin = (rLin * tintConv) + (1 - tintConv);
  124. else
  125. rLin = rLin * (1 + tintConv);
  126. if (tintConv > 0)
  127. gLin = (gLin * tintConv) + (1 - tintConv);
  128. else
  129. gLin = gLin * (1 + tintConv);
  130. if (tintConv > 0)
  131. bLin = (bLin * tintConv) + (1 - tintConv);
  132. else
  133. bLin = bLin * (1 + tintConv);
  134. r = linearRGB_to_RGB(rLin);
  135. g = linearRGB_to_RGB(gLin);
  136. b = linearRGB_to_RGB(bLin);
  137. Color outColor = Color.FromArgb((int)Math.Round(r * 255), (int)Math.Round(g * 255), (int)Math.Round(b * 255));
  138. return outColor.R.ToString("X2") + outColor.G.ToString("X2") + outColor.B.ToString("X2");
  139. }
  140. public string SetSaturationMod(Color c, double saturation)
  141. {
  142. double satMod = (saturation / 1000)*0.01;
  143. HSLColor hsl = RGB_to_HSL(c);
  144. hsl.Saturation *= satMod;
  145. c = HSL_to_RGB(hsl);
  146. return c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
  147. }
  148. public string SetSaturationOff(Color c, double saturation)
  149. {
  150. double satOff = (saturation / 1000) * 0.01;
  151. HSLColor hsl = RGB_to_HSL(c);
  152. hsl.Saturation += satOff;
  153. c = HSL_to_RGB(hsl);
  154. return c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
  155. }
  156. public string SetBrightness(Color c, double brightness)
  157. {
  158. double convBrightness = brightness / 100000;
  159. HSLColor hsl = RGB_to_HSL(c);
  160. hsl.Luminance *= convBrightness;
  161. c = HSL_to_RGB(hsl);
  162. return c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
  163. }
  164. public string SetLuminanceMod(Color c, double luminance)
  165. {
  166. double lumMod = (luminance / 1000)*0.01;
  167. HSLColor hsl = RGB_to_HSL(c);
  168. hsl.Luminance *= lumMod;
  169. Color outColor = HSL_to_RGB(hsl);
  170. return outColor.R.ToString("X2") + outColor.G.ToString("X2") + outColor.B.ToString("X2");
  171. }
  172. public string SetLuminanceOff(Color c, double luminance)
  173. {
  174. double lumModOff = (luminance / 1000) * 0.01;
  175. HSLColor hsl = RGB_to_HSL(c);
  176. hsl.Luminance += lumModOff;
  177. Color outColor = HSL_to_RGB(hsl);
  178. return outColor.R.ToString("X2") + outColor.G.ToString("X2") + outColor.B.ToString("X2");
  179. }
  180. public string SetHueMod(Color c, double hue)
  181. {
  182. double hueMod = (hue / 1000) * 0.01;
  183. HSLColor hsl = RGB_to_HSL(c);
  184. hsl.Hue *= hueMod;
  185. Color outColor = HSL_to_RGB(hsl);
  186. return outColor.R.ToString("X2") + outColor.G.ToString("X2") + outColor.B.ToString("X2");
  187. }
  188. public string SetHueOff(Color c, double hue)
  189. {
  190. double hueMod = (hue / 1000) * 0.01;
  191. HSLColor hsl = RGB_to_HSL(c);
  192. hsl.Hue += hueMod;
  193. Color outColor = HSL_to_RGB(hsl);
  194. return outColor.R.ToString("X2") + outColor.G.ToString("X2") + outColor.B.ToString("X2");
  195. }
  196. private HSLColor RGB_to_HSL(Color c)
  197. {
  198. HSLColor hsl = new HSLColor();
  199. hsl.Hue = c.GetHue() / 360.0;
  200. hsl.Luminance = c.GetBrightness();
  201. hsl.Saturation = c.GetSaturation();
  202. return hsl;
  203. }
  204. private HSLColor CreateHSL(int r, int g, int b)
  205. {
  206. HSLColor hsl = new HSLColor();
  207. double R = (double)r / 255;
  208. double G = (double)g / 255;
  209. double B = (double)b / 255;
  210. double max = 0, min = 0;
  211. double H = 0;//, S, L;
  212. bool rBool = false, gBool = false, bBool = false;
  213. //find max
  214. if ((R > G) && (R > B))
  215. {
  216. max = R;
  217. rBool = true;
  218. }
  219. else if ((G > R) && (G > B))
  220. {
  221. max = G;
  222. gBool = true;
  223. }
  224. else if ((B > G) && (B > R))
  225. {
  226. max = B;
  227. bBool = true;
  228. }
  229. if ((R < G) && (R < B))
  230. min = R;
  231. else if ((G < R) && (G < B))
  232. min = G;
  233. else if ((B < G) && (B < R))
  234. min = B;
  235. //set Luminance
  236. hsl.Luminance = (min + max) / 2;
  237. if (min == max)
  238. {
  239. hsl.Saturation = 0;
  240. hsl.Hue = 0;
  241. }
  242. //set saturation
  243. if (hsl.Luminance < 0.5)
  244. hsl.Saturation = (max - min) / (max + min);
  245. else if (hsl.Luminance > 0.5)
  246. hsl.Saturation = (max - min) / (2.0 - max - min);
  247. if (rBool)
  248. {
  249. H = ((G - B) / (max - min)) * 60.0;
  250. Console.WriteLine("red");
  251. }
  252. if (gBool)
  253. {
  254. H = ((2.0 + ((B - R) / (max - min))) * 60.0);
  255. Console.WriteLine("green");
  256. }
  257. if (bBool)
  258. {
  259. H = (4.0 + (R - G) / (max - min)) * 60.0;
  260. Console.WriteLine("blue");
  261. }
  262. if (H < 0)
  263. H += 360;
  264. H = Math.Round(H);
  265. H = H / 360;
  266. hsl.Hue = H;
  267. return hsl;
  268. }
  269. private Color HSL_to_RGB(HSLColor hslColor)
  270. {
  271. double r = 0, g = 0, b = 0;
  272. double temp1, temp2;
  273. if (hslColor.Luminance == 0)
  274. r = g = b = 0;
  275. else
  276. {
  277. if (hslColor.Saturation == 0)
  278. {
  279. r = g = b = hslColor.Luminance;
  280. }
  281. else
  282. {
  283. temp2 = ((hslColor.Luminance <= 0.5) ? hslColor.Luminance * (1.0 + hslColor.Saturation) : hslColor.Luminance + hslColor.Saturation - (hslColor.Luminance * hslColor.Saturation));
  284. temp1 = 2.0 * hslColor.Luminance - temp2;
  285. double[] t3 = new double[] { hslColor.Hue + 1.0 / 3.0, hslColor.Hue, hslColor.Hue - 1.0 / 3.0 };
  286. double[] clr = new double[] { 0, 0, 0 };
  287. for (int i = 0; i < 3; i++)
  288. {
  289. if (t3[i] < 0)
  290. t3[i] += 1.0;
  291. if (t3[i] > 1)
  292. t3[i] -= 1.0;
  293. if (6.0 * t3[i] < 1.0)
  294. clr[i] = temp1 + (temp2 - temp1) * t3[i] * 6.0;
  295. else if (2.0 * t3[i] < 1.0)
  296. clr[i] = temp2;
  297. else if (3.0 * t3[i] < 2.0)
  298. clr[i] = (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - t3[i]) * 6.0);
  299. else
  300. clr[i] = temp1;
  301. }
  302. r = clr[0];
  303. g = clr[1];
  304. b = clr[2];
  305. }
  306. }
  307. return Color.FromArgb((int)(255 * r), (int)(255 * g), (int)(255 * b));
  308. }
  309. }
  310. }