Hi guys. How can I have display a color from RGB coordinates?
조회 수: 19 (최근 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!


