poster
2019-12-14, 20:13
在许多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向量?
回答:
我在MathWorks File Exchange (http://www.mathworks.com/matlabcentral/fileexchange/)上找到了这种通用替代方法,它甚至可以处理除MATLAB中的默认8以外的颜色字符串:
rgb.m (http://www.mathworks.com/matlabcentral/fileexchange/1805-rgb-m) ,作者Ben Mitch (http://www.mathworks.com/matlabcentral/fileexchange/authors/2765)
如果您只关心默认的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 = 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 请注意,此功能允许您输入字符串[I]或 N×3数字或逻辑数组(RGB值从0到1或0到255),并返回相反的颜色表示。它还使用ISMEMBER (http://www.mathworks.com/help/techdoc/ref/ismember.html)函数进行转换。
更多&回答... (https://stackoverflow.com/questions/4922383)
例如,这两个语句是等效的:
plot(x, y, 'Color', 'r'); plot(x, y, 'Color', [1 0 0]); 可以通过字符串值指定8种颜色: 'r','g','b','c','m','y','k','w' 。是否有MATLAB内置函数将这些字符串转换为等效的RGB向量?
回答:
我在MathWorks File Exchange (http://www.mathworks.com/matlabcentral/fileexchange/)上找到了这种通用替代方法,它甚至可以处理除MATLAB中的默认8以外的颜色字符串:
rgb.m (http://www.mathworks.com/matlabcentral/fileexchange/1805-rgb-m) ,作者Ben Mitch (http://www.mathworks.com/matlabcentral/fileexchange/authors/2765)
如果您只关心默认的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 = 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 请注意,此功能允许您输入字符串[I]或 N×3数字或逻辑数组(RGB值从0到1或0到255),并返回相反的颜色表示。它还使用ISMEMBER (http://www.mathworks.com/help/techdoc/ref/ismember.html)函数进行转换。
更多&回答... (https://stackoverflow.com/questions/4922383)