2009-08-26 2 views

답변

17

.NET Framework에서이 작업을 수행하는 방법이 있다고 생각하지 않습니다.
체크 아웃 Converting HSV to RGB colour using C#

void HsvToRgb(double h, double S, double V, out int r, out int g, out int b) 
{  
    double H = h; 
    while (H < 0) { H += 360; }; 
    while (H >= 360) { H -= 360; }; 
    double R, G, B; 
    if (V <= 0) 
    { R = G = B = 0; } 
    else if (S <= 0) 
    { 
    R = G = B = V; 
    } 
    else 
    { 
    double hf = H/60.0; 
    int i = (int)Math.Floor(hf); 
    double f = hf - i; 
    double pv = V * (1 - S); 
    double qv = V * (1 - S * f); 
    double tv = V * (1 - S * (1 - f)); 
    switch (i) 
    { 

     // Red is the dominant color 

     case 0: 
     R = V; 
     G = tv; 
     B = pv; 
     break; 

     // Green is the dominant color 

     case 1: 
     R = qv; 
     G = V; 
     B = pv; 
     break; 
     case 2: 
     R = pv; 
     G = V; 
     B = tv; 
     break; 

     // Blue is the dominant color 

     case 3: 
     R = pv; 
     G = qv; 
     B = V; 
     break; 
     case 4: 
     R = tv; 
     G = pv; 
     B = V; 
     break; 

     // Red is the dominant color 

     case 5: 
     R = V; 
     G = pv; 
     B = qv; 
     break; 

     // Just in case we overshoot on our math by a little, we put these here. Since its a switch it won't slow us down at all to put these here. 

     case 6: 
     R = V; 
     G = tv; 
     B = pv; 
     break; 
     case -1: 
     R = V; 
     G = pv; 
     B = qv; 
     break; 

     // The color is not defined, we should throw an error. 

     default: 
     //LFATAL("i Value error in Pixel conversion, Value is %d", i); 
     R = G = B = V; // Just pretend its black/white 
     break; 
    } 
    } 
    r = Clamp((int)(R * 255.0)); 
    g = Clamp((int)(G * 255.0)); 
    b = Clamp((int)(B * 255.0)); 
} 

/// <summary> 
/// Clamp a value to 0-255 
/// </summary> 
int Clamp(int i) 
{ 
    if (i < 0) return 0; 
    if (i > 255) return 255; 
    return i; 
} 
+6

감사합니다. Colour가 .GetHue(), .GetSaturation() 및 .GetBrightness()를 가지고 있지만 .fromHSB()와 같은 역 방법은 없습니다. – MusiGenesis

+0

사실 ... 아주 이상한 생략입니다. – jsight

+0

세 개의 개별 값에 * out *을 사용하는 대신 Color 객체를 반환하는 것은 어떻습니까? –

48

이 작업을 수행하는 내장 된 방법이 없습니다, 구현 코드이지만, 계산은 대단히 복잡하지 않습니다.
또한 Color의 GetHue(), GetSaturation() 및 GetBrightness()는 HSV가 아닌 HSL 값을 반환합니다.

다음 C# 코드는 Wikipedia에 설명 된 알고리즘을 사용하여 RGB와 HSV 사이를 변환합니다.
이미이 답변 here을 게시했지만 빠른 참조를 위해 여기에 코드를 복사 해 보겠습니다. 및 Windows 시스템 색상을 포함한 모든 필요한 변환을 제공 할 수있는 훌륭한 C# 클래스를 가지고 http://richnewman.wordpress.com/hslcolor-class/에서

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); 
} 
+0

ColorFromHSV에 문제가있을 수 있습니다. 반대 색상의 코드를 사용하여 색조를 180도 회전하려고했는데 너무 잘 작동하지 않습니다. 허용 된 코드는 나에게 맞는 다른 색상을 제공합니다. –

+0

그러나 ColorToHSV 함수를 사용하고 있습니다. 잘 작동하는 것 같습니다. –

+0

@IsaacBolinger는 네거티브 색조에서는 잘 작동하지 않지만 색조> = 0에서는 잘 작동하지만 코드에서 <0, 360> 사이에서는 색조를 사용하는 것이 좋습니다. – xmedeko

-1

봐.

+0

HSL이 아니라 HSB/V가 문제입니다. 종종 섞여 있습니다. 사실 Microsoft 자체는 Color.GetBrightness()를 HSB로 언급함으로써 잘못 이해하고 있습니다. 실제로 HSL입니다. – redshift5

12

내장되어 있지 않지만, 색 공간을 쉽게 변환 할 수있는 ColorMine이라는 오픈 소스 C# 라이브러리가 있습니다. HSV에

RGB : RGB로

var rgb = new Rgb {R = 123, G = 11, B = 7}; 
var hsv = rgb.To<Hsv>(); 

HSV : 그 방법

var hsv = new Hsv { H = 360, S = .5, L = .17 } 
var rgb = hsv.to<Rgb>();