ColorConverter.cs 13 KB

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