ColorConverter.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. internal string SetComp(string colorHex)
  115. {
  116. Color c = ColorTranslator.FromHtml("#" + colorHex);
  117. int cc = Max(c) + Min(c);
  118. var rn = cc - c.R;
  119. var gn = cc - c.G;
  120. var bn = cc - c.B;
  121. return rn.ToString("X2") + gn.ToString("X2") + bn.ToString("X2");
  122. }
  123. private double RGB_to_linearRGB(double val)
  124. {
  125. if (val < 0.0)
  126. return 0.0;
  127. if (val <= 0.04045)
  128. return val / 12.92;
  129. if (val <= 1.0)
  130. return (double)System. Math.Pow(((val + 0.055) / 1.055), 2.4);
  131. return 1.0;
  132. }
  133. private double linearRGB_to_RGB(double val)
  134. {
  135. if (val < 0.0)
  136. return 0.0;
  137. if (val <= 0.0031308)
  138. return val * 12.92;
  139. if (val < 1.0)
  140. return (1.055 * System.Math.Pow(val, (1.0 / 2.4))) - 0.055;
  141. return 1.0;
  142. /*
  143. Public Function linearRGB_to_sRGB(ByVal value As Double) As Double
  144. If value < 0.0# Then Return 0.0#
  145. If value <= 0.0031308# Then Return value * 12.92
  146. If value < 1.0# Then Return 1.055 * (value ^ (1.0# / 2.4)) - 0.055
  147. Return 1.0#
  148. End Function
  149. */
  150. }
  151. public string SetShade(Color c, double shade)
  152. {
  153. //Console.WriteLine("Shade: " + shade);
  154. double convShade = (shade / 1000) * 0.01;
  155. double r = (double)c.R / 255;
  156. double g = (double)c.G / 255;
  157. double b = (double)c.B / 255;
  158. double rLin = RGB_to_linearRGB(r);
  159. double gLin = RGB_to_linearRGB(g);
  160. double bLin = RGB_to_linearRGB(b);
  161. //Console.WriteLine("Linear R: " + rLin + "\nLinear G: " + gLin + "\nLinear B: " + bLin);
  162. //SHADE
  163. if ((rLin * convShade) < 0)
  164. rLin = 0;
  165. if ((rLin * convShade) > 1)
  166. rLin = 0;
  167. else
  168. rLin *= convShade;
  169. if ((gLin * convShade) < 0)
  170. gLin = 0;
  171. if ((gLin * convShade) > 1)
  172. gLin = 0;
  173. else
  174. gLin *= convShade;
  175. if ((bLin * convShade) < 0)
  176. bLin = 0;
  177. if ((bLin * convShade) > 1)
  178. bLin = 0;
  179. else
  180. bLin *= convShade;
  181. //SHADEEND
  182. r = linearRGB_to_RGB(rLin);
  183. g = linearRGB_to_RGB(gLin);
  184. b = linearRGB_to_RGB(bLin);
  185. Color outColor = Color.FromArgb((int)System.Math.Round(r * 255), (int)System.Math.Round(g * 255), (int)System.Math.Round(b * 255));
  186. return outColor.R.ToString("X2") + outColor.G.ToString("X2") + outColor.B.ToString("X2");
  187. }
  188. public string SetTint(Color c, double tint)
  189. {
  190. double tintConv = (tint / 1000) * 0.01;
  191. double r = (double)c.R / 255;
  192. double g = (double)c.G / 255;
  193. double b = (double)c.B / 255;
  194. double rLin = RGB_to_linearRGB(r);
  195. double gLin = RGB_to_linearRGB(g);
  196. double bLin = RGB_to_linearRGB(b);
  197. /**TINT**/
  198. if (tintConv > 0)
  199. rLin = (rLin * tintConv) + (1 - tintConv);
  200. else
  201. rLin = rLin * (1 + tintConv);
  202. if (tintConv > 0)
  203. gLin = (gLin * tintConv) + (1 - tintConv);
  204. else
  205. gLin = gLin * (1 + tintConv);
  206. if (tintConv > 0)
  207. bLin = (bLin * tintConv) + (1 - tintConv);
  208. else
  209. bLin = bLin * (1 + tintConv);
  210. r = linearRGB_to_RGB(rLin);
  211. g = linearRGB_to_RGB(gLin);
  212. b = linearRGB_to_RGB(bLin);
  213. Color outColor = Color.FromArgb((int)System.Math.Round(r * 255), (int)System.Math.Round(g * 255), (int)System.Math.Round(b * 255));
  214. return outColor.R.ToString("X2") + outColor.G.ToString("X2") + outColor.B.ToString("X2");
  215. }
  216. public string SetSaturationMod(Color c, double saturation)
  217. {
  218. double satMod = (saturation / 1000) * 0.01;
  219. ColorHSL hsl = RGB_to_HSL(c);
  220. hsl.S *= satMod;
  221. c = HSL_to_RGB(hsl);
  222. return c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
  223. }
  224. public string SetSaturationOff(Color c, double saturation)
  225. {
  226. double satOff = (saturation / 1000) * 0.01;
  227. ColorHSL hsl = RGB_to_HSL(c);
  228. hsl.S += satOff;
  229. c = HSL_to_RGB(hsl);
  230. return c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
  231. }
  232. public string SetBrightness(Color c, double brightness)
  233. {
  234. double convBrightness = brightness / 100000;
  235. ColorHSL hsl = RGB_to_HSL(c);
  236. hsl.L *= convBrightness;
  237. c = HSL_to_RGB(hsl);
  238. return c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
  239. }
  240. public string SetLuminanceMod(Color c, double luminance)
  241. {
  242. double lumMod = (luminance / 1000) * 0.01;
  243. ColorHSL hsl = RGB_to_HSL(c);
  244. hsl.L *= lumMod;
  245. Color outColor = HSL_to_RGB(hsl);
  246. return outColor.R.ToString("X2") + outColor.G.ToString("X2") + outColor.B.ToString("X2");
  247. }
  248. public string SetLuminanceOff(Color c, double luminance)
  249. {
  250. double lumModOff = (luminance / 1000) * 0.01;
  251. ColorHSL hsl = RGB_to_HSL(c);
  252. hsl.L += lumModOff;
  253. Color outColor = HSL_to_RGB(hsl);
  254. return outColor.R.ToString("X2") + outColor.G.ToString("X2") + outColor.B.ToString("X2");
  255. }
  256. public byte Max(byte r, byte g, byte b) {
  257. byte max = r;
  258. if (max < g) {
  259. max = g;
  260. }
  261. if (max < b) {
  262. max = b;
  263. }
  264. return max;
  265. }
  266. public byte Min(byte r, byte g, byte b)
  267. {
  268. byte min = r;
  269. if (min > g)
  270. {
  271. min = g;
  272. }
  273. if (min > b)
  274. {
  275. min = b;
  276. }
  277. return min;
  278. }
  279. public byte Max(Color color)
  280. {
  281. byte max = color.R;
  282. if (max < color.G)
  283. {
  284. max = color.G;
  285. }
  286. if (max < color.B)
  287. {
  288. max = color.B;
  289. }
  290. return max;
  291. }
  292. public byte Min(Color color)
  293. {
  294. byte min = color.R;
  295. if (min > color.G)
  296. {
  297. min = color.G;
  298. }
  299. if (min > color.B)
  300. {
  301. min = color.B;
  302. }
  303. return min;
  304. }
  305. public string SetHueMod(Color c, double hue)
  306. {
  307. double hueMod = (hue / 1000) * 0.01;
  308. ColorHSL hsl = RGB_to_HSL(c);
  309. hsl.H *= hueMod;
  310. Color outColor = HSL_to_RGB(hsl);
  311. return outColor.R.ToString("X2") + outColor.G.ToString("X2") + outColor.B.ToString("X2");
  312. }
  313. public string SetHueOff(Color c, double hue)
  314. {
  315. double hueMod = (hue / 1000) * 0.01;
  316. ColorHSL hsl = RGB_to_HSL(c);
  317. hsl.H += hueMod;
  318. Color outColor = HSL_to_RGB(hsl);
  319. return outColor.R.ToString("X2") + outColor.G.ToString("X2") + outColor.B.ToString("X2");
  320. }
  321. private ColorHSL RGB_to_HSL(Color c)
  322. {
  323. ColorHSL hsl = new ColorHSL();
  324. hsl.H = c.GetHue() / 360.0;
  325. hsl.L = c.GetBrightness();
  326. hsl.S = c.GetSaturation();
  327. return hsl;
  328. }
  329. private ColorHSL CreateHSL(int r, int g, int b)
  330. {
  331. ColorHSL hsl = new ColorHSL();
  332. double R = (double)r / 255;
  333. double G = (double)g / 255;
  334. double B = (double)b / 255;
  335. double max = 0, min = 0;
  336. double H = 0, S = 0, L = 0;
  337. bool rBool = false, gBool = false, bBool = false;
  338. //find max
  339. if ((R > G) && (R > B))
  340. {
  341. max = R;
  342. rBool = true;
  343. }
  344. else if ((G > R) && (G > B))
  345. {
  346. max = G;
  347. gBool = true;
  348. }
  349. else if ((B > G) && (B > R))
  350. {
  351. max = B;
  352. bBool = true;
  353. }
  354. if ((R < G) && (R < B))
  355. min = R;
  356. else if ((G < R) && (G < B))
  357. min = G;
  358. else if ((B < G) && (B < R))
  359. min = B;
  360. //set Luminance
  361. hsl.L = (min + max) / 2;
  362. if (min == max)
  363. {
  364. hsl.S = 0;
  365. hsl.H = 0;
  366. }
  367. //set saturation
  368. if (hsl.L < 0.5)
  369. hsl.S = (max - min) / (max + min);
  370. else if (hsl.L > 0.5)
  371. hsl.S = (max - min) / (2.0 - max - min);
  372. if (rBool)
  373. {
  374. H = ((G - B) / (max - min)) * 60.0;
  375. Console.WriteLine("red");
  376. }
  377. if (gBool)
  378. {
  379. H = ((2.0 + ((B - R) / (max - min))) * 60.0);
  380. Console.WriteLine("green");
  381. }
  382. if (bBool)
  383. {
  384. H = (4.0 + (R - G) / (max - min)) * 60.0;
  385. Console.WriteLine("blue");
  386. }
  387. if (H < 0)
  388. H += 360;
  389. H = System.Math.Round(H);
  390. H = H / 360;
  391. hsl.H = H;
  392. return hsl;
  393. }
  394. private Color HSL_to_RGB(ColorHSL hslColor)
  395. {
  396. double r = 0, g = 0, b = 0;
  397. double temp1, temp2;
  398. if (hslColor.L == 0)
  399. r = g = b = 0;
  400. else
  401. {
  402. if (hslColor.S == 0)
  403. {
  404. r = g = b = hslColor.L;
  405. }
  406. else
  407. {
  408. temp2 = ((hslColor.L <= 0.5) ? hslColor.L * (1.0 + hslColor.S) : hslColor.L + hslColor.S - (hslColor.L * hslColor.S));
  409. temp1 = 2.0 * hslColor.L - temp2;
  410. double[] t3 = new double[] { hslColor.H + 1.0 / 3.0, hslColor.H, hslColor.H - 1.0 / 3.0 };
  411. double[] clr = new double[] { 0, 0, 0 };
  412. for (int i = 0; i < 3; i++)
  413. {
  414. if (t3[i] < 0)
  415. t3[i] += 1.0;
  416. if (t3[i] > 1)
  417. t3[i] -= 1.0;
  418. if (6.0 * t3[i] < 1.0)
  419. clr[i] = temp1 + (temp2 - temp1) * t3[i] * 6.0;
  420. else if (2.0 * t3[i] < 1.0)
  421. clr[i] = temp2;
  422. else if (3.0 * t3[i] < 2.0)
  423. clr[i] = (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - t3[i]) * 6.0);
  424. else
  425. clr[i] = temp1;
  426. }
  427. r = clr[0];
  428. g = clr[1];
  429. b = clr[2];
  430. }
  431. }
  432. return Color.FromArgb((int)(255 * r), (int)(255 * g), (int)(255 * b));
  433. }
  434. }
  435. }