code_junkie

How to change RGB color to HSV?

Nov 12th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. System.Drawing.Color color = System.Drawing.Color.FromArgb(red, green, blue);
  2. float hue = color.Gethue();
  3. float saturation = color.GetSaturation();
  4. float lightness = color.GetBrightness();
  5.  
  6. Color original = Color.FromArgb(50, 120, 200);
  7. // original = {Name=ff3278c8, ARGB=(255, 50, 120, 200)}
  8.  
  9. double hue;
  10. double saturation;
  11. double value;
  12. ColorToHSV(original, out hue, out saturation, out value);
  13. // hue = 212.0
  14. // saturation = 0.75
  15. // value = 0.78431372549019607
  16.  
  17. Color copy = ColorFromHSV(hue, saturation, value);
  18. // copy = {Name=ff3278c8, ARGB=(255, 50, 120, 200)}
  19.  
  20. // Compare that to the HSL values that the .NET framework provides:
  21. original.GetHue(); // 212.0
  22. original.GetSaturation(); // 0.6
  23. original.GetBrightness(); // 0.490196079
  24.  
  25. public static void ColorToHSV(Color color, out double hue, out double saturation, out double value)
  26. {
  27. int max = Math.Max(color.R, Math.Max(color.G, color.B));
  28. int min = Math.Min(color.R, Math.Min(color.G, color.B));
  29.  
  30. hue = color.GetHue();
  31. saturation = (max == 0) ? 0 : 1d - (1d * min / max);
  32. value = max / 255d;
  33. }
  34.  
  35. public static Color ColorFromHSV(double hue, double saturation, double value)
  36. {
  37. int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
  38. double f = hue / 60 - Math.Floor(hue / 60);
  39.  
  40. value = value * 255;
  41. int v = Convert.ToInt32(value);
  42. int p = Convert.ToInt32(value * (1 - saturation));
  43. int q = Convert.ToInt32(value * (1 - f * saturation));
  44. int t = Convert.ToInt32(value * (1 - (1 - f) * saturation));
  45.  
  46. if (hi == 0)
  47. return Color.FromArgb(255, v, t, p);
  48. else if (hi == 1)
  49. return Color.FromArgb(255, q, v, p);
  50. else if (hi == 2)
  51. return Color.FromArgb(255, p, v, t);
  52. else if (hi == 3)
  53. return Color.FromArgb(255, p, q, v);
  54. else if (hi == 4)
  55. return Color.FromArgb(255, t, p, v);
  56. else
  57. return Color.FromArgb(255, v, p, q);
  58. }
  59.  
  60. Public Sub HSVtoRGB(ByRef r As Double, ByRef g As Double, ByRef b As Double, ByVal h As Double, ByVal s As Double, ByVal v As Double)
  61. Dim i As Integer
  62. Dim f, p, q, t As Double
  63.  
  64. If (s = 0) Then
  65. ' achromatic (grey)
  66. r = v
  67. g = v
  68. b = v
  69. Exit Sub
  70. End If
  71.  
  72. h /= 60 'sector 0 to 5
  73. i = Math.Floor(h)
  74. f = h - i 'factorial part of h
  75. p = v * (1 - s)
  76. q = v * (1 - s * f)
  77. t = v * (1 - s * (1 - f))
  78.  
  79. Select Case (i)
  80. Case 0
  81. r = v
  82. g = t
  83. b = p
  84. Exit Select
  85. Case 1
  86. r = q
  87. g = v
  88. b = p
  89. Exit Select
  90. Case 2
  91. r = p
  92. g = v
  93. b = t
  94. Exit Select
  95. Case 3
  96. r = p
  97. g = q
  98. b = v
  99. Exit Select
  100. Case 4
  101. r = t
  102. g = p
  103. b = v
  104. Exit Select
  105. Case Else 'case 5:
  106. r = v
  107. g = p
  108. b = q
  109. Exit Select
  110. End Select
  111. End Sub
Add Comment
Please, Sign In to add comment