timocratic-color 1.4.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,281 @@
1
+ #--
2
+ # Color
3
+ # Colour management with Ruby
4
+ # http://rubyforge.org/projects/color
5
+ # Version 1.4.0
6
+ #
7
+ # Licensed under a MIT-style licence. See Licence.txt in the main
8
+ # distribution for full licensing information.
9
+ #
10
+ # Copyright (c) 2005 - 2007 Austin Ziegler and Matt Lyon
11
+ #
12
+ # $Id: test_all.rb 55 2007-02-03 23:29:34Z austin $
13
+ #++
14
+
15
+ # An CMYK colour object. CMYK (cyan, magenta, yellow, and black) colours are
16
+ # based on additive percentages of ink. A CMYK colour of (0.3, 0, 0.8, 0.3)
17
+ # would be mixed from 30% cyan, 0% magenta, 80% yellow, and 30% black.
18
+ # Primarily used in four-colour printing processes.
19
+ class Color::CMYK
20
+ # The format of a DeviceCMYK colour for PDF. In color-tools 2.0 this will
21
+ # be removed from this package and added back as a modification by the
22
+ # PDF::Writer package.
23
+ PDF_FORMAT_STR = "%.3f %.3f %.3f %.3f %s"
24
+
25
+ # Compares the other colour to this one. The other colour will be
26
+ # converted to CMYK before comparison, so the comparison between a CMYK
27
+ # colour and a non-CMYK colour will be approximate and based on the other
28
+ # colour's #to_cmyk conversion. If there is no #to_cmyk conversion, this
29
+ # will raise an exception. This will report that two CMYK colours are
30
+ # equivalent if all component values are within COLOR_TOLERANCE of each
31
+ # other.
32
+ def ==(other)
33
+ other = other.to_cmyk
34
+ other.kind_of?(Color::CMYK) and
35
+ ((@c - other.c).abs <= Color::COLOR_TOLERANCE) and
36
+ ((@m - other.m).abs <= Color::COLOR_TOLERANCE) and
37
+ ((@y - other.y).abs <= Color::COLOR_TOLERANCE) and
38
+ ((@k - other.k).abs <= Color::COLOR_TOLERANCE)
39
+ end
40
+
41
+ # Creates a CMYK colour object from fractional values 0..1.
42
+ #
43
+ # Color::CMYK.from_fraction(0.3, 0, 0.8, 0.3)
44
+ def self.from_fraction(c = 0, m = 0, y = 0, k = 0)
45
+ colour = Color::CMYK.new
46
+ colour.c = c
47
+ colour.m = m
48
+ colour.y = y
49
+ colour.k = k
50
+ colour
51
+ end
52
+
53
+ # Creates a CMYK colour object from percentages. Internally, the colour is
54
+ # managed as fractional values 0..1.
55
+ #
56
+ # Color::CMYK.new(30, 0, 80, 30)
57
+ def self.from_percent(c = 0, m = 0, y = 0, k = 0)
58
+ Color::CMYK.new(c, m, y, k)
59
+ end
60
+
61
+ # Creates a CMYK colour object from percentages. Internally, the colour is
62
+ # managed as fractional values 0..1.
63
+ #
64
+ # Color::CMYK.new(30, 0, 80, 30)
65
+ def initialize(c = 0, m = 0, y = 0, k = 0)
66
+ @c = c / 100.0
67
+ @m = m / 100.0
68
+ @y = y / 100.0
69
+ @k = k / 100.0
70
+ end
71
+
72
+ # Present the colour as a DeviceCMYK fill colour string for PDF. This will
73
+ # be removed from the default package in color-tools 2.0.
74
+ def pdf_fill
75
+ PDF_FORMAT_STR % [ @c, @m, @y, @k, "k" ]
76
+ end
77
+
78
+ # Present the colour as a DeviceCMYK stroke colour string for PDF. This
79
+ # will be removed from the default package in color-tools 2.0.
80
+ def pdf_stroke
81
+ PDF_FORMAT_STR % [ @c, @m, @y, @k, "K" ]
82
+ end
83
+
84
+ # Present the colour as an RGB HTML/CSS colour string (e.g., "#aabbcc").
85
+ # Note that this will perform a #to_rgb operation using the default
86
+ # conversion formula.
87
+ def html
88
+ to_rgb.html
89
+ end
90
+
91
+ # Present the colour as an RGB HTML/CSS colour string (e.g., "rgb(0%, 50%,
92
+ # 100%)"). Note that this will perform a #to_rgb operation using the
93
+ # default conversion formula.
94
+ def css_rgb
95
+ to_rgb.css_rgb
96
+ end
97
+
98
+ # Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g.,
99
+ # "rgb(0%, 50%, 100%, 1)"). Note that this will perform a #to_rgb
100
+ # operation using the default conversion formula.
101
+ def css_rgba
102
+ to_rgb.css_rgba
103
+ end
104
+
105
+ # Present the colour as an HSL HTML/CSS colour string (e.g., "hsl(180,
106
+ # 25%, 35%)"). Note that this will perform a #to_hsl operation using the
107
+ # default conversion formula.
108
+ def css_hsl
109
+ to_hsl.css_hsl
110
+ end
111
+
112
+ # Present the colour as an HSLA (with alpha) HTML/CSS colour string (e.g.,
113
+ # "hsla(180, 25%, 35%, 1)"). Note that this will perform a #to_hsl
114
+ # operation using the default conversion formula.
115
+ def css_hsla
116
+ to_hsl.css_hsla
117
+ end
118
+
119
+ # Converts the CMYK colour to RGB. Most colour experts strongly suggest
120
+ # that this is not a good idea (some even suggesting that it's a very bad
121
+ # idea). CMYK represents additive percentages of inks on white paper,
122
+ # whereas RGB represents mixed colour intensities on a black screen.
123
+ #
124
+ # However, the colour conversion can be done, and there are two different
125
+ # methods for the conversion that provide slightly different results.
126
+ # Adobe PDF conversions are done with the first form.
127
+ #
128
+ # # Adobe PDF Display Formula
129
+ # r = 1.0 - min(1.0, c + k)
130
+ # g = 1.0 - min(1.0, m + k)
131
+ # b = 1.0 - min(1.0, y + k)
132
+ #
133
+ # # Other
134
+ # r = 1.0 - (c * (1.0 - k) + k)
135
+ # g = 1.0 - (m * (1.0 - k) + k)
136
+ # b = 1.0 - (y * (1.0 - k) + k)
137
+ #
138
+ # If we have a CMYK colour of [33% 66% 83% 25%], the first method will
139
+ # give an approximate RGB colour of (107, 23, 0) or #6b1700. The second
140
+ # method will give an approximate RGB colour of (128, 65, 33) or #804121.
141
+ # Which is correct? Although the colours may seem to be drastically
142
+ # different in the RGB colour space, they are very similar colours,
143
+ # differing mostly in intensity. The first is a darker, slightly redder
144
+ # brown; the second is a lighter brown.
145
+ #
146
+ # Because of this subtlety, both methods are now offered for conversion.
147
+ # The Adobe method is not used by default; to enable it, pass +true+ to
148
+ # #to_rgb.
149
+ #
150
+ # Future versions of Color may offer other conversion mechanisms that
151
+ # offer greater colour fidelity, including recognition of ICC colour
152
+ # profiles.
153
+ def to_rgb(use_adobe_method = false)
154
+ if use_adobe_method
155
+ r = 1.0 - [1.0, @c + @k].min
156
+ g = 1.0 - [1.0, @m + @k].min
157
+ b = 1.0 - [1.0, @y + @k].min
158
+ else
159
+ r = 1.0 - (@c.to_f * (1.0 - @k.to_f) + @k.to_f)
160
+ g = 1.0 - (@m.to_f * (1.0 - @k.to_f) + @k.to_f)
161
+ b = 1.0 - (@y.to_f * (1.0 - @k.to_f) + @k.to_f)
162
+ end
163
+ Color::RGB.from_fraction(r, g, b)
164
+ end
165
+
166
+ # Converts the CMYK colour to a single greyscale value. There are
167
+ # undoubtedly multiple methods for this conversion, but only a minor
168
+ # variant of the Adobe conversion method will be used:
169
+ #
170
+ # g = 1.0 - min(1.0, 0.299 * c + 0.587 * m + 0.114 * y + k)
171
+ #
172
+ # This treats the CMY values similarly to YIQ (NTSC) values and then adds
173
+ # the level of black. This is a variant of the Adobe version because it
174
+ # uses the more precise YIQ (NTSC) conversion values for Y (intensity)
175
+ # rather than the approximates provided by Adobe (0.3, 0.59, and 0.11).
176
+ def to_grayscale
177
+ c = 0.299 * @c.to_f
178
+ m = 0.587 * @m.to_f
179
+ y = 0.114 * @y.to_f
180
+ g = 1.0 - [1.0, c + m + y + @k].min
181
+ Color::GrayScale.from_fraction(g)
182
+ end
183
+ alias to_greyscale to_grayscale
184
+
185
+ def to_cmyk
186
+ self
187
+ end
188
+
189
+ def inspect
190
+ "CMYK [%.2f%%, %.2f%%, %.2f%%, %.2f%%]" % [ cyan, magenta, yellow, black ]
191
+ end
192
+
193
+ # Converts to RGB then YIQ.
194
+ def to_yiq
195
+ to_rgb.to_yiq
196
+ end
197
+
198
+ # Converts to RGB then HSL.
199
+ def to_hsl
200
+ to_rgb.to_hsl
201
+ end
202
+
203
+ # Returns the cyan (C) component of the CMYK colour as a percentage value.
204
+ def cyan
205
+ @c * 100.0
206
+ end
207
+ # Returns the cyan (C) component of the CMYK colour as a value in the
208
+ # range 0.0 .. 1.0.
209
+ def c
210
+ @c
211
+ end
212
+ # Sets the cyan (C) component of the CMYK colour as a percentage value.
213
+ def cyan=(cc)
214
+ @c = Color.normalize(cc / 100.0)
215
+ end
216
+ # Sets the cyan (C) component of the CMYK colour as a value in the range
217
+ # 0.0 .. 1.0.
218
+ def c=(cc)
219
+ @c = Color.normalize(cc)
220
+ end
221
+
222
+ # Returns the magenta (M) component of the CMYK colour as a percentage
223
+ # value.
224
+ def magenta
225
+ @m * 100.0
226
+ end
227
+ # Returns the magenta (M) component of the CMYK colour as a value in the
228
+ # range 0.0 .. 1.0.
229
+ def m
230
+ @m
231
+ end
232
+ # Sets the magenta (M) component of the CMYK colour as a percentage value.
233
+ def magenta=(mm)
234
+ @m = Color.normalize(mm / 100.0)
235
+ end
236
+ # Sets the magenta (M) component of the CMYK colour as a value in the
237
+ # range 0.0 .. 1.0.
238
+ def m=(mm)
239
+ @m = Color.normalize(mm)
240
+ end
241
+
242
+ # Returns the yellow (Y) component of the CMYK colour as a percentage
243
+ # value.
244
+ def yellow
245
+ @y * 100.0
246
+ end
247
+ # Returns the yellow (Y) component of the CMYK colour as a value in the
248
+ # range 0.0 .. 1.0.
249
+ def y
250
+ @y
251
+ end
252
+ # Sets the yellow (Y) component of the CMYK colour as a percentage value.
253
+ def yellow=(yy)
254
+ @y = Color.normalize(yy / 100.0)
255
+ end
256
+ # Sets the yellow (Y) component of the CMYK colour as a value in the range
257
+ # 0.0 .. 1.0.
258
+ def y=(kk)
259
+ @y = Color.normalize(kk)
260
+ end
261
+
262
+ # Returns the black (K) component of the CMYK colour as a percentage
263
+ # value.
264
+ def black
265
+ @k * 100.0
266
+ end
267
+ # Returns the black (K) component of the CMYK colour as a value in the
268
+ # range 0.0 .. 1.0.
269
+ def k
270
+ @k
271
+ end
272
+ # Sets the black (K) component of the CMYK colour as a percentage value.
273
+ def black=(kk)
274
+ @k = Color.normalize(kk / 100.0)
275
+ end
276
+ # Sets the black (K) component of the CMYK colour as a value in the range
277
+ # 0.0 .. 1.0.
278
+ def k=(kk)
279
+ @k = Color.normalize(kk)
280
+ end
281
+ end
@@ -0,0 +1,30 @@
1
+ #--
2
+ # Color
3
+ # Colour management with Ruby
4
+ # http://rubyforge.org/projects/color
5
+ # Version 1.4.0
6
+ #
7
+ # Licensed under a MIT-style licence. See Licence.txt in the main
8
+ # distribution for full licensing information.
9
+ #
10
+ # Copyright (c) 2005 - 2007 Austin Ziegler and Matt Lyon
11
+ #
12
+ # $Id: test_all.rb 55 2007-02-03 23:29:34Z austin $
13
+ #++
14
+
15
+ require 'color'
16
+
17
+ # This namespace contains some CSS colour names.
18
+ module Color::CSS
19
+ # Returns the RGB colour for name or +nil+ if the name is not valid.
20
+ def self.[](name)
21
+ @colors[name.to_s.downcase.to_sym]
22
+ end
23
+
24
+ @colors = {}
25
+ Color::RGB.constants.each do |const|
26
+ next if const == "PDF_FORMAT_STR"
27
+ next if const == "Metallic"
28
+ @colors[const.downcase.to_sym] ||= Color::RGB.const_get(const)
29
+ end
30
+ end
@@ -0,0 +1,214 @@
1
+ #--
2
+ # Color
3
+ # Colour management with Ruby
4
+ # http://rubyforge.org/projects/color
5
+ # Version 1.4.0
6
+ #
7
+ # Licensed under a MIT-style licence. See Licence.txt in the main
8
+ # distribution for full licensing information.
9
+ #
10
+ # Copyright (c) 2005 - 2007 Austin Ziegler and Matt Lyon
11
+ #
12
+ # $Id: test_all.rb 55 2007-02-03 23:29:34Z austin $
13
+ #++
14
+
15
+ # A colour object representing shades of grey. Used primarily in PDF
16
+ # document creation.
17
+ class Color::GrayScale
18
+ # The format of a DeviceGrey colour for PDF. In color-tools 2.0 this will
19
+ # be removed from this package and added back as a modification by the
20
+ # PDF::Writer package.
21
+ PDF_FORMAT_STR = "%.3f %s"
22
+
23
+ # Creates a greyscale colour object from fractional values 0..1.
24
+ #
25
+ # Color::GreyScale.from_fraction(0.5)
26
+ def self.from_fraction(g = 0)
27
+ color = Color::GrayScale.new
28
+ color.g = g
29
+ color
30
+ end
31
+
32
+ # Creates a greyscale colour object from percentages 0..100.
33
+ #
34
+ # Color::GrayScale.from_percent(50)
35
+ def self.from_percent(g = 0)
36
+ Color::GrayScale.new(g)
37
+ end
38
+
39
+ # Creates a greyscale colour object from percentages 0..100.
40
+ #
41
+ # Color::GrayScale.new(50)
42
+ def initialize(g = 0)
43
+ @g = g / 100.0
44
+ end
45
+
46
+ # Compares the other colour to this one. The other colour will be
47
+ # converted to GreyScale before comparison, so the comparison between a
48
+ # GreyScale colour and a non-GreyScale colour will be approximate and
49
+ # based on the other colour's #to_greyscale conversion. If there is no
50
+ # #to_greyscale conversion, this will raise an exception. This will report
51
+ # that two GreyScale values are equivalent if they are within
52
+ # COLOR_TOLERANCE of each other.
53
+ def ==(other)
54
+ other = other.to_grayscale
55
+ other.kind_of?(Color::GrayScale) and
56
+ ((@g - other.g).abs <= Color::COLOR_TOLERANCE)
57
+ end
58
+
59
+ # Present the colour as a DeviceGrey fill colour string for PDF. This will
60
+ # be removed from the default package in color-tools 2.0.
61
+ def pdf_fill
62
+ PDF_FORMAT_STR % [ @g, "g" ]
63
+ end
64
+
65
+ # Present the colour as a DeviceGrey stroke colour string for PDF. This
66
+ # will be removed from the default package in color-tools 2.0.
67
+ def pdf_stroke
68
+ PDF_FORMAT_STR % [ @g, "G" ]
69
+ end
70
+
71
+ def to_255
72
+ [(@g * 255).round, 255].min
73
+ end
74
+ private :to_255
75
+
76
+ # Present the colour as an HTML/CSS colour string.
77
+ def html
78
+ gs = "%02x" % to_255
79
+ "##{gs * 3}"
80
+ end
81
+
82
+ # Present the colour as an RGB HTML/CSS colour string (e.g., "rgb(0%, 50%,
83
+ # 100%)").
84
+ def css_rgb
85
+ "rgb(%3.2f%%, %3.2f%%, %3.2f%%)" % [ gray, gray, gray ]
86
+ end
87
+
88
+ # Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g.,
89
+ # "rgb(0%, 50%, 100%, 1)").
90
+ def css_rgba
91
+ "rgba(%3.2f%%, %3.2f%%, %3.2f%%, %1.2f)" % [ gray, gray, gray, 1 ]
92
+ end
93
+
94
+ # Present the colour as an HSL HTML/CSS colour string (e.g., "hsl(180,
95
+ # 25%, 35%)"). Note that this will perform a #to_hsl operation.
96
+ def css_hsl
97
+ to_hsl.css_hsl
98
+ end
99
+
100
+ # Present the colour as an HSLA (with alpha) HTML/CSS colour string (e.g.,
101
+ # "hsla(180, 25%, 35%, 1)"). Note that this will perform a #to_hsl
102
+ # operation.
103
+ def css_hsla
104
+ to_hsl.css_hsla
105
+ end
106
+
107
+ # Convert the greyscale colour to CMYK.
108
+ def to_cmyk
109
+ k = 1.0 - @g.to_f
110
+ Color::CMYK.from_fraction(0, 0, 0, k)
111
+ end
112
+
113
+ # Convert the greyscale colour to RGB.
114
+ def to_rgb(ignored = true)
115
+ Color::RGB.from_fraction(g, g, g)
116
+ end
117
+
118
+ # Reflexive conversion.
119
+ def to_grayscale
120
+ self
121
+ end
122
+ alias to_greyscale to_grayscale
123
+
124
+ # Lightens the greyscale colour by the stated percent.
125
+ def lighten_by(percent)
126
+ g = [@g + (@g * (percent / 100.0)), 1.0].min
127
+ Color::GrayScale.from_fraction(g)
128
+ end
129
+
130
+ # Darken the greyscale colour by the stated percent.
131
+ def darken_by(percent)
132
+ g = [@g - (@g * (percent / 100.0)), 0.0].max
133
+ Color::GrayScale.from_fraction(g)
134
+ end
135
+
136
+ # Returns the YIQ (NTSC) colour encoding of the greyscale value. This is
137
+ # an approximation, as the values for I and Q are calculated by treating
138
+ # the greyscale value as an RGB value. The Y (intensity or brightness)
139
+ # value is the same as the greyscale value.
140
+ def to_yiq
141
+ y = @g
142
+ i = (@g * 0.596) + (@g * -0.275) + (@g * -0.321)
143
+ q = (@g * 0.212) + (@g * -0.523) + (@g * 0.311)
144
+ Color::YIQ.from_fraction(y, i, q)
145
+ end
146
+
147
+ # Returns the HSL colour encoding of the greyscale value.
148
+ def to_hsl
149
+ Color::HSL.from_fraction(0, 0, @g)
150
+ end
151
+
152
+ # Returns the brightness value for this greyscale value; this is the
153
+ # greyscale value itself.
154
+ def brightness
155
+ @g
156
+ end
157
+
158
+ # Returns the grayscale value as a percentage of white (100% gray is
159
+ # white).
160
+ def gray
161
+ @g * 100.0
162
+ end
163
+ alias grey gray
164
+ # Returns the grayscale value as a fractional value of white in the range
165
+ # 0.0 .. 1.0.
166
+ def g
167
+ @g
168
+ end
169
+ # Sets the grayscale value as a percentage of white.
170
+ def gray=(gg)
171
+ @g = Color.normalize(gg / 100.0)
172
+ end
173
+ alias grey= gray= ;
174
+ # Returns the grayscale value as a fractional value of white in the range
175
+ # 0.0 .. 1.0.
176
+ def g=(gg)
177
+ @g = Color.normalize(gg)
178
+ end
179
+
180
+ # Adds another colour to the current colour. The other colour will be
181
+ # converted to grayscale before addition. This conversion depends upon a
182
+ # #to_grayscale method on the other colour.
183
+ #
184
+ # The addition is done using the grayscale accessor methods to ensure a
185
+ # valid colour in the result.
186
+ def +(other)
187
+ other = other.to_grayscale
188
+ ng = self.dup
189
+ ng.g += other.g
190
+ ng
191
+ end
192
+
193
+ # Subtracts another colour to the current colour. The other colour will be
194
+ # converted to grayscale before subtraction. This conversion depends upon
195
+ # a #to_grayscale method on the other colour.
196
+ #
197
+ # The subtraction is done using the grayscale accessor methods to ensure a
198
+ # valid colour in the result.
199
+ def -(other)
200
+ other = other.to_grayscale
201
+ ng = self.dup
202
+ ng.g -= other.g
203
+ ng
204
+ end
205
+
206
+ def inspect
207
+ "Gray [%.2f%%]" % [ gray ]
208
+ end
209
+ end
210
+
211
+ module Color
212
+ # A synonym for Color::GrayScale.
213
+ GreyScale = GrayScale
214
+ end