728x90
728x170

HSV : hue saturation value의 약어. 색상(H), 채도(S), 명도(V) 를 의미한다.

 

아래 메서드를 통해 RGB,HSV 간에 변환을 할수 있다.

 

        /// <summary>

        /// Color 를 HSV 값으로 반환합니다.

        /// </summary>

        /// <param name="color">Color</param>

        /// <param name="hue">hue</param>

        /// <param name="saturation">saturation</param>

        /// <param name="value">value</param>

        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;

        }

 

        /// <summary>

        /// HSV 값을 RGB 로 반환합니다.

        /// </summary>

        /// <param name="hue">hue</param>

        /// <param name="saturation">saturation</param>

        /// <param name="value">value</param>

        /// <returns>Color</returns>

        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);

        }

 

 

(테스트)

 

            Color blueColor = Color.LightBlue;

 

            ColorToHSV(blueColor, out double hue, out double saturation, out double value);

 

            Color convertColor =  ColorFromHSV(hue, saturation, value);

 

            if (blueColor.ToArgb() == convertColor.ToArgb())

            {

 

            }

 

 

값을 보면 같은 값으로 다시 변환되서 나온걸 확인 할수 있다.

 

 

 

728x90
그리드형
Posted by kjun.kr
,