2008-08-05 11 views

답변

13

색상 구조체는 GetHue, GetSaturation 및 GetBrightness의 세 가지 메소드를 제공합니다.

Bob Powell은 수년 전에 interesting piece on this을 작성했습니다.

"HSL"(및 관련 "HSV")은 1970 년 제록스의 Palo Alto Research Center (PARC)에서 비롯된 많은 것들 중 하나입니다. Alvy Ray Smith입니다.

+0

Bob Powell의 코드는 실제로 편리합니다. 이후로 내 물건에 사용하고 System.Windows.Media.Color 클래스의 확장 메서드를 제공하도록 변환했습니다. –

+0

http://www.bobpowell.net/RGBHSB.htm에 대한 링크가 죽은 링크입니다 –

+0

감사합니다. Derek에게 archive.org의 기사 사본 링크가 추가되었습니다. –

14

이 ColorRGB 클래스는 System.Drawing.Color와의 암시 적 변환과 함께 HSL을 가져오고 설정하는 방법을 제공합니다. 우수 사례 인 GeekMonkey.com을 기반으로합니다.

Public Declare Sub ColorRGBToHLS Lib "shlwapi.dll" _ 
            (ByVal clrRGB As UInteger, _ 
            ByRef pwHue As Short, _ 
            ByRef pwLuminance As Short, _ 
            ByRef pwSaturation As Short) 
Public Declare Function ColorHLSToRGB Lib "shlwapi.dll" _ 
             (ByVal wHue As Short, _ 
             ByVal wLuminance As Short, _ 
             ByVal wSaturation As Short) As UInteger 

:

secretGeek의 대답에 추가
using System; 
using System.Drawing; 

namespace RMA.Drawing 
{ 
    public class ColorRGB 
    { 
    public byte R; 
    public byte G; 
    public byte B; 
    public byte A; 

    public ColorRGB() 
    { 
     R = 255; 
     G = 255; 
     B = 255; 
     A = 255; 
    } 

    public ColorRGB(Color value) 
    { 
     this.R = value.R; 
     this.G = value.G; 
     this.B = value.B; 
     this.A = value.A; 
    } 

    public static implicit operator Color(ColorRGB rgb) 
    { 
     Color c = Color.FromArgb(rgb.A, rgb.R, rgb.G, rgb.B); 
     return c; 
    } 

    public static explicit operator ColorRGB(Color c) 
    { 
     return new ColorRGB(c); 
    } 

    // Given H,S,L in range of 0-1 
    // Returns a Color (RGB struct) in range of 0-255 
    public static ColorRGB FromHSL(double H, double S, double L) 
    { 
     return FromHSLA(H, S, L, 1.0); 
    } 

    // Given H,S,L,A in range of 0-1 
    // Returns a Color (RGB struct) in range of 0-255 
    public static ColorRGB FromHSLA(double H, double S, double L, double A) 
    { 
     double v; 
     double r, g, b; 

     if (A > 1.0) 
     A = 1.0; 

     r = L; // default to gray 
     g = L; 
     b = L; 
     v = (L <= 0.5) ? (L * (1.0 + S)) : (L + S - L * S); 

     if (v > 0) 
     { 
     double m; 
     double sv; 
     int sextant; 
     double fract, vsf, mid1, mid2; 

     m = L + L - v; 
     sv = (v - m)/v; 
     H *= 6.0; 
     sextant = (int)H; 
     fract = H - sextant; 
     vsf = v * sv * fract; 
     mid1 = m + vsf; 
     mid2 = v - vsf; 

     switch (sextant) 
     { 
      case 0: 
      r = v; 
      g = mid1; 
      b = m; 
      break; 

      case 1: 
      r = mid2; 
      g = v; 
      b = m; 
      break; 

      case 2: 
      r = m; 
      g = v; 
      b = mid1; 
      break; 

      case 3: 
      r = m; 
      g = mid2; 
      b = v; 
      break; 

      case 4: 
      r = mid1; 
      g = m; 
      b = v; 
      break; 

      case 5: 
      r = v; 
      g = m; 
      b = mid2; 
      break; 
     } 
     } 

     ColorRGB rgb = new ColorRGB(); 
     rgb.R = Convert.ToByte(r * 255.0f); 
     rgb.G = Convert.ToByte(g * 255.0f); 
     rgb.B = Convert.ToByte(b * 255.0f); 
     rgb.A = Convert.ToByte(A * 255.0f); 
     return rgb; 
    } 

    // Hue in range from 0.0 to 1.0 
    public float H 
    { 
     get 
     { 
     // Use System.Drawing.Color.GetHue, but divide by 360.0F 
     // because System.Drawing.Color returns hue in degrees (0 - 360) 
     // rather than a number between 0 and 1. 
     return ((Color)this).GetHue()/360.0F; 
     } 
    } 

    // Saturation in range 0.0 - 1.0 
    public float S 
    { 
     get 
     { 
     return ((Color)this).GetSaturation(); 
     } 
    } 

    // Lightness in range 0.0 - 1.0 
    public float L 
    { 
     get 
     { 
     return ((Color)this).GetBrightness(); 
     } 
    } 
    } 
} 
+1

FromHSLA는 h = 1.0의 경우를 처리하지 않습니다. –

+1

코드를 업데이트했습니다. switch 문이 이제 h = 1.0을 처리합니다 –

+0

아니요, Alpha 클램핑을 수정했습니다. 문제는 Hue가 1.0 일 수 있다는 것입니다.이 값은 6의 육수 값을 제공하며, case 문에서 처리되지 않습니다. –

6

하는 HSL 값과 색상 (또는 그 반대), 당신은 이러한 네이티브 함수 호출 (Visual Basic의 샘플 코드를) 사용할 수를 얻을 수 있습니다 (색상 매개 변수/결과를 전달/수신 할 때 ColorTranslator.ToWin32ColorTranslator.FromWin32 사용)