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

17 ビュー (過去 30 日間)
Luigi Treccozzi
Luigi Treccozzi 2018 年 7 月 11 日
回答済み: Robert Watson 2025 年 1 月 18 日
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

採用された回答

KSSV
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 件のコメント
Luigi Treccozzi
Luigi Treccozzi 2018 年 7 月 11 日
Thanks! This is what I was looking for!

サインインしてコメントする。

その他の回答 (1 件)

Robert Watson
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.
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,:))

カテゴリ

Help Center および File ExchangeGraphics Object Properties についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by