ripta-color-tools 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Changelog +55 -0
- data/Install +18 -0
- data/README.rdoc +82 -0
- data/Rakefile +123 -0
- data/lib/color.rb +78 -0
- data/lib/color/cmyk.rb +185 -0
- data/lib/color/css.rb +27 -0
- data/lib/color/grayscale.rb +135 -0
- data/lib/color/hsl.rb +134 -0
- data/lib/color/palette.rb +18 -0
- data/lib/color/palette/gimp.rb +105 -0
- data/lib/color/palette/monocontrast.rb +178 -0
- data/lib/color/rgb-colors.rb +189 -0
- data/lib/color/rgb.rb +315 -0
- data/lib/color/rgb/metallic.rb +28 -0
- data/lib/color/yiq.rb +80 -0
- data/metaconfig +13 -0
- data/pre-setup.rb +55 -0
- data/setup.rb +1366 -0
- data/tests/test_cmyk.rb +116 -0
- data/tests/test_css.rb +28 -0
- data/tests/test_gimp.rb +79 -0
- data/tests/test_grayscale.rb +87 -0
- data/tests/test_hsl.rb +93 -0
- data/tests/test_monocontrast.rb +145 -0
- data/tests/test_rgb.rb +160 -0
- data/tests/test_yiq.rb +81 -0
- data/tests/testall.rb +20 -0
- metadata +84 -0
@@ -0,0 +1,189 @@
|
|
1
|
+
#--
|
2
|
+
# Colour management with Ruby.
|
3
|
+
#
|
4
|
+
# Copyright 2005 Austin Ziegler
|
5
|
+
# http://rubyforge.org/ruby-pdf/
|
6
|
+
#
|
7
|
+
# Licensed under a MIT-style licence.
|
8
|
+
#
|
9
|
+
# $Id$
|
10
|
+
#++
|
11
|
+
|
12
|
+
class Color::RGB
|
13
|
+
AliceBlue = Color::RGB.new(0xf0, 0xf8, 0xff).freeze
|
14
|
+
AntiqueWhite = Color::RGB.new(0xfa, 0xeb, 0xd7).freeze
|
15
|
+
Aqua = Color::RGB.new(0x00, 0xff, 0xff).freeze
|
16
|
+
Aquamarine = Color::RGB.new(0x7f, 0xff, 0xd4).freeze
|
17
|
+
Azure = Color::RGB.new(0xf0, 0xff, 0xff).freeze
|
18
|
+
Beige = Color::RGB.new(0xf5, 0xf5, 0xdc).freeze
|
19
|
+
Bisque = Color::RGB.new(0xff, 0xe4, 0xc4).freeze
|
20
|
+
Black = Color::RGB.new(0, 0, 0).freeze
|
21
|
+
BlanchedAlmond = Color::RGB.new(0xff, 0xeb, 0xcd).freeze
|
22
|
+
Blue = Color::RGB.new(0x00, 0x00, 0xff).freeze
|
23
|
+
BlueViolet = Color::RGB.new(0x8a, 0x2b, 0xe2).freeze
|
24
|
+
Brown = Color::RGB.new(0xa5, 0x2a, 0x2a).freeze
|
25
|
+
Burlywood = Color::RGB.new(0xde, 0xb8, 0x87).freeze
|
26
|
+
BurlyWood = Burlywood
|
27
|
+
CadetBlue = Color::RGB.new(0x5f, 0x9e, 0xa0).freeze
|
28
|
+
Chartreuse = Color::RGB.new(0x7f, 0xff, 0x00).freeze
|
29
|
+
Chocolate = Color::RGB.new(0xd2, 0x69, 0x1e).freeze
|
30
|
+
Coral = Color::RGB.new(0xff, 0x7f, 0x50).freeze
|
31
|
+
CornflowerBlue = Color::RGB.new(0x64, 0x95, 0xed).freeze
|
32
|
+
Cornsilk = Color::RGB.new(0xff, 0xf8, 0xdc).freeze
|
33
|
+
Crimson = Color::RGB.new(0xdc, 0x14, 0x3c).freeze
|
34
|
+
Cyan = Color::RGB.new(0x00, 0xff, 0xff).freeze
|
35
|
+
DarkBlue = Color::RGB.new(0x00, 0x00, 0x8b).freeze
|
36
|
+
DarkCyan = Color::RGB.new(0x00, 0x8b, 0x8b).freeze
|
37
|
+
DarkGoldenrod = Color::RGB.new(0xb8, 0x86, 0x0b).freeze
|
38
|
+
DarkGoldenRod = DarkGoldenrod
|
39
|
+
DarkGray = Color::RGB.new(0xa9, 0xa9, 0xa9).freeze
|
40
|
+
DarkGreen = Color::RGB.new(0x00, 0x64, 0x00).freeze
|
41
|
+
DarkGrey = DarkGray
|
42
|
+
DarkKhaki = Color::RGB.new(0xbd, 0xb7, 0x6b).freeze
|
43
|
+
DarkMagenta = Color::RGB.new(0x8b, 0x00, 0x8b).freeze
|
44
|
+
DarkoliveGreen = Color::RGB.new(0x55, 0x6b, 0x2f).freeze
|
45
|
+
DarkOliveGreen = DarkoliveGreen
|
46
|
+
Darkorange = Color::RGB.new(0xff, 0x8c, 0x00).freeze
|
47
|
+
DarkOrange = Darkorange
|
48
|
+
DarkOrchid = Color::RGB.new(0x99, 0x32, 0xcc).freeze
|
49
|
+
DarkRed = Color::RGB.new(0x8b, 0x00, 0x00).freeze
|
50
|
+
Darksalmon = Color::RGB.new(0xe9, 0x96, 0x7a).freeze
|
51
|
+
DarkSalmon = Darksalmon
|
52
|
+
DarkSeaGreen = Color::RGB.new(0x8f, 0xbc, 0x8f).freeze
|
53
|
+
DarkSlateBlue = Color::RGB.new(0x48, 0x3d, 0x8b).freeze
|
54
|
+
DarkSlateGray = Color::RGB.new(0x2f, 0x4f, 0x4f).freeze
|
55
|
+
DarkSlateGrey = DarkSlateGray
|
56
|
+
DarkTurquoise = Color::RGB.new(0x00, 0xce, 0xd1).freeze
|
57
|
+
DarkViolet = Color::RGB.new(0x94, 0x00, 0xd3).freeze
|
58
|
+
DeepPink = Color::RGB.new(0xff, 0x14, 0x93).freeze
|
59
|
+
DeepSkyBlue = Color::RGB.new(0x00, 0xbf, 0xbf).freeze
|
60
|
+
DimGray = Color::RGB.new(0x69, 0x69, 0x69).freeze
|
61
|
+
DimGrey = DimGray
|
62
|
+
DodgerBlue = Color::RGB.new(0x1e, 0x90, 0xff).freeze
|
63
|
+
Feldspar = Color::RGB.new(0xd1, 0x92, 0x75).freeze
|
64
|
+
Firebrick = Color::RGB.new(0xb2, 0x22, 0x22).freeze
|
65
|
+
FireBrick = Firebrick
|
66
|
+
FloralWhite = Color::RGB.new(0xff, 0xfa, 0xf0).freeze
|
67
|
+
ForestGreen = Color::RGB.new(0x22, 0x8b, 0x22).freeze
|
68
|
+
Fuchsia = Color::RGB.new(0xff, 0x00, 0xff).freeze
|
69
|
+
Gainsboro = Color::RGB.new(0xdc, 0xdc, 0xdc).freeze
|
70
|
+
GhostWhite = Color::RGB.new(0xf8, 0xf8, 0xff).freeze
|
71
|
+
Gold = Color::RGB.new(0xff, 0xd7, 0x00).freeze
|
72
|
+
Goldenrod = Color::RGB.new(0xda, 0xa5, 0x20).freeze
|
73
|
+
GoldenRod = Goldenrod
|
74
|
+
Gray = Color::RGB.new(0x80, 0x80, 0x80).freeze
|
75
|
+
Green = Color::RGB.new(0x00, 0x80, 0x00).freeze
|
76
|
+
GreenYellow = Color::RGB.new(0xad, 0xff, 0x2f).freeze
|
77
|
+
Grey = Gray
|
78
|
+
Honeydew = Color::RGB.new(0xf0, 0xff, 0xf0).freeze
|
79
|
+
HoneyDew = Honeydew
|
80
|
+
HotPink = Color::RGB.new(0xff, 0x69, 0xb4).freeze
|
81
|
+
IndianRed = Color::RGB.new(0xcd, 0x5c, 0x5c).freeze
|
82
|
+
Indigo = Color::RGB.new(0x4b, 0x00, 0x82).freeze
|
83
|
+
Ivory = Color::RGB.new(0xff, 0xff, 0xf0).freeze
|
84
|
+
Khaki = Color::RGB.new(0xf0, 0xe6, 0x8c).freeze
|
85
|
+
Lavender = Color::RGB.new(0xe6, 0xe6, 0xfa).freeze
|
86
|
+
LavenderBlush = Color::RGB.new(0xff, 0xf0, 0xf5).freeze
|
87
|
+
LawnGreen = Color::RGB.new(0x7c, 0xfc, 0x00).freeze
|
88
|
+
LemonChiffon = Color::RGB.new(0xff, 0xfa, 0xcd).freeze
|
89
|
+
LightBlue = Color::RGB.new(0xad, 0xd8, 0xe6).freeze
|
90
|
+
LightCoral = Color::RGB.new(0xf0, 0x80, 0x80).freeze
|
91
|
+
LightCyan = Color::RGB.new(0xe0, 0xff, 0xff).freeze
|
92
|
+
LightGoldenrodYellow = Color::RGB.new(0xfa, 0xfa, 0xd2).freeze
|
93
|
+
LightGoldenRodYellow = LightGoldenrodYellow
|
94
|
+
LightGray = Color::RGB.new(0xd3, 0xd3, 0xd3).freeze
|
95
|
+
LightGreen = Color::RGB.new(0x90, 0xee, 0x90).freeze
|
96
|
+
LightGrey = LightGray
|
97
|
+
LightPink = Color::RGB.new(0xff, 0xb6, 0xc1).freeze
|
98
|
+
Lightsalmon = Color::RGB.new(0xff, 0xa0, 0x7a).freeze
|
99
|
+
LightSalmon = Lightsalmon
|
100
|
+
LightSeaGreen = Color::RGB.new(0x20, 0xb2, 0xaa).freeze
|
101
|
+
LightSkyBlue = Color::RGB.new(0x87, 0xce, 0xfa).freeze
|
102
|
+
LightSlateBlue = Color::RGB.new(0x84, 0x70, 0xff).freeze
|
103
|
+
LightSlateGray = Color::RGB.new(0x77, 0x88, 0x99).freeze
|
104
|
+
LightSlateGrey = LightSlateGray
|
105
|
+
LightsteelBlue = Color::RGB.new(0xb0, 0xc4, 0xde).freeze
|
106
|
+
LightSteelBlue = LightsteelBlue
|
107
|
+
LightYellow = Color::RGB.new(0xff, 0xff, 0xe0).freeze
|
108
|
+
Lime = Color::RGB.new(0x00, 0xff, 0x00).freeze
|
109
|
+
LimeGreen = Color::RGB.new(0x32, 0xcd, 0x32).freeze
|
110
|
+
Linen = Color::RGB.new(0xfa, 0xf0, 0xe6).freeze
|
111
|
+
Magenta = Color::RGB.new(0xff, 0x00, 0xff).freeze
|
112
|
+
Maroon = Color::RGB.new(0x80, 0x00, 0x00).freeze
|
113
|
+
MediumAquamarine = Color::RGB.new(0x66, 0xcd, 0xaa).freeze
|
114
|
+
MediumAquaMarine = MediumAquamarine
|
115
|
+
MediumBlue = Color::RGB.new(0x00, 0x00, 0xcd).freeze
|
116
|
+
MediumOrchid = Color::RGB.new(0xba, 0x55, 0xd3).freeze
|
117
|
+
MediumPurple = Color::RGB.new(0x93, 0x70, 0xdb).freeze
|
118
|
+
MediumSeaGreen = Color::RGB.new(0x3c, 0xb3, 0x71).freeze
|
119
|
+
MediumSlateBlue = Color::RGB.new(0x7b, 0x68, 0xee).freeze
|
120
|
+
MediumSpringGreen = Color::RGB.new(0x00, 0xfa, 0x9a).freeze
|
121
|
+
MediumTurquoise = Color::RGB.new(0x48, 0xd1, 0xcc).freeze
|
122
|
+
MediumVioletRed = Color::RGB.new(0xc7, 0x15, 0x85).freeze
|
123
|
+
MidnightBlue = Color::RGB.new(0x19, 0x19, 0x70).freeze
|
124
|
+
MintCream = Color::RGB.new(0xf5, 0xff, 0xfa).freeze
|
125
|
+
MistyRose = Color::RGB.new(0xff, 0xe4, 0xe1).freeze
|
126
|
+
Moccasin = Color::RGB.new(0xff, 0xe4, 0xb5).freeze
|
127
|
+
NavajoWhite = Color::RGB.new(0xff, 0xde, 0xad).freeze
|
128
|
+
Navy = Color::RGB.new(0x00, 0x00, 0x80).freeze
|
129
|
+
OldLace = Color::RGB.new(0xfd, 0xf5, 0xe6).freeze
|
130
|
+
Olive = Color::RGB.new(0x80, 0x80, 0x00).freeze
|
131
|
+
Olivedrab = Color::RGB.new(0x6b, 0x8e, 0x23).freeze
|
132
|
+
OliveDrab = Olivedrab
|
133
|
+
Orange = Color::RGB.new(0xff, 0xa5, 0x00).freeze
|
134
|
+
OrangeRed = Color::RGB.new(0xff, 0x45, 0x00).freeze
|
135
|
+
Orchid = Color::RGB.new(0xda, 0x70, 0xd6).freeze
|
136
|
+
PaleGoldenrod = Color::RGB.new(0xee, 0xe8, 0xaa).freeze
|
137
|
+
PaleGoldenRod = PaleGoldenrod
|
138
|
+
PaleGreen = Color::RGB.new(0x98, 0xfb, 0x98).freeze
|
139
|
+
PaleTurquoise = Color::RGB.new(0xaf, 0xee, 0xee).freeze
|
140
|
+
PaleVioletRed = Color::RGB.new(0xdb, 0x70, 0x93).freeze
|
141
|
+
PapayaWhip = Color::RGB.new(0xff, 0xef, 0xd5).freeze
|
142
|
+
Peachpuff = Color::RGB.new(0xff, 0xda, 0xb9).freeze
|
143
|
+
PeachPuff = Peachpuff
|
144
|
+
Peru = Color::RGB.new(0xcd, 0x85, 0x3f).freeze
|
145
|
+
Pink = Color::RGB.new(0xff, 0xc0, 0xcb).freeze
|
146
|
+
Plum = Color::RGB.new(0xdd, 0xa0, 0xdd).freeze
|
147
|
+
PowderBlue = Color::RGB.new(0xb0, 0xe0, 0xe6).freeze
|
148
|
+
Purple = Color::RGB.new(0x80, 0x00, 0x80).freeze
|
149
|
+
Red = Color::RGB.new(0xff, 0x00, 0x00).freeze
|
150
|
+
RosyBrown = Color::RGB.new(0xbc, 0x8f, 0x8f).freeze
|
151
|
+
RoyalBlue = Color::RGB.new(0x41, 0x69, 0xe1).freeze
|
152
|
+
SaddleBrown = Color::RGB.new(0x8b, 0x45, 0x13).freeze
|
153
|
+
Salmon = Color::RGB.new(0xfa, 0x80, 0x72).freeze
|
154
|
+
SandyBrown = Color::RGB.new(0xf4, 0xa4, 0x60).freeze
|
155
|
+
SeaGreen = Color::RGB.new(0x2e, 0x8b, 0x57).freeze
|
156
|
+
Seashell = Color::RGB.new(0xff, 0xf5, 0xee).freeze
|
157
|
+
SeaShell = Seashell
|
158
|
+
Sienna = Color::RGB.new(0xa0, 0x52, 0x2d).freeze
|
159
|
+
Silver = Color::RGB.new(0xc0, 0xc0, 0xc0).freeze
|
160
|
+
SkyBlue = Color::RGB.new(0x87, 0xce, 0xeb).freeze
|
161
|
+
SlateBlue = Color::RGB.new(0x6a, 0x5a, 0xcd).freeze
|
162
|
+
SlateGray = Color::RGB.new(0x70, 0x80, 0x90).freeze
|
163
|
+
SlateGrey = SlateGray
|
164
|
+
Snow = Color::RGB.new(0xff, 0xfa, 0xfa).freeze
|
165
|
+
SpringGreen = Color::RGB.new(0x00, 0xff, 0x7f).freeze
|
166
|
+
SteelBlue = Color::RGB.new(0x46, 0x82, 0xb4).freeze
|
167
|
+
Tan = Color::RGB.new(0xd2, 0xb4, 0x8c).freeze
|
168
|
+
Teal = Color::RGB.new(0x00, 0x80, 0x80).freeze
|
169
|
+
Thistle = Color::RGB.new(0xd8, 0xbf, 0xd8).freeze
|
170
|
+
Tomato = Color::RGB.new(0xff, 0x63, 0x47).freeze
|
171
|
+
Turquoise = Color::RGB.new(0x40, 0xe0, 0xd0).freeze
|
172
|
+
Violet = Color::RGB.new(0xee, 0x82, 0xee).freeze
|
173
|
+
VioletRed = Color::RGB.new(0xd0, 0x20, 0x90).freeze
|
174
|
+
Wheat = Color::RGB.new(0xf5, 0xde, 0xb3).freeze
|
175
|
+
White = Color::RGB.new(0xff, 0xff, 0xff).freeze
|
176
|
+
WhiteSmoke = Color::RGB.new(0xf5, 0xf5, 0xf5).freeze
|
177
|
+
Yellow = Color::RGB.new(0xff, 0xff, 0x00).freeze
|
178
|
+
YellowGreen = Color::RGB.new(0x9a, 0xcd, 0x32).freeze
|
179
|
+
|
180
|
+
Gray10 = Grey10 = Color::RGB.from_percentage(10, 10, 10).freeze
|
181
|
+
Gray20 = Grey20 = Color::RGB.from_percentage(20, 20, 20).freeze
|
182
|
+
Gray30 = Grey30 = Color::RGB.from_percentage(30, 30, 30).freeze
|
183
|
+
Gray40 = Grey40 = Color::RGB.from_percentage(40, 40, 40).freeze
|
184
|
+
Gray50 = Grey50 = Color::RGB.from_percentage(50, 50, 50).freeze
|
185
|
+
Gray60 = Grey60 = Color::RGB.from_percentage(60, 60, 60).freeze
|
186
|
+
Gray70 = Grey70 = Color::RGB.from_percentage(70, 70, 70).freeze
|
187
|
+
Gray80 = Grey80 = Color::RGB.from_percentage(80, 80, 80).freeze
|
188
|
+
Gray90 = Grey90 = Color::RGB.from_percentage(90, 90, 90).freeze
|
189
|
+
end
|
data/lib/color/rgb.rb
ADDED
@@ -0,0 +1,315 @@
|
|
1
|
+
#--
|
2
|
+
# Colour management with Ruby.
|
3
|
+
#
|
4
|
+
# Copyright 2005 Austin Ziegler
|
5
|
+
# http://rubyforge.org/ruby-pdf/
|
6
|
+
#
|
7
|
+
# Licensed under a MIT-style licence.
|
8
|
+
#
|
9
|
+
# $Id$
|
10
|
+
#++
|
11
|
+
|
12
|
+
# An RGB colour object.
|
13
|
+
class Color::RGB
|
14
|
+
|
15
|
+
autoload :Metallic, "color/rgb/metallic"
|
16
|
+
|
17
|
+
# The format of a DeviceRGB colour for PDF. In color-tools 2.0 this will
|
18
|
+
# be removed from this package and added back as a modification by the
|
19
|
+
# PDF::Writer package.
|
20
|
+
PDF_FORMAT_STR = "%.3f %.3f %.3f %s"
|
21
|
+
|
22
|
+
class << self
|
23
|
+
# Creates an RGB colour object from percentages 0..100.
|
24
|
+
#
|
25
|
+
# Color::RGB.from_percentage(10, 20 30)
|
26
|
+
def from_percentage(r = 0, g = 0, b = 0)
|
27
|
+
from_fraction(r / 100.0, g / 100.0, b / 100.0)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Creates an RGB colour object from fractional values 0..1.
|
31
|
+
#
|
32
|
+
# Color::RGB.from_fraction(.3, .2, .1)
|
33
|
+
def from_fraction(r = 0.0, g = 0.0, b = 0.0)
|
34
|
+
colour = Color::RGB.new
|
35
|
+
colour.r = r
|
36
|
+
colour.g = g
|
37
|
+
colour.b = b
|
38
|
+
colour
|
39
|
+
end
|
40
|
+
|
41
|
+
# Creates an RGB colour object from an HTML colour descriptor (e.g.,
|
42
|
+
# <tt>"fed"</tt> or <tt>"#cabbed;"</tt>.
|
43
|
+
#
|
44
|
+
# Color::RGB.from_html("fed")
|
45
|
+
# Color::RGB.from_html("#fed")
|
46
|
+
# Color::RGB.from_html("#cabbed")
|
47
|
+
# Color::RGB.from_html("cabbed")
|
48
|
+
def from_html(html_colour)
|
49
|
+
html_colour = html_colour.gsub(%r{[#;]}, '')
|
50
|
+
case html_colour.size
|
51
|
+
when 3
|
52
|
+
colours = html_colour.scan(%r{[0-9A-Fa-f]}).map { |el| (el * 2).to_i(16) }
|
53
|
+
when 6
|
54
|
+
colours = html_colour.scan(%r<[0-9A-Fa-f]{2}>).map { |el| el.to_i(16) }
|
55
|
+
else
|
56
|
+
raise ArgumentError
|
57
|
+
end
|
58
|
+
Color::RGB.new(*colours)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Compares the other colour to this one. The other colour will be
|
63
|
+
# converted to RGB before comparison, so the comparison between a RGB
|
64
|
+
# colour and a non-RGB colour will be approximate and based on the other
|
65
|
+
# colour's default #to_rgb conversion. If there is no #to_rgb
|
66
|
+
# conversion, this will raise an exception. This will report that two
|
67
|
+
# RGB colours are equivalent if all component values are within 1e-4
|
68
|
+
# (0.0001) of each other.
|
69
|
+
def ==(other)
|
70
|
+
other = other.to_rgb
|
71
|
+
other.kind_of?(Color::RGB) and
|
72
|
+
((@r - other.r).abs <= 1e-4) and
|
73
|
+
((@g - other.g).abs <= 1e-4) and
|
74
|
+
((@b - other.b).abs <= 1e-4)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Creates an RGB colour object from the standard range 0..255.
|
78
|
+
#
|
79
|
+
# Color::RGB.new(32, 64, 128)
|
80
|
+
# Color::RGB.new(0x20, 0x40, 0x80)
|
81
|
+
def initialize(r = 0, g = 0, b = 0)
|
82
|
+
@r = r / 255.0
|
83
|
+
@g = g / 255.0
|
84
|
+
@b = b / 255.0
|
85
|
+
end
|
86
|
+
|
87
|
+
# Present the colour as a DeviceRGB fill colour string for PDF. This
|
88
|
+
# will be removed from the default package in color-tools 2.0.
|
89
|
+
def pdf_fill
|
90
|
+
PDF_FORMAT_STR % [ @r, @g, @b, "rg" ]
|
91
|
+
end
|
92
|
+
|
93
|
+
# Present the colour as a DeviceRGB stroke colour string for PDF. This
|
94
|
+
# will be removed from the default package in color-tools 2.0.
|
95
|
+
def pdf_stroke
|
96
|
+
PDF_FORMAT_STR % [ @r, @g, @b, "RG" ]
|
97
|
+
end
|
98
|
+
|
99
|
+
# Present the colour as an HTML/CSS colour string.
|
100
|
+
def html
|
101
|
+
r = (@r * 255).round
|
102
|
+
r = 255 if r > 255
|
103
|
+
|
104
|
+
g = (@g * 255).round
|
105
|
+
g = 255 if g > 255
|
106
|
+
|
107
|
+
b = (@b * 255).round
|
108
|
+
b = 255 if b > 255
|
109
|
+
|
110
|
+
"#%02x%02x%02x" % [ r, g, b ]
|
111
|
+
end
|
112
|
+
|
113
|
+
# Converts the RGB colour to CMYK. Most colour experts strongly suggest
|
114
|
+
# that this is not a good idea (some even suggesting that it's a very
|
115
|
+
# bad idea). CMYK represents additive percentages of inks on white
|
116
|
+
# paper, whereas RGB represents mixed colour intensities on a black
|
117
|
+
# screen.
|
118
|
+
#
|
119
|
+
# However, the colour conversion can be done. The basic method is
|
120
|
+
# multi-step:
|
121
|
+
#
|
122
|
+
# 1. Convert the R, G, and B components to C, M, and Y components.
|
123
|
+
# c = 1.0 � r
|
124
|
+
# m = 1.0 � g
|
125
|
+
# y = 1.0 � b
|
126
|
+
# 2. Compute the minimum amount of black (K) required to smooth the
|
127
|
+
# colour in inks.
|
128
|
+
# k = min(c, m, y)
|
129
|
+
# 3. Perform undercolour removal on the C, M, and Y components of the
|
130
|
+
# colours because less of each colour is needed for each bit of
|
131
|
+
# black. Also, regenerate the black (K) based on the undercolour
|
132
|
+
# removal so that the colour is more accurately represented in ink.
|
133
|
+
# c = min(1.0, max(0.0, c � UCR(k)))
|
134
|
+
# m = min(1.0, max(0.0, m � UCR(k)))
|
135
|
+
# y = min(1.0, max(0.0, y � UCR(k)))
|
136
|
+
# k = min(1.0, max(0.0, BG(k)))
|
137
|
+
#
|
138
|
+
# The undercolour removal function and the black generation functions
|
139
|
+
# return a value based on the brightness of the RGB colour.
|
140
|
+
def to_cmyk
|
141
|
+
c = 1.0 - @r.to_f
|
142
|
+
m = 1.0 - @g.to_f
|
143
|
+
y = 1.0 - @b.to_f
|
144
|
+
|
145
|
+
k = [c, m, y].min
|
146
|
+
k = k - (k * brightness)
|
147
|
+
|
148
|
+
c = [1.0, [0.0, c - k].max].min
|
149
|
+
m = [1.0, [0.0, m - k].max].min
|
150
|
+
y = [1.0, [0.0, y - k].max].min
|
151
|
+
k = [1.0, [0.0, k].max].min
|
152
|
+
|
153
|
+
Color::CMYK.from_fraction(c, m, y, k)
|
154
|
+
end
|
155
|
+
|
156
|
+
def to_rgb(ignored = nil)
|
157
|
+
self
|
158
|
+
end
|
159
|
+
|
160
|
+
# Returns the YIQ (NTSC) colour encoding of the RGB value.
|
161
|
+
def to_yiq
|
162
|
+
y = (@r * 0.299) + (@g * 0.587) + (@b * 0.114)
|
163
|
+
i = (@r * 0.596) + (@g * -0.275) + (@b * -0.321)
|
164
|
+
q = (@r * 0.212) + (@g * -0.523) + (@b * 0.311)
|
165
|
+
Color::YIQ.from_fraction(y, i, q)
|
166
|
+
end
|
167
|
+
|
168
|
+
# Returns the HSL colour encoding of the RGB value.
|
169
|
+
def to_hsl
|
170
|
+
min = [ @r, @g, @b ].min
|
171
|
+
max = [ @r, @g, @b ].max
|
172
|
+
delta = (max - min).to_f
|
173
|
+
|
174
|
+
lum = (max + min) / 2.0
|
175
|
+
|
176
|
+
if delta <= 1e-5 # close to 0.0, so it's a grey
|
177
|
+
hue = 0
|
178
|
+
sat = 0
|
179
|
+
else
|
180
|
+
if (lum - 0.5) <= 1e-5
|
181
|
+
sat = delta / (max + min).to_f
|
182
|
+
else
|
183
|
+
sat = delta / (2 - max - min).to_f
|
184
|
+
end
|
185
|
+
|
186
|
+
if @r == max
|
187
|
+
hue = (@g - @b) / delta.to_f
|
188
|
+
elsif @g == max
|
189
|
+
hue = (2.0 + @b - @r) / delta.to_f
|
190
|
+
elsif (@b - max) <= 1e-5
|
191
|
+
hue = (4.0 + @r - @g) / delta.to_f
|
192
|
+
end
|
193
|
+
hue /= 6.0
|
194
|
+
|
195
|
+
hue += 1 if hue < 0
|
196
|
+
hue -= 1 if hue > 1
|
197
|
+
end
|
198
|
+
Color::HSL.from_fraction(hue, sat, lum)
|
199
|
+
end
|
200
|
+
|
201
|
+
# Mix the RGB hue with White so that the RGB hue is the specified
|
202
|
+
# percentage of the resulting colour. Strictly speaking, this isn't a
|
203
|
+
# darken_by operation.
|
204
|
+
def lighten_by(percent)
|
205
|
+
mix_with(White, percent)
|
206
|
+
end
|
207
|
+
|
208
|
+
# Mix the RGB hue with Black so that the RGB hue is the specified
|
209
|
+
# percentage of the resulting colour. Strictly speaking, this isn't a
|
210
|
+
# darken_by operation.
|
211
|
+
def darken_by(percent)
|
212
|
+
mix_with(Black, percent)
|
213
|
+
end
|
214
|
+
|
215
|
+
# Mix the mask colour (which must be an RGB object) with the current
|
216
|
+
# colour at the stated opacity percentage (0..100).
|
217
|
+
def mix_with(mask, opacity)
|
218
|
+
opacity /= 100.0
|
219
|
+
rgb = self.dup
|
220
|
+
|
221
|
+
rgb.r = (@r * opacity) + (mask.r * (1 - opacity))
|
222
|
+
rgb.g = (@g * opacity) + (mask.g * (1 - opacity))
|
223
|
+
rgb.b = (@b * opacity) + (mask.b * (1 - opacity))
|
224
|
+
|
225
|
+
rgb
|
226
|
+
end
|
227
|
+
|
228
|
+
# Returns the brightness value for a colour, a number between 0..1.
|
229
|
+
# Based on the Y value of YIQ encoding, representing luminosity, or
|
230
|
+
# perceived brightness.
|
231
|
+
#
|
232
|
+
# This may be modified in a future version of color-tools to use the
|
233
|
+
# luminosity value of HSL.
|
234
|
+
def brightness
|
235
|
+
to_yiq.y
|
236
|
+
end
|
237
|
+
def to_grayscale
|
238
|
+
Color::GrayScale.from_fraction(to_hsl.l)
|
239
|
+
end
|
240
|
+
|
241
|
+
alias to_greyscale to_grayscale
|
242
|
+
|
243
|
+
# Returns a new colour with the brightness adjusted by the specified
|
244
|
+
# percentage. Negative percentages will darken the colour; positive
|
245
|
+
# percentages will brighten the colour.
|
246
|
+
#
|
247
|
+
# Color::RGB::DarkBlue.adjust_brightness(10)
|
248
|
+
# Color::RGB::DarkBlue.adjust_brightness(-10)
|
249
|
+
def adjust_brightness(percent)
|
250
|
+
percent /= 100.0
|
251
|
+
percent += 1.0
|
252
|
+
percent = [ percent, 2.0 ].min
|
253
|
+
percent = [ 0.0, percent ].max
|
254
|
+
|
255
|
+
hsl = to_hsl
|
256
|
+
hsl.l *= percent
|
257
|
+
hsl.to_rgb
|
258
|
+
end
|
259
|
+
|
260
|
+
# Returns a new colour with the saturation adjusted by the specified
|
261
|
+
# percentage. Negative percentages will reduce the saturation; positive
|
262
|
+
# percentages will increase the saturation.
|
263
|
+
#
|
264
|
+
# Color::RGB::DarkBlue.adjust_saturation(10)
|
265
|
+
# Color::RGB::DarkBlue.adjust_saturation(-10)
|
266
|
+
def adjust_saturation(percent)
|
267
|
+
percent /= 100.0
|
268
|
+
percent += 1.0
|
269
|
+
percent = [ percent, 2.0 ].min
|
270
|
+
percent = [ 0.0, percent ].max
|
271
|
+
|
272
|
+
hsl = to_hsl
|
273
|
+
hsl.s *= percent
|
274
|
+
hsl.to_rgb
|
275
|
+
end
|
276
|
+
|
277
|
+
# Returns a new colour with the hue adjusted by the specified
|
278
|
+
# percentage. Negative percentages will reduce the hue; positive
|
279
|
+
# percentages will increase the hue.
|
280
|
+
#
|
281
|
+
# Color::RGB::DarkBlue.adjust_hue(10)
|
282
|
+
# Color::RGB::DarkBlue.adjust_hue(-10)
|
283
|
+
def adjust_hue(percent)
|
284
|
+
percent /= 100.0
|
285
|
+
percent += 1.0
|
286
|
+
percent = [ percent, 2.0 ].min
|
287
|
+
percent = [ 0.0, percent ].max
|
288
|
+
|
289
|
+
hsl = to_hsl
|
290
|
+
hsl.h *= percent
|
291
|
+
hsl.to_rgb
|
292
|
+
end
|
293
|
+
|
294
|
+
attr_reader :r, :g, :b
|
295
|
+
|
296
|
+
def r=(rr) #:nodoc:
|
297
|
+
rr = 1.0 if rr > 1
|
298
|
+
rr = 0.0 if rr < 0
|
299
|
+
@r = rr
|
300
|
+
end
|
301
|
+
|
302
|
+
def g=(gg) #:nodoc:
|
303
|
+
gg = 1.0 if gg > 1
|
304
|
+
gg = 0.0 if gg < 0
|
305
|
+
@g = gg
|
306
|
+
end
|
307
|
+
|
308
|
+
def b=(bb) #:nodoc:
|
309
|
+
bb = 1.0 if bb > 1
|
310
|
+
bb = 0.0 if bb < 0
|
311
|
+
@b = bb
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
require 'color/rgb-colors'
|