Hi guys. How can I have display a color from RGB coordinates?

25 vues (au cours des 30 derniers jours)
Luigi Treccozzi
Luigi Treccozzi le 11 Juil 2018
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

Réponse acceptée

KSSV
KSSV le 11 Juil 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)

Plus de réponses (1)

Robert Watson
Robert Watson le 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.
Figured I should share, so its also on File Exchange.
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,:))

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by