简体   繁体   English

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

[英]How can I convert a color name to a 3 element RGB vector?

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. 在许多MATLAB绘图函数中,您可以将颜色指定为字符串或直接列出红色,绿色和蓝色值的3元素向量。

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' . 可以通过字符串值指定8种颜色: 'r','g','b','c','m','y','k','w' Is there a MATLAB built-in function that converts these strings to an equivalent RGB vector? 是否有MATLAB内置函数将这些字符串转换为等效的RGB向量?

I found this general alternative on the MathWorks File Exchange which will even handle color strings other than the default 8 in MATLAB: 我在MathWorks File Exchange上找到了这种通用替代方法,它甚至可以处理除MATLAB中的默认8以外的颜色字符串:

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 (ie single characters): 如果您只关心默认的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

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. 请注意,此功能允许您输入字符串 N×3数字或逻辑数组(RGB值从0到1或0到255),并返回相反的颜色表示。 It also uses the function ISMEMBER to do the conversions. 它还使用ISMEMBER函数进行转换。

I don't think there is a function for this in matlab. 我认为在matlab中没有为此功能。 I suggest you use Marcs function, or this one-liner. 我建议您使用Marcs函数或单行代码。

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

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

here's a oneliner you don't have to solve for C: 这是一个不必为C解决的oneliner:

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

Now str2rgb gives you the answer. 现在, str2rgb给您答案。 for example str2rgb('c') = [0 1 1] . 例如str2rgb('c') = [0 1 1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM