c++ - 在 cpp 中创建颜色变化

标签 c++ qt colors

我有一个给定的颜色,并希望在色调、饱和度和亮度方面创建它的变体。

我发现一个网页可以按照我想要的方式创建变体(请参阅 http://coloreminder.com/ )。然而,我并不完全理解如何为任意颜色创建这些变化。从我在这个主页上考虑创建的变体来看,仅仅简单地单独更改 HSL 值来创建变体似乎还不够。

因此,我想问是否有人知道创建这些变化的方法,或者最好知道在哪里获取一段代码以在我自己的程序中采用这种颜色变化创建?

我正在使用 C++ 和 QT。

编辑:谢谢您的回复!实际上,给定主页的变化实际上仅以 10% 的步长单独改变 HSL 值。自从我将这些值与程序的颜色选择器中的 HSV 值进行比较后,我感到很困惑。

最佳答案

From what I can tell from considering created variations at this home page, it seems not to be enough to simply change the HSL values seperately to create variations.

真的吗?界面似乎很清楚它所做的修改。您可以选择“色调”、“饱和度”或“亮度”,它会在该 channel 上显示 9 种变化。以下 MATLAB 脚本将以类似的方式绘制不同的变化(尽管是在 HSV 颜色空间,而不是 HSL)。

% display n variations of HTML-style color code.
function [] = colorwheel ( hex, n )
      % parse color code.
    rgb = hex2rgb(hex);
      % render all variations.
    h = figure();
    for j = 1 : 3,
          % build n variations on current channel.
        colors = variantsof(rgb, j, n);
          % display variations.
        for i = 1 : n,
              % generate patch of specified color.
            I = zeros(128, 128, 3);
            I(:,:,1) = colors(i, 1);
            I(:,:,2) = colors(i, 2);
            I(:,:,3) = colors(i, 3);
              % render patches side-by-side to show progression.
            imshow(I, 'parent', ...
                subplot(3, n, (j-1)*n+i, 'parent', h));
        end
    end
end

% parse HTML-style color code.
function [ rgb ] = hex2rgb ( hex )
    r = double(hex2dec(hex(1:2))) / 255;
    g = double(hex2dec(hex(3:4))) / 255;
    b = double(hex2dec(hex(5:6))) / 255;
    rgb = [r g b];
end

% generate n variants of color on j-th channel.
function [ colors ] = variantsof ( rgb, j, n )
    colors = zeros(n, 3);
    for i = 1 : n,
          % convert to HSV.
        color = rgb2hsv(rgb);
          % apply variation to selected channel.
        color(j) = color(j) + ((i-1) / n);
        if color(j) > 1.0,
            color(j) = color(j) - 1.0;
        end
          % convert to RGB.
        colors(i,:) = hsv2rgb(color);
    end
      % order colors with respect to channel.
    if j > 1,
        colors = sortrows(colors, j);
    end
end

使用“goldenrod”示例颜色,如下:

colorwheel('daa520', 9);

我得到:sample output

第一行是色调的变化,第二行是饱和度的变化,第三行是明度的变化。输出并不完全对应 ones on the coloreminder.com ,但这可以通过颜色空间和排列中使用的精确值的差异来解释。

关于c++ - 在 cpp 中创建颜色变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7501620/

相关文章:

java - JEdi​​torKit颜色单个字符没有html

c++ - 组织项目后Qt中.h文件错误没有这样的文件或目录

c++ - 访问子函数,同时使用父类

c++ - 如果作为参数传递,则打开文件,否则打开主应用程序菜单

python - 为什么Numpy的RGB图像阵列具有4层而不是3层?

swift - 节点启动后如何更新SKLabelNode颜色?

c++ - 如何将应用于所有顶点的顶点着色器动态数据传递?

c++ - CL_INVALID_WORK_GROUP_SIZE opencl

c++ - 无法使用 snappy 支持编译 leveldb

c++ - MFC COleControl::DoPropExchange 在哪里存储持久属性?