color-tools 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Changelog +35 -1
- data/Install +11 -2
- data/README +44 -12
- data/Rakefile +11 -2
- data/lib/color.rb +35 -158
- data/lib/color/cmyk.rb +62 -18
- data/lib/color/css.rb +27 -0
- data/lib/color/grayscale.rb +43 -13
- data/lib/color/hsl.rb +130 -0
- data/lib/color/palette.rb +4 -0
- data/lib/color/palette/gimp.rb +47 -12
- data/lib/color/palette/monocontrast.rb +41 -11
- data/lib/color/rgb-colors.rb +189 -0
- data/lib/color/rgb.rb +163 -27
- data/lib/color/rgb/metallic.rb +28 -0
- data/lib/color/yiq.rb +35 -9
- data/metaconfig +11 -2
- data/pre-setup.rb +12 -3
- 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 +23 -8
@@ -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: rgb-colors.rb,v 1.1 2005/08/05 23:07:20 austin Exp $
|
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
CHANGED
@@ -3,20 +3,30 @@
|
|
3
3
|
#
|
4
4
|
# Copyright 2005 Austin Ziegler
|
5
5
|
# http://rubyforge.org/ruby-pdf/
|
6
|
+
#
|
7
|
+
# Licensed under a MIT-style licence.
|
8
|
+
#
|
9
|
+
# $Id: rgb.rb,v 1.6 2005/08/08 02:44:17 austin Exp $
|
6
10
|
#++
|
7
11
|
|
8
12
|
# An RGB colour object.
|
9
13
|
class Color::RGB
|
10
|
-
# The format
|
14
|
+
# The format of a DeviceRGB colour for PDF. In color-tools 2.0 this will
|
15
|
+
# be removed from this package and added back as a modification by the
|
16
|
+
# PDF::Writer package.
|
11
17
|
PDF_FORMAT_STR = "%.3f %.3f %.3f %s"
|
12
18
|
|
13
19
|
class << self
|
14
|
-
# Creates an RGB colour object from percentages 0
|
20
|
+
# Creates an RGB colour object from percentages 0..100.
|
21
|
+
#
|
22
|
+
# Color::RGB.from_percentage(10, 20 30)
|
15
23
|
def from_percentage(r = 0, g = 0, b = 0)
|
16
24
|
from_fraction(r / 100.0, g / 100.0, b / 100.0)
|
17
25
|
end
|
18
26
|
|
19
|
-
# Creates an RGB colour object from fractional values 0
|
27
|
+
# Creates an RGB colour object from fractional values 0..1.
|
28
|
+
#
|
29
|
+
# Color::RGB.from_fraction(.3, .2, .1)
|
20
30
|
def from_fraction(r = 0.0, g = 0.0, b = 0.0)
|
21
31
|
colour = Color::RGB.new
|
22
32
|
colour.r = r
|
@@ -27,6 +37,11 @@ class Color::RGB
|
|
27
37
|
|
28
38
|
# Creates an RGB colour object from an HTML colour descriptor (e.g.,
|
29
39
|
# <tt>"fed"</tt> or <tt>"#cabbed;"</tt>.
|
40
|
+
#
|
41
|
+
# Color::RGB.from_html("fed")
|
42
|
+
# Color::RGB.from_html("#fed")
|
43
|
+
# Color::RGB.from_html("#cabbed")
|
44
|
+
# Color::RGB.from_html("cabbed")
|
30
45
|
def from_html(html_colour)
|
31
46
|
html_colour = html_colour.gsub(%r{[#;]}, '')
|
32
47
|
case html_colour.size
|
@@ -42,27 +57,39 @@ class Color::RGB
|
|
42
57
|
end
|
43
58
|
end
|
44
59
|
|
60
|
+
# Compares the other colour to this one. The other colour will be
|
61
|
+
# converted to RGB before comparison, so the comparison between a RGB
|
62
|
+
# colour and a non-RGB colour will be approximate and based on the other
|
63
|
+
# colour's default #to_rgb conversion. If there is no #to_rgb
|
64
|
+
# conversion, this will raise an exception. This will report that two
|
65
|
+
# RGB colours are equivalent if all component values are within 1e-4
|
66
|
+
# (0.0001) of each other.
|
45
67
|
def ==(other)
|
46
68
|
other = other.to_rgb
|
47
69
|
other.kind_of?(Color::RGB) and
|
48
|
-
(@r
|
49
|
-
(@g
|
50
|
-
(@b
|
70
|
+
((@r - other.r).abs <= 1e-4) and
|
71
|
+
((@g - other.g).abs <= 1e-4) and
|
72
|
+
((@b - other.b).abs <= 1e-4)
|
51
73
|
end
|
52
74
|
|
53
|
-
# Creates an RGB colour object from the standard range 0
|
75
|
+
# Creates an RGB colour object from the standard range 0..255.
|
76
|
+
#
|
77
|
+
# Color::RGB.new(32, 64, 128)
|
78
|
+
# Color::RGB.new(0x20, 0x40, 0x80)
|
54
79
|
def initialize(r = 0, g = 0, b = 0)
|
55
80
|
@r = r / 255.0
|
56
81
|
@g = g / 255.0
|
57
82
|
@b = b / 255.0
|
58
83
|
end
|
59
84
|
|
60
|
-
# Present the colour as a fill colour string for PDF.
|
85
|
+
# Present the colour as a DeviceRGB fill colour string for PDF. This
|
86
|
+
# will be removed from the default package in color-tools 2.0.
|
61
87
|
def pdf_fill
|
62
88
|
PDF_FORMAT_STR % [ @r, @g, @b, "rg" ]
|
63
89
|
end
|
64
90
|
|
65
|
-
# Present the colour as a stroke colour string for PDF.
|
91
|
+
# Present the colour as a DeviceRGB stroke colour string for PDF. This
|
92
|
+
# will be removed from the default package in color-tools 2.0.
|
66
93
|
def pdf_stroke
|
67
94
|
PDF_FORMAT_STR % [ @r, @g, @b, "RG" ]
|
68
95
|
end
|
@@ -128,22 +155,67 @@ class Color::RGB
|
|
128
155
|
self
|
129
156
|
end
|
130
157
|
|
131
|
-
#
|
158
|
+
# Returns the YIQ (NTSC) colour encoding of the RGB value.
|
159
|
+
def to_yiq
|
160
|
+
y = (@r * 0.299) + (@g * 0.587) + (@b * 0.114)
|
161
|
+
i = (@r * 0.596) + (@g * -0.275) + (@b * -0.321)
|
162
|
+
q = (@r * 0.212) + (@g * -0.523) + (@b * 0.311)
|
163
|
+
Color::YIQ.from_fraction(y, i, q)
|
164
|
+
end
|
165
|
+
|
166
|
+
# Returns the HSL colour encoding of the RGB value.
|
167
|
+
def to_hsl
|
168
|
+
min = [ @r, @g, @b ].min
|
169
|
+
max = [ @r, @g, @b ].max
|
170
|
+
delta = (max - min).to_f
|
171
|
+
|
172
|
+
lum = (max + min) / 2.0
|
173
|
+
|
174
|
+
if delta <= 1e-5 # close to 0.0, so it's a grey
|
175
|
+
hue = 0
|
176
|
+
sat = 0
|
177
|
+
else
|
178
|
+
if (lum - 0.5) <= 1e-5
|
179
|
+
sat = delta / (max + min).to_f
|
180
|
+
else
|
181
|
+
sat = delta / (2 - max - min).to_f
|
182
|
+
end
|
183
|
+
|
184
|
+
if @r == max
|
185
|
+
hue = (@g - @b) / delta.to_f
|
186
|
+
elsif @g == max
|
187
|
+
hue = (2.0 + @b - @r) / delta.to_f
|
188
|
+
elsif (@b - max) <= 1e-5
|
189
|
+
hue = (4.0 + @r - @g) / delta.to_f
|
190
|
+
end
|
191
|
+
hue /= 6.0
|
192
|
+
|
193
|
+
hue += 1 if hue < 0
|
194
|
+
hue -= 1 if hue > 1
|
195
|
+
end
|
196
|
+
Color::HSL.from_fraction(hue, sat, lum)
|
197
|
+
end
|
198
|
+
|
199
|
+
# Mix the RGB hue with White so that the RGB hue is the specified
|
200
|
+
# percentage of the resulting colour. Strictly speaking, this isn't a
|
201
|
+
# darken_by operation.
|
132
202
|
def lighten_by(percent)
|
133
203
|
mix_with(White, percent)
|
134
204
|
end
|
135
205
|
|
136
|
-
#
|
206
|
+
# Mix the RGB hue with Black so that the RGB hue is the specified
|
207
|
+
# percentage of the resulting colour. Strictly speaking, this isn't a
|
208
|
+
# darken_by operation.
|
137
209
|
def darken_by(percent)
|
138
210
|
mix_with(Black, percent)
|
139
211
|
end
|
140
212
|
|
141
213
|
# Mix the mask colour (which must be an RGB object) with the current
|
142
|
-
# colour at the stated opacity percentage (0
|
214
|
+
# colour at the stated opacity percentage (0..100).
|
143
215
|
def mix_with(mask, opacity)
|
144
216
|
opacity /= 100.0
|
145
217
|
rgb = self.dup
|
146
|
-
|
218
|
+
|
147
219
|
rgb.r = (@r * opacity) + (mask.r * (1 - opacity))
|
148
220
|
rgb.g = (@g * opacity) + (mask.g * (1 - opacity))
|
149
221
|
rgb.b = (@b * opacity) + (mask.b * (1 - opacity))
|
@@ -151,25 +223,89 @@ class Color::RGB
|
|
151
223
|
rgb
|
152
224
|
end
|
153
225
|
|
154
|
-
# Returns the
|
155
|
-
|
156
|
-
y = (@r * 0.299) + (@g * 0.587) + (@b * 0.114)
|
157
|
-
i = (@r * 0.596) + (@g * -0.275) + (@b * -0.321)
|
158
|
-
q = (@r * 0.212) + (@g * -0.523) + (@b * 0.311)
|
159
|
-
Color::YIQ.from_fraction(y, i, q)
|
160
|
-
end
|
161
|
-
|
162
|
-
# Returns the brightness value for a colour, a number between 0 and
|
163
|
-
# 1. Based on the Y value of YIQ encoding, representing luminosity, or
|
226
|
+
# Returns the brightness value for a colour, a number between 0..1.
|
227
|
+
# Based on the Y value of YIQ encoding, representing luminosity, or
|
164
228
|
# perceived brightness.
|
229
|
+
#
|
230
|
+
# This may be modified in a future version of color-tools to use the
|
231
|
+
# luminosity value of HSL.
|
165
232
|
def brightness
|
166
233
|
to_yiq.y
|
167
234
|
end
|
168
|
-
|
235
|
+
def to_grayscale
|
236
|
+
Color::GrayScale.from_fraction(to_hsl.l)
|
237
|
+
end
|
238
|
+
|
169
239
|
alias to_greyscale to_grayscale
|
170
240
|
|
171
|
-
|
241
|
+
# Returns a new colour with the brightness adjusted by the specified
|
242
|
+
# percentage. Negative percentages will darken the colour; positive
|
243
|
+
# percentages will brighten the colour.
|
244
|
+
#
|
245
|
+
# Color::RGB::DarkBlue.adjust_brightness(10)
|
246
|
+
# Color::RGB::DarkBlue.adjust_brightness(-10)
|
247
|
+
def adjust_brightness(percent)
|
248
|
+
percent /= 100.0
|
249
|
+
percent += 1.0
|
250
|
+
percent = [ percent, 2.0 ].min
|
251
|
+
percent = [ 0.0, percent ].max
|
172
252
|
|
173
|
-
|
174
|
-
|
253
|
+
hsl = to_hsl
|
254
|
+
hsl.l *= percent
|
255
|
+
hsl.to_rgb
|
256
|
+
end
|
257
|
+
|
258
|
+
# Returns a new colour with the saturation adjusted by the specified
|
259
|
+
# percentage. Negative percentages will reduce the saturation; positive
|
260
|
+
# percentages will increase the saturation.
|
261
|
+
#
|
262
|
+
# Color::RGB::DarkBlue.adjust_saturation(10)
|
263
|
+
# Color::RGB::DarkBlue.adjust_saturation(-10)
|
264
|
+
def adjust_saturation(percent)
|
265
|
+
percent /= 100.0
|
266
|
+
percent += 1.0
|
267
|
+
percent = [ percent, 2.0 ].min
|
268
|
+
percent = [ 0.0, percent ].max
|
269
|
+
|
270
|
+
hsl = to_hsl
|
271
|
+
hsl.s *= percent
|
272
|
+
hsl.to_rgb
|
273
|
+
end
|
274
|
+
|
275
|
+
# Returns a new colour with the hue adjusted by the specified
|
276
|
+
# percentage. Negative percentages will reduce the hue; positive
|
277
|
+
# percentages will increase the hue.
|
278
|
+
#
|
279
|
+
# Color::RGB::DarkBlue.adjust_hue(10)
|
280
|
+
# Color::RGB::DarkBlue.adjust_hue(-10)
|
281
|
+
def adjust_hue(percent)
|
282
|
+
percent /= 100.0
|
283
|
+
percent += 1.0
|
284
|
+
percent = [ percent, 2.0 ].min
|
285
|
+
percent = [ 0.0, percent ].max
|
286
|
+
|
287
|
+
hsl = to_hsl
|
288
|
+
hsl.h *= percent
|
289
|
+
hsl.to_rgb
|
290
|
+
end
|
291
|
+
|
292
|
+
attr_accessor :r, :g, :b
|
293
|
+
remove_method :r=, :g=, :b= ;
|
294
|
+
def r=(rr) #:nodoc:
|
295
|
+
rr = 1.0 if rr > 1
|
296
|
+
rr = 0.0 if rr < 0
|
297
|
+
@r = rr
|
298
|
+
end
|
299
|
+
def g=(gg) #:nodoc:
|
300
|
+
gg = 1.0 if gg > 1
|
301
|
+
gg = 0.0 if gg < 0
|
302
|
+
@g = gg
|
303
|
+
end
|
304
|
+
def b=(bb) #:nodoc:
|
305
|
+
bb = 1.0 if bb > 1
|
306
|
+
bb = 0.0 if bb < 0
|
307
|
+
@b = bb
|
308
|
+
end
|
175
309
|
end
|
310
|
+
|
311
|
+
require 'color/rgb-colors'
|