Hi guys. How can I have display a color from RGB coordinates?
18 次查看(过去 30 天)
显示 更早的评论
What I'd like to do is to print a color starting from its RGB coordinates. Like MS Paint tool, more or less. There already exists a tool in Matlab? Thanks
0 个评论
采纳的回答
KSSV
2018-7-11
Red = [255 0 0] ;
Lime = [0 255 0] ;
x = [0 1 1 0] ; y = [0 0 1 1] ;
figure
fill(x,y,Red/255)
figure
fill(x,y,Lime/255)
更多回答(1 个)
Robert Watson
2025-1-18
Its an older thread, but I made a utility function to help me do this more cleanly in future. This will take both normalised floats and 8-bit RGB values, throwing errors on invalid input.
function showColor(rgb)
%SHOWCOLOR Display a colorswatch of the given rgb value
arguments
rgb (1,3) {mustBeNonnegative, mustBeLessThanOrEqual(rgb, 255)} % 1x3 vector of RGB values.
end
% Validate & handle colors in 8-bit format
max_val = max(rgb);
float_tol = 1e-12;
if max_val > 1
% Error if not integer values
is_ints = all(mod(rgb,1) <= float_tol);
assert(is_ints, "Invalid color format supplied. Must be 1x3 vector of floats in range [0,1] or integers in range [0,255]");
% Scale to floats for consistent processing
rgb = rgb./ 255;
end
% Make color swatch
color(1,1,:) = rgb;
color = repmat (color, 128,128,1);
% Display color swatch
% (Want a small figure with minimal whitespace)
f = figure();
tiledlayout(1,1,"TileSpacing","tight","Padding","compact");
nexttile;
imshow(color);
daspect([1 1 1]);
f.Units = "normalized";
f.Position(3:4) = [0.125, 0.125];
end
% Example 1: Basic 8-bit colors
showColor([128,230,45])
% Example 2: Basic Normalized Float Colors
showColor([0.5,0.7,0.8])
% Example 3: Show part of a color map
cmap = lines(3);
showColor(cmap(2,:))
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Properties 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


