/// <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)); }
/// <summary> /// HSVの色調整を行ってピクセルデータを返す。 /// </summary> /// <param name="c">カラー</param> /// <param name="hue">色差加算値(-360≦hue≦360)</param> /// <param name="saturation">彩度加割合(-255≦saturation≦255)</param> /// <param name="lightness">輝度加算割合(-255≦lightness≦255)</param> /// <returns></returns> public static Color ProcessHSLFilter(Color c, int hue, int saturation, int lightness) { if (((hue == 0) && (saturation == 0) && (lightness == 0))) { return(c); // 色変換しない。 } ColorHSL srcHSL = ColorConverter.ConvertRGBtoHSL(c); float h = ColorUtility.GetHueWithLimitedRange((srcHSL.Hue + hue) % 360.0f); float s = ColorUtility.ModifyValueByPercent(srcHSL.Saturation, 0f, 1.0f, saturation / 255.0f); float l = Math.Min(ColorUtility.ModifyValueByPercent(srcHSL.Lightness, 0f, srcHSL.Lightness * 2, lightness / 255.0f), 1.0f); return(ColorConverter.ConvertHSLtoRGB(ColorHSL.FromHSL(h, s, l), c.A)); }
/// <summary> /// RGBからHSLへ変換を行う。 /// </summary> /// <param name="rgb">RGB色</param> /// <returns>HSL色</returns> public static ColorHSL ConvertRGBtoHSL(Color rgb) { int min = Math.Min(rgb.G, Math.Min(rgb.B, rgb.R)); int max = Math.Max(rgb.G, Math.Max(rgb.B, rgb.R)); float hue = GetHue(rgb, min, max); float lightness = ((min + max) / 2.0f) / 255.0f; float saturation; if (min == max) { saturation = 0.0f; } else { saturation = (lightness < 0.5) ? (float)(max - min) / (float)(max + min) : (float)(max - min) / (2 * 255.0f - max - min); } return(ColorHSL.FromHSL(hue, saturation, lightness)); }
/// <summary> /// colorHSVで指定される色と一致しているかどうかを判定する。 /// </summary> /// <param name="colorHSL">色</param> /// <returns>同値なものである場合にはtrue, それ以外はfalse</returns> public bool Equals(ColorHSL colorHSL) { return((colorHSL.Hue == Hue) && (colorHSL.Saturation == Saturation) && (colorHSL.Lightness == Lightness)); }
/// <summary> /// HSLからRGBへ変換を行う。 /// </summary> /// <param name="hsl">HSL色</param> /// <param name="alpha">アルファ値(0≦alpha≦1.0)</param> /// <returns>RGB色</returns> public static Color ConvertHSLtoRGB(ColorHSL hsl, float alpha) { int a = Convert.ToInt32(ColorUtility.Clamp(alpha * 255.0f, 0.0f, 255.0f)); return(ConvertHSLtoRGB(hsl, (byte)(a))); }
/// <summary> /// HSLからRGBへ変換を行う。 /// </summary> /// <param name="hsl">HSL色</param> /// <returns>RGB色</returns> public static Color ConvertHSLtoRGB(ColorHSL hsl) => ConvertHSLtoRGB(hsl, hsl.Alpha);