2015-02-10 87 views
0

我使用HSL顏色空間。 色調因子從0到360度,從零到360意味着在色環上轉了一圈。所以0接近360(或者它們是相同的)。這意味着要進行一些範圍檢查,需要模函數。如何檢查HSL顏色的色調是否在給定的範圍內?

我需要檢查是否值HueX是距離HueRangeH內: 所以,如果Hue = 20RangeH = 50然後如果HueX = 350350值範圍內。

我一直在嘗試一些數學的組合,但不是我期待的結果,因爲我認爲這可以寫成一個布爾函數。

+0

我不知道你需要什麼。也許你可以在你的文章中糾正幾個拼寫錯誤,看看這會導致什麼? Heu和Hue一樣嗎? – nvoigt 2015-02-10 15:11:21

+0

我修補色相,HSL是一個圓柱形色彩空間模型。其中H,.. Hue代表顏色,S代表飽和度(顏色vs灰色),L代表明亮程度,或者顏色是多淡。 – user613326 2015-02-10 15:16:08

+2

你可能想看看[這篇文章](http://stackoverflow.com/questions/27374550/how-to-compare-color-object-and-get-closest-color-in-an-color/ 27375621#27375621);特別是其中的行:'float d = Math.Abs​​(hue1 - hue2);返回d> 180? 360 - d:d; }' – TaW 2015-02-10 15:17:21

回答

2

我用這個方法與色調值的工作:

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

然後你就可以檢查是否值是給定的範圍是這樣:水井基於Taw4

if (HueDifference(HueX, Hue) <= RangeH) 
    // ... 
0

,我寫道:一個包含HSL色調數學的函數。 它不是onliner檢查,我認爲這是使用mod計算存在的。 嗯,我一直在尋找那個,但我不記得它。 我重寫了一下Taw4鏈接的邏輯。

我寫了這個作爲我的函數的一部分,檢查RGB顏色是否在HSL範圍內,S和L因子很容易,但我H讓我感到困擾。我只是在這裏發佈整個事情,以防萬一有人需要它。

 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;   
    }