/// <summary> /// /// </summary> /// <param name="c2"></param> /// <param name="amount"></param> /// <returns></returns> public HSLColor Interpolate(HSLColor c2, float amount) { return new HSLColor(this.Hue + ((c2.Hue - this.Hue) * amount), this.Saturation + ((c2.Saturation - this.Saturation) * amount), this.Luminance + ((c2.Luminance - this.Luminance) * amount)); }
/// <summary> /// /// </summary> /// <param name="rr"></param> /// <param name="gg"></param> /// <param name="bb"></param> public static HSLColor FromRGB(int rr, int gg, int bb) { double r = (rr / 255.0); double g = (gg / 255.0); double b = (bb / 255.0); double min = Math.Min(Math.Min(r, g), b); double max = Math.Max(Math.Max(r, g), b); double delta = max - min; HSLColor hsl = new HSLColor(); // get luminance value hsl.Luminance = (float)(max + min) / 2; if (delta == 0) { // gray color hsl.Hue = 0; hsl.Saturation = 0.0f; } else { // get saturation value hsl.Saturation = (float)((hsl.Luminance < 0.5) ? (delta / (max + min)) : (delta / (2 - max - min))); // get hue value double del_r = (((max - r) / 6) + (delta / 2)) / delta; double del_g = (((max - g) / 6) + (delta / 2)) / delta; double del_b = (((max - b) / 6) + (delta / 2)) / delta; double hue; if (r == max) hue = del_b - del_g; else if (g == max) hue = (1.0 / 3) + del_r - del_b; else hue = (2.0 / 3) + del_g - del_r; // correct hue if needed if (hue < 0) hue += 1; if (hue > 1) hue -= 1; hsl.Hue = (int)(hue * 360); } return hsl; }