Change HSL Color values

    0

    0

    Jason Storey

    The ColorHSL struct contains a constructor to create an instance of the ColorHSL struct based on a provided hue, saturation, and lightness values. The ColorHSL struct also has a ToString method that returns the hue, saturation, and lightness values as a string.

    public static class ColorExtensions
    {
      public static Color WithHue(this Color rgb,float hue) =>
      ColorHSL.From(rgb).WithHue(hue);
    
      public static Color WithRandomHue(this Color c, float min = 0, float max = 1) =>
      c.WithHue(Mathf.Lerp(min, max, Random.value));
    
      public static Color WithSaturation(this Color c, float saturation) =>
      ColorHSL.From(c).WithSaturation(saturation);
    
      public static Color WithRandomSaturation(this Color c, float min = 0, float max = 1) =>
      c.WithSaturation(Mathf.Lerp(min, max, Random.value));
    
      public static Color WithLightness(this Color c,float value)=>
      ColorHSL.From(c).WithLightness(value);
    
      public static Color WithRandomLightness(this Color c, float min = 0, float max = 1) =>
      c.WithLightness(Mathf.Lerp(min, max, Random.value));
    
      public static Color WithAlpha(this Color c, float a) => new(c.r, c.g, c.b, a);
    }
    
    [Serializable]
    
    public struct ColorHSL
    {
      public static ColorHSL From(float r, float g, float b) => new Color(r, g, b);
      public static ColorHSL From(Color c) => c;
      
      public static readonly ColorHSL White = new(0, 0, 1);
      public static readonly ColorHSL Black = new(0, 0, 0);
      
      public readonly float Hue;
      public readonly float Saturation;
      public readonly float Lightness;
      
      public static implicit operator Color(ColorHSL hsl) => Color.HSVToRGB(hsl.Hue, hsl.Saturation, hsl.Lightness);
      
      public static implicit operator ColorHSL(Color col)
      {
        Color.RGBToHSV(col,out var h,out var s,out var l);
        return new ColorHSL(h, s, l);
      }
      
      public ColorHSL(float h,float s,float l)
      {
      Hue = Mathf.Clamp01(h);
      Saturation = Mathf.Clamp01(s);
      Lightness = Mathf.Clamp01(l);
      }
    
      public ColorHSL Inverted() => new(Hue + 0.5f, 0, 1 - Lightness);
      public ColorHSL WithInvertedLuminance() => new ColorHSL(Hue, Saturation, 1 - Lightness);
      public ColorHSL WithInvertSaturation() => new ColorHSL(Hue, 1 - Saturation, Lightness);
      public ColorHSL WithHue(float hue) => new(hue, Saturation, Lightness);
      public ColorHSL WithSaturation(float saturation) => new(Hue, saturation, Lightness);
      public ColorHSL WithLightness(float lightness) => new(Hue, Saturation, lightness);
      
      public override string ToString() => string.Format("[h: {0}, s: {1}, l: {2}]", Hue, Saturation, Lightness);
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.