【问题标题】:How to check if a HSL color's hue is in given range?如何检查 HSL 颜色的色调是否在给定范围内?
【发布时间】:2015-02-10 15:08:50
【问题描述】:

我正在使用 HSL 颜色空间。 色调因子是从 0 到 360 度,从 0 到 360 意味着在色环上绕了一整圈。所以 0 接近 360(或者它们是相同的)。这意味着要进行一些范围检查,需要模函数。

我需要检查值HueX 是否在RangeH 的距离Hue 内: 所以如果Hue = 20RangeH = 50 然后如果HueX = 350 那么350 的值在范围内。

我一直在尝试一些数学组合,但没有得到我希望的结果,因为我认为这可以写成一个布尔函数。

【问题讨论】:

  • 我不知道你需要什么。也许您可以更正帖子中的一些错别字,看看这会导致什么? Heu 和 Hue 一样吗?
  • 我修色相,HSL 是一个圆柱颜色空间模型。其中 H,..Hue 代表颜色,S 代表饱和度(颜色与灰色),L 代表亮度或颜色的亮度。
  • 你可能想看看this post;尤其是以下行:float d = Math.Abs(hue1 - hue2); return d > 180 ? 360 - d : d; }

标签: c# hsl


【解决方案1】:

我使用这种方法来处理色调值:

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

然后您可以检查该值是否在给定范围内,如下所示:

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

【讨论】:

    【解决方案2】:

    基于 Taw4,我编写了一个包含 HSL 色调数学的函数。 它不是在线检查,我认为使用 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;           
        }
    

    【讨论】:

      猜你喜欢
      • 2011-07-17
      • 2018-11-22
      • 1970-01-01
      • 2012-11-11
      • 2010-11-01
      • 2021-03-15
      • 1970-01-01
      • 2019-11-26
      • 2022-06-13
      相关资源
      最近更新 更多