Color to Hue,Lightness,Saturation value : Color « 2D Graphics « C# / C Sharp






Color to Hue,Lightness,Saturation value

  

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media;

namespace WozLib.UI.Util
{
  public class HlsConverter
  {
        public static Color ConvertFromHls(HlsValue value){
          return ConvertFromHls(value.Hue, value.Lightness, value.Saturation);
        }

    /// <summary>
    /// Converts a hls value to a Color.
    /// </summary>
    /// <param name="h">The hue, H, of the new color (from 0 to 1).</param>
    /// <param name="l">The lightness, L, of the new color (from 0 to 1).</param>
    /// <param name="s">The saturation, S, of the new color (from 0 to 1).</param>
    public static Color ConvertFromHls(Double h, Double l, Double s) {
        Double p1;
        Double p2;

        Double r;
        Double g;
        Double b;

        h *= 360;

        if (l <= 0.5){
            p2 = l * (1 + s);
        }
        else {
            p2 = l + s - l * s;
        }

        p1 = 2 * l - p2;
        if (s == 0) {
            r = l;
            g = l;
            b = l;
        }
        else {
            r = QqhToRgb(p1, p2, h + 120);
            g = QqhToRgb(p1, p2, h);
            b = QqhToRgb(p1, p2, h - 120);
        }

        r *= Byte.MaxValue;
        g *= Byte.MaxValue;
        b *= Byte.MaxValue;

        return Color.FromRgb((byte)r, (byte)g, (byte)b);
  }

    private static double QqhToRgb(double q1, double q2, double hue){
        if (hue > 360) {
            hue = hue - 360;
        }
        else if (hue < 0) {
            hue = hue + 360;
        }

        if (hue < 60) {
            return q1 + (q2 - q1) * hue / 60.0;
        }
        else if (hue < 180) {
          return q2;
        }
        else if (hue < 240) {
          return q1 + (q2 - q1) * (240 - hue) / 60.0;
        }
        else {
          return q1;
        }
    }

    /// <summary>
    /// Converts a color to a HlsValue.
    /// </summary>
    /// <param name="color">The color to convert.</param>
    public static HlsValue ConvertToHls(Color color){
        Double max;
        Double min;
        Double diff;
        Double r_dist;
        Double g_dist;
        Double b_dist;

        Double R;
        Double G;
        Double B;

        Double h;
        Double l;
        Double s;

      double ByteMaxValue = Byte.MaxValue;
      R = color.R / ByteMaxValue;
      G = color.G / ByteMaxValue;
      B = color.B / ByteMaxValue;

        max = R;
        if ( max < G ) max = G;
        if ( max < B ) max = B;

        min = R;
        if ( min > G ) min = G;
        if ( min > B ) min = B;

        diff = max - min;
        l = (max + min) / 2.0;
        if (Math.Abs(diff) < 0.00001) {
            s = 0;
            h = 0;
        }
        else {
            if (l <= 0.5) {
                s = diff / (max + min);
            }
            else {
                s = diff / (2 - max - min);
            }

            r_dist = (max - R) / diff;
            g_dist = (max - G) / diff;
            b_dist = (max - B) / diff;

            if (R == max) {
                h = b_dist - g_dist;
            }
            else if (G == max) {
                h = 2 + r_dist - b_dist;
            }
            else {
                h = 4 + g_dist - r_dist;
            }

            h = h * 60;
            if (h < 0) h = h + 360;
        }

        h /= 360;
        return new HlsValue(h, l, s);
    }
  }

  public struct HlsValue
  {
    private double mHue;
    private double mLightness;
    private double mSaturation;

    public HlsValue(double h, double l, double s)
    {
      mHue = h;
      mLightness = l;
      mSaturation = s;
    }

    public double Hue
    {
      get
      {
        return mHue;
      }
      set
      {
        mHue = value;
      }
    }

    public double Lightness
    {
      get
      {
        return mLightness;
      }
      set
      {
        mLightness = value;
      }
    }

    public double Saturation
    {
      get
      {
        return mSaturation;
      }
      set
      {
        mSaturation = value;
      }
    }

    public static bool operator !=(HlsValue left, HlsValue right)
    {
      bool bool1;

      bool1 = left.Lightness != right.Lightness || left.Saturation != right.Saturation;

      if (!bool1)
      {
        return left.Hue != right.Hue && (left.Hue > 0 && right.Hue < 1) && (left.Hue < 1 && right.Hue > 0);
      }
      else
      {
        return bool1;
      }
    }

    public static bool operator ==(HlsValue left, HlsValue right)
    {
      bool bool1;

      bool1 = left.Lightness == right.Lightness && left.Saturation == right.Saturation;

      if (bool1)
      {
        return Math.Round(left.Hue, 2) == Math.Round(right.Hue, 2) || (Math.Round(left.Hue, 2) == 1 &&
          Math.Round(right.Hue, 2) == 0) || (Math.Round(left.Hue, 2) == 0 && Math.Round(right.Hue, 2) == 1);
      }
      else
      {
        return false;
      }
    }

    public override string ToString()
    {
      return String.Format("H: {0:0.00} L: {1:0.00} S: {2:0.00}", Hue, Lightness, Saturation);
    }
  }
}

   
    
  








Related examples in the same category

1.Transparent colorTransparent color
2.List all known color in a systemList all known color in a system
3.Draw each of 100 cells with randomly chosen colorsDraw each of 100 cells with randomly chosen colors
4.Filled with the semi transparent and transparent colorFilled with the semi transparent and transparent color
5.All the colors that are supported in C# according
6.Color ChangerColor Changer
7.Known ColorsKnown Colors
8.Five yellow squares with different alpha values(Transparensy)
9.Create two color instances with different alpha components
10.Color.Chocolate
11.Use Color.FromArgb to create Color
12.Get all known color
13.Color representation in r,g,b with values [0.0 - 1.0].
14.Color representation in h,s,v with h = [0 - 360], s,v = [0.0 - 1.0].
15.Return a randomly-generated color
16.Color to RGB value
17.Get color outof String
18.Parse Color
19.Color To Rgb
20.Rgb To Color
21.Returns an HTML #XXXXXX format for a color.
22.Convert color name to Color object
23.Hex Color Util
24.Helper method to get a color based on it's string value
25.Converts a color string to a hex value string
26.Get Color From String
27.Convert String value To Color
28.Color to String
29.Masks an image and returns a two color bitmap.
30.Convert Hex To Color
31.Create Color From HLS value
32.Parse Color with Color.FromArgb
33.Create Color Object from RGB value
34.Get Rainbow Colors
35.Convert RGB to HSL
36.HSL to RGB conversion.
37.Hsv To Rgb
38.Rgb Linear Interpolate
39.Get Luminance
40.To Grey scale
41.Heat Map background colour calculations