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

hfyxw5xn  于 7个月前  发布在  Matlab
关注(0)|答案(6)|浏览(111)

在许多MATLAB绘图函数中,可以将颜色指定为字符串或直接列出红色、绿色和蓝色值的3元素向量。
例如,这两个语句是等效的:

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

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

4xrmg8kj

4xrmg8kj1#

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

如果你只关心默认的8个颜色字符串的转换,这里有一个我自己写的函数,我用它来在RGB三元组和短颜色名称(即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进行转换。

ubby3x7f

ubby3x7f2#

我不认为在matlab中有这样的功能。我建议你使用Marcs函数,或者这一行程序。

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

6gpjuf903#

如果没有的话,我就把它黑了出来

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
byqmnocz

byqmnocz4#

这里有一条线,你不必求解C:

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

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

6ju8rftf

6ju8rftf5#

从R2020 b开始,您可以使用validatecolor来实现此功能。

m4pnthwp

m4pnthwp6#

没有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]的字段'Color'
因此,无论使用什么值[0 255]将其除以255来绘制自定义颜色。

相关问题