ColorManager.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Drawing;
  3. namespace quick_color_picker
  4. {
  5. public static class ColorManager
  6. {
  7. public static int[] ColorToHSV(Color color)
  8. {
  9. int max = Math.Max(color.R, Math.Max(color.G, color.B));
  10. int min = Math.Min(color.R, Math.Min(color.G, color.B));
  11. double hue = color.GetHue();
  12. double saturation = (max == 0) ? 0 : 1d - (1d * min / max);
  13. double value = max / 255d;
  14. int h = Convert.ToInt32(hue);
  15. int s = Convert.ToInt32(saturation * 100);
  16. int v = Convert.ToInt32(value * 100);
  17. return new int[] { h, s, v };
  18. }
  19. public static int[] ColorToHSL(Color color)
  20. {
  21. float hue = color.GetHue();
  22. float saturation = color.GetSaturation();
  23. float lightness = color.GetBrightness();
  24. int intHue = Convert.ToInt32(hue);
  25. int intSaturation = Convert.ToInt32(saturation * 100);
  26. int intLightness = Convert.ToInt32(lightness * 100);
  27. return new int[] { intHue, intSaturation, intLightness };
  28. }
  29. public static int[] ColorToCMYK(Color color)
  30. {
  31. double c, m, y, k;
  32. double r = color.R, g = color.G, b = color.B;
  33. double r1 = r / 255, g1 = g / 255, b1 = b / 255;
  34. k = 1 - Math.Max(Math.Max(r1, g1), b1);
  35. if (k == 1)
  36. {
  37. return new int[] { 0, 0, 0, 1 };
  38. }
  39. else
  40. {
  41. c = (1 - r1 - k) / (1 - k);
  42. m = (1 - g1 - k) / (1 - k);
  43. y = (1 - b1 - k) / (1 - k);
  44. int intC = Convert.ToInt32(c * 100);
  45. int intM = Convert.ToInt32(m * 100);
  46. int intY = Convert.ToInt32(y * 100);
  47. int intK = Convert.ToInt32(k * 100);
  48. return new int[] { intC, intM, intY, intK };
  49. }
  50. }
  51. public static Color TextToColor(string text)
  52. {
  53. string[] strArr = text.Split(',');
  54. int[] arr = Array.ConvertAll(strArr, s => int.Parse(s));
  55. return Color.FromArgb(arr[0], arr[1], arr[2]);
  56. }
  57. }
  58. }