如何将颜色名称转换为 3 元素 RGB 向量?

发布于 2024-10-16 08:58:52 字数 268 浏览 6 评论 0原文

在许多 MATLAB 绘图函数中,您可以将颜色指定为字符串或直接列出红色、绿色和蓝色值的三元素向量。

例如,这两个语句是等效的:

plot(x, y, 'Color', 'r');
plot(x, y, 'Color', [1 0 0]);

字符串值可以指定 8 种颜色:'r','g','b','c','m','y',' k','w'。是否有 MATLAB 内置函数可以将这些字符串转换为等效的 RGB 向量?

In many MATLAB plotting functions, you can specify the color as either a string or as a 3 element vector that directly lists the red, green, and blue values.

For instance, these two statements are equivalent:

plot(x, y, 'Color', 'r');
plot(x, y, 'Color', [1 0 0]);

There are 8 colors that can be specified by a string value: 'r','g','b','c','m','y','k','w'. Is there a MATLAB built-in function that converts these strings to an equivalent RGB vector?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无名指的心愿 2024-10-23 08:58:52

我在 MathWorks File Exchange 上找到了这个通用替代方案,它甚至可以处理默认值以外的颜色字符串MATLAB 中的 8:

如果您只关心默认 8 种颜色字符串的转换,这是我自己编写的一个函数我用来在 RGB 三元组和短颜色名称(即单个字符)之间来回转换:

function outColor = convert_color(inColor)

  charValues = 'rgbcmywk'.';  %#'
  rgbValues = [eye(3); 1-eye(3); 1 1 1; 0 0 0];
  assert(~isempty(inColor),'convert_color:badInputSize',...
         'Input argument must not be empty.');

  if ischar(inColor)  %# Input is a character string

    [isColor,colorIndex] = ismember(inColor(:),charValues);
    assert(all(isColor),'convert_color:badInputContents',...
           'String input can only contain the characters ''rgbcmywk''.');
    outColor = rgbValues(colorIndex,:);

  elseif isnumeric(inColor) || islogical(inColor)  %# Input is a numeric or
                                                   %#   logical array
    assert(size(inColor,2) == 3,'convert_color:badInputSize',...
           'Numeric input must be an N-by-3 matrix');
    inColor = double(inColor);           %# Convert input to type double
    scaleIndex = max(inColor,[],2) > 1;  %# Find rows with values > 1
    inColor(scaleIndex,:) = inColor(scaleIndex,:)./255;  %# Scale by 255
    [isColor,colorIndex] = ismember(inColor,rgbValues,'rows');
    assert(all(isColor),'convert_color:badInputContents',...
           'RGB input must define one of the colors ''rgbcmywk''.');
    outColor = charValues(colorIndex(:));

  else  %# Input is an invalid type

    error('convert_color:badInputType',...
          'Input must be a character or numeric array.');

  end

请注意,此函数允许您输入字符串 N×3 数字或逻辑值数组(RGB 值从 0 到 1 或 0 到 255),它返回相反的颜色表示。它还使用函数 ISMEMBER 进行转换。

I found this general alternative on the MathWorks File Exchange which will even handle color strings other than the default 8 in MATLAB:

If you're only concerned with conversions for the default 8 color strings, here's a function I wrote myself that I use to convert back and forth between RGB triples and short color names (i.e. single characters):

function outColor = convert_color(inColor)

  charValues = 'rgbcmywk'.';  %#'
  rgbValues = [eye(3); 1-eye(3); 1 1 1; 0 0 0];
  assert(~isempty(inColor),'convert_color:badInputSize',...
         'Input argument must not be empty.');

  if ischar(inColor)  %# Input is a character string

    [isColor,colorIndex] = ismember(inColor(:),charValues);
    assert(all(isColor),'convert_color:badInputContents',...
           'String input can only contain the characters ''rgbcmywk''.');
    outColor = rgbValues(colorIndex,:);

  elseif isnumeric(inColor) || islogical(inColor)  %# Input is a numeric or
                                                   %#   logical array
    assert(size(inColor,2) == 3,'convert_color:badInputSize',...
           'Numeric input must be an N-by-3 matrix');
    inColor = double(inColor);           %# Convert input to type double
    scaleIndex = max(inColor,[],2) > 1;  %# Find rows with values > 1
    inColor(scaleIndex,:) = inColor(scaleIndex,:)./255;  %# Scale by 255
    [isColor,colorIndex] = ismember(inColor,rgbValues,'rows');
    assert(all(isColor),'convert_color:badInputContents',...
           'RGB input must define one of the colors ''rgbcmywk''.');
    outColor = charValues(colorIndex(:));

  else  %# Input is an invalid type

    error('convert_color:badInputType',...
          'Input must be a character or numeric array.');

  end

Note that this function allows you to input either a string of characters or an N-by-3 numeric or logical array (with RGB values from 0 to 1 or 0 to 255) and it returns the opposite color representation. It also uses the function ISMEMBER to do the conversions.

笑着哭最痛 2024-10-23 08:58:52

我认为matlab中没有这样的函数。我建议你使用 Marcs 函数,或者这个单行代码。

C = rem(floor((strfind('kbgcrmyw', C) - 1) * [0.25 0.5 1]), 2); 

I don't think there is a function for this in matlab. I suggest you use Marcs function, or this one-liner.

C = rem(floor((strfind('kbgcrmyw', C) - 1) * [0.25 0.5 1]), 2); 
行雁书 2024-10-23 08:58:52

万一没有,我就一起黑了一个

function rgbvec = char2rgb (charcolor)
%function rgbvec = char2rgb (charcolor)
%
%converts a character color (one of 'r','g','b','c','m','y','k','w') to a 3
%value RGB vector
%if charcolor is a string (vector of chars), the result is a Nx3 matrix of
%color values, where N is the length of charcolor

if (~exist(charcolor,'var') || ~ischar(charcolor))
    warning('RGB2VEC:NOTC', 'You must pass a character (rgbcmykw)');
    rgbvec = [0 0 0];
    return;
end
rgbvec = zeros(length(charcolor), 3);
charwarning = false;
for j = 1:length(charcolor)
    switch(lower(charcolor(j)))
        case 'r'
            rgbvec(j,:) = [1 0 0];
        case 'g'
            rgbvec(j,:) = [0 1 0];
        case 'b'
            rgbvec(j,:) = [0 0 1];
        case 'c'
            rgbvec(j,:) = [0 1 1];
        case 'm'
            rgbvec(j,:) = [1 0 1];
        case 'y'
            rgbvec(j,:) = [1 1 0];
        case 'w'
            rgbvec(j,:) = [1 1 1];
        case 'k'
            rgbvec(j,:) = [0 0 0];
        otherwise
            charwarning = true;
    end
end

if (charwarning)
    warning('RGB2VEC:BADC', 'Only r,g,b,c,m,y,k,and w are recognized colors');
end

In case there isn't, I just hacked one together

function rgbvec = char2rgb (charcolor)
%function rgbvec = char2rgb (charcolor)
%
%converts a character color (one of 'r','g','b','c','m','y','k','w') to a 3
%value RGB vector
%if charcolor is a string (vector of chars), the result is a Nx3 matrix of
%color values, where N is the length of charcolor

if (~exist(charcolor,'var') || ~ischar(charcolor))
    warning('RGB2VEC:NOTC', 'You must pass a character (rgbcmykw)');
    rgbvec = [0 0 0];
    return;
end
rgbvec = zeros(length(charcolor), 3);
charwarning = false;
for j = 1:length(charcolor)
    switch(lower(charcolor(j)))
        case 'r'
            rgbvec(j,:) = [1 0 0];
        case 'g'
            rgbvec(j,:) = [0 1 0];
        case 'b'
            rgbvec(j,:) = [0 0 1];
        case 'c'
            rgbvec(j,:) = [0 1 1];
        case 'm'
            rgbvec(j,:) = [1 0 1];
        case 'y'
            rgbvec(j,:) = [1 1 0];
        case 'w'
            rgbvec(j,:) = [1 1 1];
        case 'k'
            rgbvec(j,:) = [0 0 0];
        otherwise
            charwarning = true;
    end
end

if (charwarning)
    warning('RGB2VEC:BADC', 'Only r,g,b,c,m,y,k,and w are recognized colors');
end
镜花水月 2024-10-23 08:58:52

从 R2020b 开始,您可以使用 validatecolor这。

Since R2020b you can use validatecolor for this.

指尖上的星空 2024-10-23 08:58:52

这里有一个你不必解决 C 语言的问题:

str2rgb=@(x)get(line('color',x),'color');

现在 str2rgb 给你答案。例如str2rgb('c') = [0 1 1]

here's a oneliner you don't have to solve for C:

str2rgb=@(x)get(line('color',x),'color');

Now str2rgb gives you the answer. for example str2rgb('c') = [0 1 1].

天涯沦落人 2024-10-23 08:58:52

没有 MATLAB 内置函数可以转换 'r','g','b','c','m','y','k','w' 到相应的 RGB 颜色,因为所有这些情况众所周知都是基本颜色

每个混合基本颜色的人都应该随时了解其中的每一种颜色。

在任何情况下,使用以下值构建表格或定义常量可能会很有用:

1.- 主加色

红色 绿色 蓝色

r [255 0 0]
g [0 255 0]
b [0 0 255]

2.- 主减色

青色 洋红色黄色

c [0 255 255]
m [255 0 255]
y [255 255 0]

3.- 黑色:没有任何颜色

k [0 0 0]

4.- 白色:所有颜色

w [255 255 255]

请注意,在 MATLAB 中,命令 plot 需要字段 范围为 [0 1] 的“颜色” 输入。

因此,无论使用什么值[0 255],都将它们除以255来绘制自定义颜色。

There's no MATLAB built-in function to convert 'r','g','b','c','m','y','k','w' to the respective RGB colours because all these cases are well known to be BASIC colours.

Every one blending BASIC colours should know each and all of them, at all times.

In any case it may be useful to build a table or define constants using the following values:

1.- Primary additive colours

red green blue

r [255 0 0]
g [0 255 0]
b [0 0 255]

2.- Primary subtractive colours

cyan magenta yellow

c [0 255 255]
m [255 0 255]
y [255 255 0]

3.- black : absence of any colour

k [0 0 0]

4.- white : all colours

w [255 255 255]

Note that in MATLAB the command plot requires field 'Color' input with range [0 1].

so whatever values [0 255] used divide them by 255 to plot custom colours.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文