Hi guys. How can I have display a color from RGB coordinates?
19 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Luigi Treccozzi
am 11 Jul. 2018
Beantwortet: Robert Watson
am 18 Jan. 2025
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 Kommentare
Akzeptierte Antwort
KSSV
am 11 Jul. 2018
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)
Weitere Antworten (1)
Robert Watson
am 18 Jan. 2025
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 Kommentare
Siehe auch
Kategorien
Mehr zu Graphics Object Properties finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


