RGB 색상을 HSV로 변경하는 방법은 무엇입니까?
문제
RGB 색상을 HSV로 변경하는 방법은 무엇입니까? C# 언어로. 외부 라이브러리없이 매우 빠른 방법을 검색합니다.
해결책
단순히 System.Drawing 네임 스페이스 사용을 고려해 보셨습니까? 예를 들어:
System.Drawing.Color color = System.Drawing.Color.FromArgb(red, green, blue);
float hue = color.GetHue();
float saturation = color.GetSaturation();
float lightness = color.GetBrightness();
정확히 당신이 요청한 것이 아닙니다 ( HSL과 HSV의 차이점 그리고 색상 클래스에는 HSL/HSV에서 전환이 없지만 후자는 합리적입니다. 추가하기 쉽습니다.
다른 팁
주목하십시오 Color.GetSaturation()
그리고 Color.GetBrightness()
HSV가 아닌 HSL 값을 반환합니다.
다음 코드는 차이를 보여줍니다.
Color original = Color.FromArgb(50, 120, 200);
// original = {Name=ff3278c8, ARGB=(255, 50, 120, 200)}
double hue;
double saturation;
double value;
ColorToHSV(original, out hue, out saturation, out value);
// hue = 212.0
// saturation = 0.75
// value = 0.78431372549019607
Color copy = ColorFromHSV(hue, saturation, value);
// copy = {Name=ff3278c8, ARGB=(255, 50, 120, 200)}
// Compare that to the HSL values that the .NET framework provides:
original.GetHue(); // 212.0
original.GetSaturation(); // 0.6
original.GetBrightness(); // 0.490196079
다음 C# 코드는 원하는 것입니다. 다음에 설명 된 알고리즘을 사용하여 RGB와 HSV를 변환합니다. 위키 백과. 범위는 0-360입니다 hue
, 및 0-1 saturation
또는 value
.
public static void ColorToHSV(Color color, out double hue, out double saturation, out double value)
{
int max = Math.Max(color.R, Math.Max(color.G, color.B));
int min = Math.Min(color.R, Math.Min(color.G, color.B));
hue = color.GetHue();
saturation = (max == 0) ? 0 : 1d - (1d * min / max);
value = max / 255d;
}
public static Color ColorFromHSV(double hue, double saturation, double value)
{
int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
double f = hue / 60 - Math.Floor(hue / 60);
value = value * 255;
int v = Convert.ToInt32(value);
int p = Convert.ToInt32(value * (1 - saturation));
int q = Convert.ToInt32(value * (1 - f * saturation));
int t = Convert.ToInt32(value * (1 - (1 - f) * saturation));
if (hi == 0)
return Color.FromArgb(255, v, t, p);
else if (hi == 1)
return Color.FromArgb(255, q, v, p);
else if (hi == 2)
return Color.FromArgb(255, p, v, t);
else if (hi == 3)
return Color.FromArgb(255, p, q, v);
else if (hi == 4)
return Color.FromArgb(255, t, p, v);
else
return Color.FromArgb(255, v, p, q);
}
여기에는 C 구현이 있습니다.
http://www.cs.rit.edu/~ncs/color/t_convert.html
거의 기능이 없으므로 C#으로 변환하는 데 매우 간단해야합니다.
발견 된 Google
그만큼 easyrgb 많은 색상 공간 변환이 있습니다. 코드는 다음과 같습니다 RGB-> HSV 변환의 경우.
이것은 Blam의 게시물의 C 코드에서 포팅 된 VB.NET 버전입니다.
여기에는 C 구현이 있습니다.
http://www.cs.rit.edu/~ncs/color/t_convert.html
거의 기능이 없으므로 C#으로 변환하는 데 매우 간단해야합니다.
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)
Dim i As Integer
Dim f, p, q, t As Double
If (s = 0) Then
' achromatic (grey)
r = v
g = v
b = v
Exit Sub
End If
h /= 60 'sector 0 to 5
i = Math.Floor(h)
f = h - i 'factorial part of h
p = v * (1 - s)
q = v * (1 - s * f)
t = v * (1 - s * (1 - f))
Select Case (i)
Case 0
r = v
g = t
b = p
Exit Select
Case 1
r = q
g = v
b = p
Exit Select
Case 2
r = p
g = v
b = t
Exit Select
Case 3
r = p
g = q
b = v
Exit Select
Case 4
r = t
g = p
b = v
Exit Select
Case Else 'case 5:
r = v
g = p
b = q
Exit Select
End Select
End Sub
첫째 : 다음과 같이 비트 맵으로 색상이 있는지 확인하십시오.
Bitmap bmp = (Bitmap)pictureBox1.Image.Clone();
paintcolor = bmp.GetPixel(e.X, e.Y);
(E는 이벤트 핸들러에서 온 것입니다. Wich는 내 색을 골랐습니다!)
내가이 문제를 겪었을 때 내가 한 일은 전에 RGBA (빨강, 녹색, 파란색 및 알파) 값을 먼저 얻었습니다. 다음으로 플로트 색조, 플로트 포화, 플로트 밝기 등 3 개의 플로트를 만들었습니다. 그런 다음 단순히 :
hue = yourcolor.Gethue;
saturation = yourcolor.GetSaturation;
brightness = yourcolor.GetBrightness;
전체 로트는 다음과 같습니다.
Bitmap bmp = (Bitmap)pictureBox1.Image.Clone();
paintcolor = bmp.GetPixel(e.X, e.Y);
float hue;
float saturation;
float brightness;
hue = paintcolor.GetHue();
saturation = paintcolor.GetSaturation();
brightness = paintcolor.GetBrightness();
이제 레이블로 표시하려면 다음과 같이하십시오.
yourlabelname.Text = hue.ToString;
yourlabelname.Text = saturation.ToString;
yourlabelname.Text = brightness.ToString;
여기로 이동합니다. 이제 HSV 값에 RGB 값이 있습니다 :)
도움이 되었기를 바랍니다