gpt4 book ai didi

colors - 确定 HSL 变化以将颜色转换为另一种颜色

转载 作者:行者123 更新时间:2023-12-01 14:00:52 26 4
gpt4 key购买 nike

我使用 LESS ,我想利用各种集成的 color functions 只允许设置几个基本颜色,然后派生其他改变色相,饱和度,亮度,旋转,ecc。

假设我们的着色器中有以下 2 种颜色(在本例中为浅绿色和深绿色):

@primary-color:    rgb(0,100,60);
@secondary-color: rgb(185,215,50);

我想仅显式设置 @primary-color,然后在适当的 HSL 转换后获取 @secondary-color。 (例如 darken(hsl(90, 80%, 50%), 20% ))

有什么方法可以确定 我必须对 @primary-color 应用什么 hsl 设置才能获得 @secondary-color 吗?

换句话说:

给定 2 个 RGB 颜色定义,是否有任何方法可以确定它们之间在色调、饱和度和亮度方面存在哪些差异,以将 @secondary-color 表示为 @primary-color 的变体?

P.S.:如有必要,也可以借助 Photoshop 等外部工具。

最佳答案

这里是计算两种颜色的色调、饱和度和亮度值之间的差异,然后用它来计算基于第一种颜色的第二种颜色的方法。

各个步骤如下:

  • 色差计算: 使用 hue()saturation()lightness() 函数计算两种给定颜色之间的色相、饱和度和亮度差异。此功能可以单独使用,只是为了单独输出差异。
  • 到达基于原色的二次色: 这是一个三步过程,它们如下:
  • 使用 spin() 函数通过传递两种颜色之间的色调差异来调整原色的色调
  • 根据差异使用 saturate()desaturate() 函数调整色相调整后的颜色(来自上一步)的饱和度。
  • 根据差异使用 darken()lighten() 函数调整饱和度调整后的颜色(来自上一步)的亮度。

  • 这个答案是这个 SASS Article 关于如何从另一种颜色计算一种颜色的较少改编。

    @primary: rgb(0,100,60); /* primary color */
    @secondary: rgb(185,215,50); /* secondary color */

    /* mixin to calculate the difference between two given colors */
    .color-diff(@color1; @color2){
    @hueDiff: hue(@color2) - hue(@color1);
    @saturationDiff: saturation(@color1) - saturation(@color2);
    @lightnessDiff: lightness(@color1)- lightness(@color2);

    color1: @color1; color2:@color2; /* just for testing, can be removed */
    }

    /* Step 1: mixin to adjust the hue difference between the colors */
    .adjust-hue(@color; @diff){
    @hueAdjusted: spin(@color, @hueDiff);
    }

    /* Step 2: mixin to adjust the saturation difference */
    .adjust-saturation(@color; @diff) when (@diff > 0){
    @satAdjusted: desaturate(@color, abs(@diff)); /* desaturate if diff is greater than 0 */
    }
    .adjust-saturation(@color; @diff) when not (@diff > 0){
    @satAdjusted: saturate(@color, abs(@diff)); /* saturate if diff is not greater than 0 */
    }

    /* Step 3: mixin to adjust the lightness diff between the colors */
    .adjust-lightness(@color; @diff) when (@diff > 0){
    @lightnessAdjusted: darken(@color, abs(@diff)); /* darken if diff is greater than 0 */
    }
    .adjust-lightness(@color; @diff) when not (@diff > 0){
    @lightnessAdjusted: lighten(@color, abs(@diff)); /* else lighten */
    }

    div{
    .color-diff(@primary; @secondary);
    .adjust-hue(@primary; @hueDiff);
    .adjust-saturation(@hueAdjusted; @saturationDiff);
    .adjust-lightness(@satAdjusted; @lightnessDiff);
    color: @lightnessAdjusted; /* final output value */
    }

    编译的 CSS:
    div {
    color1: #00643c;
    color2: #b9d732;
    color: #b9d732;
    }

    如果您只想获得两种颜色之间的差异,那么您可以使用如下所示的循环来输出色调、饱和度和亮度值方面的差异。

    @color-list-1: rgb(0,100,60), #B0BCA7, #ABCDEF; /* list of primary colors */
    @color-list-2: rgb(185,215,50), #BADA55, #FEDCBA; /* list of secondary colors */

    #output{
    .loop-colors(@index) when (@index > 0){
    @primary: extract(@color-list-1, @index);
    @secondary: extract(@color-list-2, @index);
    .color-diff(@primary; @secondary);

    /* output the values of the comparison */
    color-comparison-@{index}+: ~"Hue Difference: @{hueDiff}";
    color-comparison-@{index}+: ~"Saturation Difference: @{saturationDiff}";
    color-comparison-@{index}+: ~"Lightness Difference: @{lightnessDiff}";
    .loop-colors(@index - 1);
    }
    .loop-colors(length(@color-list-1));
    }

    上面的代码将比较两个列表中的相应值并输出它们的差异,如下所示:

    #output {
    color-comparison-3: Hue Difference: -180, Saturation Difference: -29.142857142857153%, Lightness Difference: -5.882352941176478%;
    color-comparison-2: Hue Difference: -19.849624060150404, Saturation Difference: -50.70282063269439%, Lightness Difference: 10.196078431372548%;
    color-comparison-1: Hue Difference: -85.09090909090908, Saturation Difference: 32.65306122448979%, Lightness Difference: -32.352941176470594%;
    }

    关于colors - 确定 HSL 变化以将颜色转换为另一种颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28698726/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com