ColorConverter.cs 14 KB

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