예제 #1
0
        /// <summary>
        /// HSLからRGBへ変換を行う。
        /// </summary>
        /// <param name="hsl">HSL色</param>
        /// <param name="alpha">アルファ値(0≦alpha≦255)</param>
        /// <returns>RGB色</returns>
        public static Color ConvertHSLtoRGB(ColorHSL hsl, byte alpha)
        {
            float min = (hsl.Lightness < 0.5f)
                ? (hsl.Lightness - hsl.Lightness * (hsl.Saturation)) * 255.0f
                : (hsl.Lightness - (1.0f - hsl.Lightness) * hsl.Saturation) * 255.0f;
            float max = (hsl.Lightness < 0.5f)
                ? (hsl.Lightness + hsl.Lightness * (hsl.Saturation)) * 255.0f
                : (hsl.Lightness + (1.0f - hsl.Lightness) * hsl.Saturation) * 255.0f;

            int r, g, b;

            if ((hsl.Hue >= 0) && (hsl.Hue < 60))
            {
                r = Convert.ToInt32(max);
                g = Convert.ToInt32((hsl.Hue / 60.0f) * (max - min) + min);
                b = Convert.ToInt32(min);
            }
            else if ((hsl.Hue >= 60) && (hsl.Hue < 120))
            {
                r = Convert.ToInt32(((120.0f - hsl.Hue) / 60.0f) * (max - min) + min);
                g = Convert.ToInt32(max);
                b = Convert.ToInt32(min);
            }
            else if ((hsl.Hue >= 120) && (hsl.Hue < 180))
            {
                r = Convert.ToInt32(min);
                g = Convert.ToInt32(max);
                b = Convert.ToInt32(((hsl.Hue - 120.0f) / 60.0f) * (max - min) + min);
            }
            else if ((hsl.Hue >= 180) && (hsl.Hue < 240))
            {
                r = Convert.ToInt32(min);
                g = Convert.ToInt32(((240 - hsl.Hue) / 60.0f) * (max - min) + min);
                b = Convert.ToInt32(max);
            }
            else if ((hsl.Hue >= 240) && (hsl.Hue < 300))
            {
                r = Convert.ToInt32(((hsl.Hue - 240) / 60.0f) * (max - min) + min);
                g = Convert.ToInt32(min);
                b = Convert.ToInt32(max);
            }
            else if ((hsl.Hue >= 300) && (hsl.Hue < 360))
            {
                r = Convert.ToInt32(max);
                g = Convert.ToInt32(min);
                b = Convert.ToInt32(((360 - hsl.Hue) / 60.0f) * (max - min) + min);
            }
            else
            {
                r = 0;
                g = 0;
                b = 0;
            }
            return(ColorUtility.GetColor(alpha, r, g, b));
        }
예제 #2
0
        /// <summary>
        /// HSVからRGBへ変換を行う
        /// </summary>
        /// <param name="hsv">HSVカラー</param>
        /// <param name="alpha">アルファ値(0≦alpha≦255)</param>
        /// <returns>RGBカラーが返る</returns>
        public static Color ConvertHSVtoRGB(ColorHSV hsv, byte alpha)
        {
            int h = (Convert.ToInt32(hsv.Hue) / 60) % 6;
            int v = Convert.ToInt32(hsv.Value * 255.0f);
            int p = Convert.ToInt32((hsv.Value * (1.0f - hsv.Saturation)) * 255.0f);
            int q = Convert.ToInt32(hsv.Value * (1.0f - hsv.Saturation * (hsv.Hue - h * 60.0f) / 60.0f) * 255.0f);
            int t = Convert.ToInt32(hsv.Value * (1.0f - hsv.Saturation * ((h + 1.0f) * 60.0f - hsv.Hue) / 60.0f) * 255.0f);

            if ((hsv.Hue >= 0) && (hsv.Hue < 60))
            {
                return(ColorUtility.GetColor(alpha, v, t, p));
            }
            else if ((hsv.Hue >= 60) && (hsv.Hue < 120))
            {
                return(ColorUtility.GetColor(alpha, q, v, p));
            }
            else if ((hsv.Hue >= 120) && (hsv.Hue < 180))
            {
                return(ColorUtility.GetColor(alpha, p, v, t));
            }
            else if ((hsv.Hue >= 180) && (hsv.Hue < 240))
            {
                return(ColorUtility.GetColor(alpha, p, q, v));
            }
            else if ((hsv.Hue >= 240) && (hsv.Hue < 300))
            {
                return(ColorUtility.GetColor(alpha, t, p, v));
            }
            else if ((hsv.Hue >= 300) && (hsv.Hue < 360))
            {
                return(ColorUtility.GetColor(alpha, v, p, q));
            }
            else
            {
                return(ColorUtility.GetColor(alpha, 0, 0, 0));
            }
        }