How to visualise the colour with a given set of RGB values?
    17 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
    Salad Box
      
 le 23 Jan 2018
  
    
    
    
    
    Réponse apportée : Robert Watson
      
 le 18 Jan 2025
            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?
0 commentaires
Réponse acceptée
Plus de réponses (2)
  DGM
      
      
 le 3 Nov 2021
        
      Modifié(e) : DGM
      
      
 le 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 commentaires
  Navaneeth
 le 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
./255,[1 3 2]));
Thank you in advance.
  DGM
      
      
 le 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]))
  Robert Watson
      
 le 18 Jan 2025
        I made a utility function. Figured I should share in case its any use.
It uses imshow to display a preset 128x128 size image of the input color, scaled to take up 1/8th of your screen size.
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 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!











