color 1.8 → 2.0.0.pre.1

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.
Files changed (52) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +301 -0
  3. data/CODE_OF_CONDUCT.md +128 -0
  4. data/CONTRIBUTING.md +70 -0
  5. data/CONTRIBUTORS.md +10 -0
  6. data/LICENCE.md +27 -0
  7. data/Manifest.txt +11 -23
  8. data/README.md +54 -0
  9. data/Rakefile +74 -61
  10. data/SECURITY.md +34 -0
  11. data/lib/color/cielab.rb +348 -0
  12. data/lib/color/cmyk.rb +279 -213
  13. data/lib/color/grayscale.rb +128 -160
  14. data/lib/color/hsl.rb +205 -173
  15. data/lib/color/rgb/colors.rb +177 -163
  16. data/lib/color/rgb.rb +536 -541
  17. data/lib/color/version.rb +5 -0
  18. data/lib/color/xyz.rb +214 -0
  19. data/lib/color/yiq.rb +91 -46
  20. data/lib/color.rb +208 -142
  21. data/test/fixtures/cielab.json +444 -0
  22. data/test/minitest_helper.rb +20 -4
  23. data/test/test_cmyk.rb +49 -72
  24. data/test/test_color.rb +58 -112
  25. data/test/test_grayscale.rb +35 -57
  26. data/test/test_hsl.rb +71 -77
  27. data/test/test_rgb.rb +195 -267
  28. data/test/test_yiq.rb +12 -30
  29. metadata +90 -107
  30. data/.autotest +0 -5
  31. data/.coveralls.yml +0 -2
  32. data/.gemtest +0 -0
  33. data/.hoerc +0 -2
  34. data/.minitest.rb +0 -2
  35. data/.travis.yml +0 -41
  36. data/Code-of-Conduct.rdoc +0 -41
  37. data/Contributing.rdoc +0 -62
  38. data/Gemfile +0 -9
  39. data/History.rdoc +0 -194
  40. data/Licence.rdoc +0 -27
  41. data/README.rdoc +0 -52
  42. data/lib/color/css.rb +0 -7
  43. data/lib/color/palette/adobecolor.rb +0 -260
  44. data/lib/color/palette/gimp.rb +0 -104
  45. data/lib/color/palette/monocontrast.rb +0 -164
  46. data/lib/color/palette.rb +0 -4
  47. data/lib/color/rgb/contrast.rb +0 -57
  48. data/lib/color/rgb/metallic.rb +0 -28
  49. data/test/test_adobecolor.rb +0 -405
  50. data/test/test_css.rb +0 -19
  51. data/test/test_gimp.rb +0 -87
  52. data/test/test_monocontrast.rb +0 -130
data/lib/color.rb CHANGED
@@ -1,113 +1,226 @@
1
- # :title: Color -- Colour Management with Ruby
2
- # :main: README.rdoc
3
-
4
- # = Colour Management with Ruby
1
+ # frozen_string_literal: true
2
+
3
+ # # \Color -- \Color Math in Ruby
4
+ #
5
+ # - **code**: [github.com/halostatue/color](https://github.com/halostatue/color)
6
+ # - **issues**: [github.com/halostatue/color/issues](https://github.com/halostatue/color/issues)
7
+ # - **changelog**: [CHANGELOG](rdoc-ref:CHANGELOG.md)
8
+ #
9
+ # \Color is a Ruby library to provide RGB, CMYK, HSL, and other color space manipulation
10
+ # support to applications that require it. It provides 152 named RGB colors that are
11
+ # commonly supported in HTML, SVG, and X11 applications.
12
+ #
13
+ # The \Color library performs purely mathematical manipulation of the colors based on
14
+ # color theory without reference to device color profiles (such as sRGB or Adobe RGB). For
15
+ # most purposes, when working with RGB and HSL color spaces, this won't matter. Absolute
16
+ # color spaces (like CIE LAB and CIE XYZ), however, cannot be reliably converted to
17
+ # relative color spaces (like RGB) without color profiles. When necessary for conversions,
18
+ # \Color provides \D65 and \D50 reference white values in Color::XYZ.
19
+ #
20
+ # \Color 2.0 is a major release to the \Color library, dropping support for all versions of
21
+ # Ruby prior to 3.2.
22
+ #
23
+ # > **NOTE**: This is a pre-release version of \Color 2.0.
24
+ # > The main goals for a 2.0 release have been met: modernizing the codebase, but
25
+ # > consideration will be given to improving color transformation robustness and accuracy
26
+ # > with Matrix operations and Rational numbers instead of floating point decimal values.
27
+ #
28
+ # In \Color 2.0, color objects are immutable (derived from Data) and do not expose the
29
+ # `new` class method, instead using only `from_*` class methods. There is always
30
+ # a `from_values` class method which represents the native color channel values (which may
31
+ # not match the internal representation). This method _may_ have a counterpart that is
32
+ # recommended for readability.
5
33
  module Color
6
- COLOR_VERSION = '1.8'
7
-
8
- class RGB; end
9
- class CMYK; end
10
- class HSL; end
11
- class GrayScale; end
12
- class YIQ; end
13
-
14
- # The maximum "resolution" for colour math; if any value is less than or
15
- # equal to this value, it is treated as zero.
16
- COLOR_EPSILON = 1e-5
17
- # The tolerance for comparing the components of two colours. In general,
18
- # colours are considered equal if all of their components are within this
19
- # tolerance value of each other.
20
- COLOR_TOLERANCE = 1e-4
21
-
22
- # Compares the +other+ colour to this one. The +other+ colour will be
23
- # coerced to the same type as the current colour. Such converted colour
24
- # comparisons will always be more approximate than non-converted
25
- # comparisons.
34
+ ##
35
+ # The maximum "resolution" for color math; if any value is less than or equal to this
36
+ # value, it is treated as zero.
37
+ EPSILON = 1e-5
38
+
39
+ ##
40
+ # The tolerance for comparing the components of two colors. In general, colors are
41
+ # considered equal if all of their components are within this tolerance value of each
42
+ # other.
43
+ TOLERANCE = 1e-4
44
+
45
+ # :stopdoc:
46
+ CIELAB = Data.define(:l, :a, :b)
47
+ CMYK = Data.define(:c, :m, :y, :k)
48
+ Grayscale = Data.define(:g)
49
+ HSL = Data.define(:h, :s, :l)
50
+ RGB = Data.define(:r, :g, :b, :names)
51
+ XYZ = Data.define(:x, :y, :z)
52
+ YIQ = Data.define(:y, :i, :q)
53
+ # :startdoc:
54
+
55
+ ##
56
+ # It is useful to know the number of components in some cases. Since most colors are
57
+ # defined with three components, we define a constant value here. Color classes that
58
+ # require more or less should override this.
26
59
  #
27
- # If the +other+ colour cannot be coerced to the current colour class, a
28
- # +NoMethodError+ exception will be raised.
60
+ # We _could_ define this as `members.count`, but this would require a special case
61
+ # for Color::RGB _regardless_ because there's an additional member for RGB colors
62
+ # (names).
63
+ def components = 3 # :nodoc:
64
+
65
+ ##
66
+ # Compares the `other` color to this one. The `other` color will be coerced to the same
67
+ # type as the current color. Such converted color comparisons will always be more
68
+ # approximate than non-converted comparisons.
29
69
  #
30
- # All values are compared as floating-point values, so two colours will be
31
- # reported equivalent if all component values are within COLOR_TOLERANCE
32
- # of each other.
70
+ # All values are compared as floating-point values, so two colors will be reported
71
+ # equivalent if all component values are within +TOLERANCE+ of each other.
33
72
  def ==(other)
34
- Color.equivalent?(self, other)
73
+ other.is_a?(Color) && to_internal.zip(coerce(other).to_internal).all? { near?(_1, _2) }
35
74
  end
36
75
 
37
- # The primary name for the colour.
38
- def name
39
- names.first
40
- end
76
+ ##
77
+ # Apply the provided block to each color component in turn, returning a new color
78
+ # instance.
79
+ def map(&block) = self.class.from_internal(*to_internal.map(&block))
41
80
 
42
- # All names for the colour.
43
- def names
44
- self.names = nil unless defined? @names
45
- @names
46
- end
47
- def names=(n) # :nodoc:
48
- @names = Array(n).flatten.compact.map(&:to_s).map(&:downcase).sort.uniq
49
- end
50
- alias_method :name=, :names=
51
- end
81
+ ##
82
+ # Apply the provided block to the color component pairs in turn, returning a new color
83
+ # instance.
84
+ def map_with(other, &block) = self.class.from_internal(*zip(other).map(&block))
52
85
 
53
- class << Color
54
- # Returns +true+ if the value is less than COLOR_EPSILON.
55
- def near_zero?(value)
56
- (value.abs <= Color::COLOR_EPSILON)
57
- end
86
+ ##
87
+ # Zip the color component pairs together.
88
+ def zip(other) = to_internal.zip(coerce(other).to_internal)
58
89
 
59
- # Returns +true+ if the value is within COLOR_EPSILON of zero or less than
60
- # zero.
61
- def near_zero_or_less?(value)
62
- (value < 0.0 or near_zero?(value))
90
+ ##
91
+ # Multiplies each component value by the scaling factor or factors, returning a new
92
+ # color object with the scaled values.
93
+ #
94
+ # If a single scaling factor is provided, it is applied to all components:
95
+ #
96
+ # ```ruby
97
+ # rgb = Color::RGB::Wheat # => RGB [#f5deb3]
98
+ # rgb.scale(0.75) # => RGB [#b8a786]
99
+ # ```
100
+ #
101
+ # If more than one scaling factor is provided, there must be exactly one factor for each
102
+ # color component of the color object or an `ArgumentError` will be raised.
103
+ #
104
+ # ```ruby
105
+ # rgb = Color::RGB::Wheat # => RGB [#f5deb3]
106
+ # # 0xf5 * 0 == 0x00, 0xde * 0.5 == 0x6f, 0xb3 * 2 == 0x166 (clamped to 0xff)
107
+ # rgb.scale(0, 0.5, 2) # => RGB [#006fff]
108
+ #
109
+ # rgb.scale(1, 2) # => Invalid scaling factors [1, 2] for Color::RGB (ArgumentError)
110
+ # ```
111
+ def scale(*factors)
112
+ if factors.size == 1
113
+ factor = factors.first
114
+ map { _1 * factor }
115
+ elsif factors.size != components
116
+ raise ArgumentError, "Invalid scaling factors #{factors.inspect} for #{self.class}"
117
+ else
118
+ new_components = to_internal.zip(factors).map { _1 * _2 }
119
+ self.class.from_internal(*new_components)
120
+ end
63
121
  end
64
122
 
65
- # Returns +true+ if the value is within COLOR_EPSILON of one.
66
- def near_one?(value)
67
- near_zero?(value - 1.0)
123
+ ##
124
+ def css_value(value, format = nil) # :nodoc:
125
+ if value.nil?
126
+ "none"
127
+ elsif near_zero?(value)
128
+ "0"
129
+ else
130
+ suffix =
131
+ case format
132
+ in :percent
133
+ "%"
134
+ in :degrees
135
+ "deg"
136
+ else
137
+ ""
138
+ end
139
+
140
+ "%3.2f%s" % [value, suffix]
141
+ end
68
142
  end
69
143
 
70
- # Returns +true+ if the value is within COLOR_EPSILON of one or more than
71
- # one.
72
- def near_one_or_more?(value)
73
- (value > 1.0 or near_one?(value))
74
- end
144
+ private
145
+
146
+ ##
147
+ def from_internal(...) = self.class.from_internal(...)
148
+
149
+ ##
150
+ # Returns `true` if the value is less than EPSILON.
151
+ def near_zero?(value) = (value.abs <= Color::EPSILON) # :nodoc:
152
+
153
+ ##
154
+ # Returns `true` if the value is within EPSILON of zero or less than zero.
155
+ def near_zero_or_less?(value) = (value < 0.0 or near_zero?(value)) # :nodoc:
156
+
157
+ ##
158
+ # Returns +true+ if the value is within EPSILON of one.
159
+ def near_one?(value) = near_zero?(value - 1.0) # :nodoc:
160
+
161
+ ##
162
+ # Returns +true+ if the value is within EPSILON of one or more than one.
163
+ def near_one_or_more?(value) = (value > 1.0 or near_one?(value)) # :nodoc:
75
164
 
165
+ ##
76
166
  # Returns +true+ if the two values provided are near each other.
77
- def near?(x, y)
78
- (x - y).abs <= Color::COLOR_TOLERANCE
79
- end
167
+ def near?(x, y) = (x - y).abs <= Color::TOLERANCE # :nodoc:
80
168
 
81
- # Returns +true+ if the two colours are roughly equivalent. If colour
82
- # conversions are required, this all conversions will be implemented
83
- # using the default conversion mechanism.
84
- def equivalent?(a, b)
85
- return false unless a.kind_of?(Color) && b.kind_of?(Color)
86
- a.to_a.zip(a.coerce(b).to_a).all? { |(x, y)| near?(x, y) }
169
+ ##
170
+ def to_degrees(radians) # :nodoc:
171
+ if radians < 0
172
+ (Math::PI + radians % -Math::PI) * (180 / Math::PI) + 180
173
+ else
174
+ (radians % Math::PI) * (180 / Math::PI)
175
+ end
87
176
  end
88
177
 
89
- # Coerces, if possible, the second given colour object to the first
90
- # given colour object type. This will probably involve colour
91
- # conversion and therefore a loss of fidelity.
92
- def coerce(a, b)
93
- a.coerce(b)
178
+ ##
179
+ def to_radians(degrees) # :nodoc:
180
+ degrees = ((degrees % 360) + 360) % 360
181
+ if degrees >= 180
182
+ Math::PI * (degrees - 360) / 180.0
183
+ else
184
+ Math::PI * degrees / 180.0
185
+ end
94
186
  end
95
187
 
188
+ ##
96
189
  # Normalizes the value to the range (0.0) .. (1.0).
97
- def normalize(value)
98
- if near_zero_or_less? value
99
- 0.0
100
- elsif near_one_or_more? value
101
- 1.0
190
+ module_function def normalize(value, range = 0.0..1.0) # :nodoc:
191
+ value = value.clamp(range)
192
+ if near?(value, range.begin)
193
+ range.begin
194
+ elsif near?(value, range.end)
195
+ range.end
102
196
  else
103
197
  value
104
198
  end
105
199
  end
106
- alias normalize_fractional normalize
107
200
 
201
+ ##
202
+ # Translates a value from range `from` to range `to`. Both ranges must be closed.
203
+ # As 0.0 .. 1.0 is a common internal range, it is the default for `from`.
204
+ #
205
+ # This is based on the formula:
206
+ #
207
+ # [a, b] ← from ← [from.begin, from.end]
208
+ # [c, d] ← to ← [to.begin, to.end]
209
+ #
210
+ # y = (((x - a) * (d - c)) / (b - a)) + c
211
+ #
212
+ # The value is clamped to the values of `to`.
213
+ module_function def translate_range(x, to:, from: 0.0..1.0) # :nodoc:
214
+ a, b = [from.begin, from.end]
215
+ c, d = [to.begin, to.end]
216
+ y = (((x - a) * (d - c)) / (b - a)) + c
217
+ y.clamp(to)
218
+ end
219
+
220
+ ##
108
221
  # Normalizes the value to the specified range.
109
- def normalize_to_range(value, range)
110
- range = (range.end..range.begin) if (range.end < range.begin)
222
+ def normalize_to_range(value, range) # :nodoc:
223
+ range = (range.end..range.begin) if range.end < range.begin
111
224
 
112
225
  if value <= range.begin
113
226
  range.begin
@@ -118,68 +231,21 @@ class << Color
118
231
  end
119
232
  end
120
233
 
234
+ ##
121
235
  # Normalize the value to the range (0) .. (255).
122
- def normalize_byte(value)
123
- normalize_to_range(value, 0..255).to_i
124
- end
125
- alias normalize_8bit normalize_byte
236
+ def normalize_byte(value) = normalize_to_range(value, 0..255).to_i # :nodoc:
126
237
 
238
+ ##
127
239
  # Normalize the value to the range (0) .. (65535).
128
- def normalize_word(value)
129
- normalize_to_range(value, 0..65535).to_i
130
- end
131
- alias normalize_16bit normalize_word
240
+ def normalize_word(value) = normalize_to_range(value, 0..65535).to_i # :nodoc:
132
241
  end
133
242
 
134
- require 'color/rgb'
135
- require 'color/cmyk'
136
- require 'color/grayscale'
137
- require 'color/hsl'
138
- require 'color/yiq'
139
- require 'color/css'
140
-
141
- class << Color
142
- def const_missing(name) #:nodoc:
143
- case name
144
- when "VERSION", :VERSION, "COLOR_TOOLS_VERSION", :COLOR_TOOLS_VERSION
145
- warn "Color::#{name} has been deprecated. Use Color::COLOR_VERSION instead."
146
- Color::COLOR_VERSION
147
- else
148
- if Color::RGB.const_defined?(name)
149
- warn "Color::#{name} has been deprecated. Use Color::RGB::#{name} instead."
150
- Color::RGB.const_get(name)
151
- else
152
- super
153
- end
154
- end
155
- end
243
+ require "color/cmyk"
244
+ require "color/grayscale"
245
+ require "color/hsl"
246
+ require "color/cielab"
247
+ require "color/rgb"
248
+ require "color/xyz"
249
+ require "color/yiq"
156
250
 
157
- # Provides a thin veneer over the Color module to make it seem like this
158
- # is Color 0.1.0 (a class) and not Color 1.4 (a module). This
159
- # "constructor" will be removed in the future.
160
- #
161
- # mode = :hsl:: +values+ must be an array of [ hue deg, sat %, lum % ].
162
- # A Color::HSL object will be created.
163
- # mode = :rgb:: +values+ will either be an HTML-style colour string or
164
- # an array of [ red, green, blue ] (range 0 .. 255). A
165
- # Color::RGB object will be created.
166
- # mode = :cmyk:: +values+ must be an array of [ cyan %, magenta %, yellow
167
- # %, black % ]. A Color::CMYK object will be created.
168
- def new(values, mode = :rgb)
169
- warn "Color.new has been deprecated. Use Color::#{mode.to_s.upcase}.new instead."
170
- color = case mode
171
- when :hsl
172
- Color::HSL.new(*values)
173
- when :rgb
174
- values = [ values ].flatten
175
- if values.size == 1
176
- Color::RGB.from_html(*values)
177
- else
178
- Color::RGB.new(*values)
179
- end
180
- when :cmyk
181
- Color::CMYK.new(*values)
182
- end
183
- color.to_hsl
184
- end
185
- end
251
+ require "color/version"