-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgb2hex.m
64 lines (56 loc) · 1.77 KB
/
rgb2hex.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function [ hex ] = rgb2hex(rgb)
% rgb2hex converts rgb color values to hex color format.
%
% This function assumes rgb values are in [r g b] format on the 0 to 1
% scale. If, however, any value r, g, or b exceed 1, the function assumes
% [r g b] are scaled between 0 and 255.
%
% * * * * * * * * * * * * * * * * * * * *
% SYNTAX:
% hex = rgb2hex(rgb) returns the hexadecimal color value of the n x 3 rgb
% values. rgb can be an array.
%
% * * * * * * * * * * * * * * * * * * * *
% EXAMPLES:
%
% myhexvalue = rgb2hex([0 1 0])
% = #00FF00
%
% myhexvalue = rgb2hex([0 255 0])
% = #00FF00
%
% myrgbvalues = [.2 .3 .4;
% .5 .6 .7;
% .8 .6 .2;
% .2 .2 .9];
% myhexvalues = rgb2hex(myrgbvalues)
% = #334D66
% #8099B3
% #CC9933
% #3333E6
%
% * * * * * * * * * * * * * * * * * * * *
% Chad A. Greene, April 2014
%
% Updated August 2014: Functionality remains exactly the same, but it's a
% little more efficient and more robust. Thanks to Stephen Cobeldick for
% his suggestions.
%
% * * * * * * * * * * * * * * * * * * * *
% See also hex2rgb, dec2hex, hex2num, and ColorSpec.
%% Check inputs:
assert(nargin==1,'This function requires an RGB input.')
assert(isnumeric(rgb)==1,'Function input must be numeric.')
sizergb = size(rgb);
assert(sizergb(2)==3,'rgb value must have three components in the form [r g b].')
assert(max(rgb(:))<=255& min(rgb(:))>=0,'rgb values must be on a scale of 0 to 1 or 0 to 255')
%% If no value in RGB exceeds unity, scale from 0 to 255:
if max(rgb(:))<=1
rgb = round(rgb*255);
else
rgb = round(rgb);
end
%% Convert (Thanks to Stephen Cobeldick for this clever, efficient solution):
hex(:,2:7) = reshape(sprintf('%02X',rgb.'),6,[]).';
hex(:,1) = '#';
end