0

I'm using the HSL colour space. The Hue factor is from 0 to 360 degrees, and going from zero to 360 means to go a full circle around on the colour circle. So 0 is close to 360 (or they are the same). This implies that to do some range checking one would need the modulo function.

I need to check if value HueX is within distance RangeH of Hue: So if Hue = 20 and RangeH = 50 then if HueX = 350 then the value of 350 is in range.

I've been trying a few combinations of math but not with the results I was hoping for, as I think this could be written as a boolean function one liner.

user613326
  • 2,140
  • 9
  • 34
  • 63
  • I have no clue what you need. Maybe you can correct a few typos in your post and see where this leads? Is Heu the same as Hue? – nvoigt Feb 10 '15 at 15:11
  • I mend Hue, HSL is a cylindric color space model. Where H,..Hue stands for the color, S for saturation (color vs gray), and L for how luminous, or how light a color is. – user613326 Feb 10 '15 at 15:16
  • 2
    You may want to have a look at [this post](http://stackoverflow.com/questions/27374550/how-to-compare-color-object-and-get-closest-color-in-an-color/27375621#27375621); espcially the line where: `float d = Math.Abs(hue1 - hue2); return d > 180 ? 360 - d : d; }` – TaW Feb 10 '15 at 15:17

2 Answers2

2

I use this method to work with Hue values:

public static double HueDifference(double hue1, double hue2)
{
    return Math.Min(Math.Abs(hue1 - hue2), 360 - Math.Abs(hue1- hue2));
}

Then you can check if the value is within the given range like this:

if (HueDifference(HueX, Hue) <= RangeH)
    // ...
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
0

Well based upon Taw4, i wrote a function that included the HSL hue math. Its not the onliner check, which i think exists using a mod calculation. And well i do was looking for that one, but i dont remember it. I rewrote the logic of the link from Taw4 a bit.

I wrote this as part of my function that checks if an RGB color is within an HSL range, the S and L factors are quite easy, but i the H got me troubled. I just post the whole thing here, just in case someone needs it.

        private Boolean RGBInHSLRange
          (int r, int g, int b,
           int h,int s,int l,
           int RH, int RS, int RL)   
    {   // r,g,b colors
        // h,s,l colors
        // ranges for HSL in RH,RS,RL

        // note color math is usually done in floats not integers
        // if you need floats do a float conversion instead of int
        // for me int ewas enough 
        Color myColor = Color.FromArgb(r, g, b);
        int HSLhue = (int)myColor.GetHue(); 
        int HSLsat = (int)(myColor.GetSaturation() * 100);
        int HSLlight = (int)(myColor.GetBrightness() * 100);


        if (( HSLlight < h -RL) ^ ( HSLlight > h +RL)) return false;
        if (( HSLsat < s - RS) ^ ( HSLsat > s + RS)) return false;

        int distance = Math.Abs(h - HSLhue);
        if (distance > 180) distance = 360 - distance;
        if (distance > RH) return false;
        return true;           
    }
user613326
  • 2,140
  • 9
  • 34
  • 63