Clear Filters
Clear Filters

How to visualise the colour with a given set of RGB values?

30 views (last 30 days)
Hi,
I have one set of RGB values
RGB=[85 270 100];
How to represent/visualise the colour in a say, 80 by 80 pixel colour patch?

Accepted Answer

KSSV
KSSV on 23 Jan 2018
x = [0 1 1 0];
y = [0 0 1 1];
patch(x,y,[85 250 100]./255)

More Answers (1)

DGM
DGM on 3 Nov 2021
Edited: DGM on 3 Nov 2021
If you just want a small swatch to see the color:
c = [85 250 100]; % color tuple scaled to [0 255]
imshow(ones(80).*permute(c./255,[1 3 2]));
This should work fine in R2016b and newer.
  2 Comments
Navaneeth
Navaneeth on 7 Mar 2024
Hellow thank you for this. But I am getting an error implementing it in my code, can you kindly let me know how can I integrate it without getting this error.
clc;clear;close all;
%XYZ space to RGB space
XYZ = [1073;796;697];%XYZ value
x = XYZ(1)/sum(XYZ);
y = XYZ(2)/sum(XYZ);
z = XYZ(3)/sum(XYZ);
xyz = [x y z];
RGB_2 = xyz2rgb(xyz,'OutputType','uint8');
imshow(ones(80).*permute(RGB_2
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Error using .*
Integers can only be combined with integers of the same class, or scalar doubles.
./255,[1 3 2]));
Thank you in advance.
DGM
DGM on 7 Mar 2024
There are a couple reasons we need to pay attention to class and scale here. First is that there are restrictions on certain operations with integer class arrays and with mixed-class arrays. Second is that imshow() and other tools expect images to be within a certain scale according to their numeric class.
The problem is that your RGB tuple is proper uint8, whereas the literal c = [85 250 100] that I used in my example is an improperly-scaled double. This causes an error, since we can't do mixed-class multiplication between a uint8 array and a double array. We can get around that problem various ways, but the second problem is that floating point image data is expected to be unit-scale (0-1). When I was dealing with an improperly-scaled double as input, it's necessary to normalize it properly by dividing by 255. If the input is an actual proper uint8 array, doing this normalization is unnecessary and will destroy the image.
So let's back up. The example I gave was based around the common habit of expressing color tuples as improper uint8-scale double arrays. If we're using proper uint8, we don't need to fix the scaling. The multiplication really isn't necessary unless you want to do in-figure composition with some other objects and you want the image to be a certain size. There are simpler ways to do it.
XYZ = [1073;796;697];%XYZ value
x = XYZ(1)/sum(XYZ);
y = XYZ(2)/sum(XYZ);
z = XYZ(3)/sum(XYZ);
xyz = [x y z];
RGB_2 = xyz2rgb(xyz,'OutputType','uint8');
% expand by multiplication, but make sure both have the same class
imshow(ones(80,class(RGB_2)).*permute(RGB_2,[1 3 2]));
% expand using repmat() instead (probably faster)
imshow(repmat(permute(RGB_2,[1 3 2]),[80 80 1]));
% don't really need to do either
% the image fills the axes even if it's only a single pixel
imshow(permute(RGB_2,[1 3 2]))

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!