forked from scottclowe/matlab-schemer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color2javaRGBint.m
131 lines (114 loc) · 4.38 KB
/
color2javaRGBint.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
%COLOR2JAVARGBINT Converts a 256-bit color into a corresponding Java int
% INT = COLOR2JAVARGBINT(HEX) converts the hexadecimal string HEX into a
% negative integer INT which is used by Java as an RGB value equivalent
% to the hexadecimal HEX. HEX should be a triple of 256-bit values,
% possibly prepended by a hash ('#'). Alternatively, HEX can be a triple
% of 16-bit values to provide a 16-bit colour instead.
%
% INT = COLOR2JAVARGBINT(RGB) converts the 3-element RGB colour into INT.
%
% INT = COLOR2JAVARGBINT(R, G, B) converts the triple [R G B] into INT.
%
% This is a helper function which you can use when manually creating your
% own MATLAB colour schemes.
% If you have a set of hexadecimal colours which you want to use,
% you can copy the template colour scheme file, then convert each colour
% using COLOR2JAVARGBINT. The output will be a negative integer, which
% should be placed after the appropriate '...=C-' (with a only a single
% minus sign present).
%
% See also SCHEME_EXPORT, RGBINT2HEX.
% Copyright (c) 2013-2016, Scott C. Lowe <[email protected]>
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
function int = color2javaRGBint(X, varargin)
% Input handling
if nargin==3
if ~isnumeric(X) || ~isnumeric(varargin{1}) || ~isnumeric(varargin{2})
error('With three inputs given, all should be scalars');
end
% Concatenate R,G,B together
X = [X(:); varargin{1}(:); varargin{2}(:)];
elseif nargin~=1
error('Incorrect number of inputs. Should be 1 or 3.');
end
if ischar(X)
% Handle Hex string conversion
int = hex2javaRGBint(X);
elseif isnumeric(X)
% Handle RGB conversion
int = RGB2javaRGBint(X);
else
error('Input was neither string nor numeric');
end
end
function int = hex2javaRGBint(hex)
% Input handling
if length(hex)==7 && strcmp(hex(1),'#')
hex = hex(2:end);
end
if length(hex)==3
% Assume a 16-bit colour (fff, 333, 840, etc)
% This should be replicated to make ffffff, 333333, 884400, etc.
% This is sometimes used on the web as shorthand for the 256-bit
% colours, particularly for the grey shades.
hex = reshape(repmat(hex, 2, 1), 1, 6);
elseif length(hex)~=6
% Length should be 6 for a three channel 256-bit hexadecimal
error('Input string was not a hexadecimal number');
end
% Make a Java color object
jc = java.awt.Color(hex2dec(hex));
% Check what the RGB int is equal to
int = jc.getRGB();
% This is equivalent to
% int = hex2dec(hex) - (256^2*255 + 256*255 + 256);
end
function int = RGB2javaRGBint(X)
% Input handling
if numel(X)==1
% Assume input is greyscale
X = repmat(X, 3, 1);
end
if numel(X)~=3
error('Input did not contain three colour channels');
end
if any(X<0)
error('R,G,B cannot be negative');
end
if any(X>255)
error('R,G,B should be not exceed 255');
end
if all(X<1)
% Rescale from 0-1 to 0-255
X = round(X*255);
end
% Convert R, G, and B into a single integer
dec = 256^2 * X(1) + 256 * X(2) + X(3);
% Make a Java color object
jc = java.awt.Color(dec);
% Check what the RGB int is equal to
int = jc.getRGB();
% This is equivalent to
% int = dec - (256^2*255 + 256*255 + 256);
end