color 1.8 → 2.0.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 (53) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +314 -0
  3. data/CODE_OF_CONDUCT.md +128 -0
  4. data/CONTRIBUTING.md +84 -0
  5. data/CONTRIBUTORS.md +11 -0
  6. data/LICENCE.md +50 -0
  7. data/Manifest.txt +12 -23
  8. data/README.md +54 -0
  9. data/Rakefile +72 -61
  10. data/SECURITY.md +39 -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 +211 -161
  16. data/lib/color/rgb.rb +527 -551
  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 +204 -142
  21. data/licences/dco.txt +34 -0
  22. data/test/fixtures/cielab.json +444 -0
  23. data/test/minitest_helper.rb +20 -4
  24. data/test/test_cmyk.rb +49 -72
  25. data/test/test_color.rb +58 -112
  26. data/test/test_grayscale.rb +35 -57
  27. data/test/test_hsl.rb +71 -77
  28. data/test/test_rgb.rb +195 -267
  29. data/test/test_yiq.rb +12 -30
  30. metadata +104 -121
  31. data/.autotest +0 -5
  32. data/.coveralls.yml +0 -2
  33. data/.gemtest +0 -0
  34. data/.hoerc +0 -2
  35. data/.minitest.rb +0 -2
  36. data/.travis.yml +0 -41
  37. data/Code-of-Conduct.rdoc +0 -41
  38. data/Contributing.rdoc +0 -62
  39. data/Gemfile +0 -9
  40. data/History.rdoc +0 -194
  41. data/Licence.rdoc +0 -27
  42. data/README.rdoc +0 -52
  43. data/lib/color/css.rb +0 -7
  44. data/lib/color/palette/adobecolor.rb +0 -260
  45. data/lib/color/palette/gimp.rb +0 -104
  46. data/lib/color/palette/monocontrast.rb +0 -164
  47. data/lib/color/palette.rb +0 -4
  48. data/lib/color/rgb/contrast.rb +0 -57
  49. data/lib/color/rgb/metallic.rb +0 -28
  50. data/test/test_adobecolor.rb +0 -405
  51. data/test/test_css.rb +0 -19
  52. data/test/test_gimp.rb +0 -87
  53. data/test/test_monocontrast.rb +0 -130
data/History.rdoc DELETED
@@ -1,194 +0,0 @@
1
- == 1.8 / 2015-10-26
2
-
3
- * 1 minor enhancement:
4
-
5
- * Add an optional +alpha+ parameter to all #css_rgba calls. Thanks to Luke
6
- Bennellick (@bennell) and Alexander Popov (@AlexWayfer) for independently
7
- implemented submissions. Merged from #15.
8
-
9
- * 2 minor bug fixes:
10
-
11
- * Improve constant detection to prevent incorrectly identified name
12
- collisions with various other libraries such as Azure deployment tools.
13
- Based on work by Matthew Draper (@matthewd) in #24.
14
-
15
- * Prevent Color.equivalent? comparisons from using non-Color types for
16
- comparison. Fix provided by Benjamin Guest (@bguest) in #18.
17
-
18
- * 1 governance change
19
-
20
- * This project now has a {Code of Conduct}[Code-of-Conduct_rdoc.html]
21
-
22
- == 1.7.1 / 2014-06-12
23
-
24
- * Minor enhancements:
25
-
26
- * Renamed Color::RGB::BeccaPurple to Color::RGB::RebeccaPurple as stipulated
27
- by Eric Meyer. http://meyerweb.com/eric/thoughts/2014/06/19/rebeccapurple/
28
- For purposes of backwards compatibility, the previous name is still
29
- permitted, but its use is strongly discouraged, and it will be removed in
30
- the Color 2.0 release.
31
-
32
- == 1.7 / 2014-06-12
33
-
34
- * Minor enhancements:
35
- * Added Color::RGB::BeccaPurple (#663399) in honour of Rebecca Meyer, the
36
- daughter of Eric Meyer, who passed away on the 7th of June, 2014. Her
37
- favourite colour was purple. #663399becca
38
- https://twitter.com/meyerweb/status/476089708674428929
39
- http://www.zeldman.com/2014/06/10/the-color-purple/
40
- http://discourse.specifiction.org/t/name-663399-becca-purple-in-css4-color/225
41
-
42
- * Bug fixes:
43
- * Changed the homepage in the gem to point to github.com instead of
44
- RubyForge, which has been shut down. Fixes #10, reported by @voxik.
45
-
46
- == 1.6 / 2014-05-19
47
-
48
- * Major enhancements:
49
- * Aaron Hill (@armahillo) implemented the CIE Delta E 94 method by which an
50
- RGB colour can be asked for the closest matching colour from a list of
51
- provided colours. Fixes #5.
52
- * To implement #closest_match and #delta_e94, conversion methods for sRGB to
53
- XYZ and XYZ to L*a*b* space were implemented. These should be considered
54
- experimental.
55
-
56
- * Tooling fixes:
57
- * Ensured that the gem manifest was up-to-date. Fixes #4 reported by @boutil.
58
- Thanks!
59
- * Fixed problems with Travis builds. Note that Ruby 1.9.2 is no longer
60
- tested. Rubinius remains in a “failure-tolerated” mode.
61
- * Color 1.6 is, barring security patches, the last release of Color that will
62
- support Ruby 1.8.
63
-
64
- == 1.5.1 / 2014-01-28
65
-
66
- * color 1.5 was a yanked release.
67
-
68
- * Minor enhancements:
69
- * Added new methods to Color::RGB to make it so that the default defined
70
- colours can be looked up by hex, name, or both.
71
- * Added a method to Color::RGB to extract colours from text by hex, name, or
72
- both.
73
- * Added new common methods for colour names. Converted colours do not retain
74
- names.
75
- * Restructured color comparisons to use protocols instead of custom
76
- implementations. This makes it easier to implement new colour classes. To
77
- make this work, color classes should +include+ Color only need to implement
78
- <tt>#coerce(other)</tt>, <tt>#to_a</tt>, and supported conversion methods
79
- (e.g., <tt>#to_rgb</tt>).
80
- * Added @daveheitzman’s initial implementation of an RGB contrast method as
81
- an extension file: <tt>require 'color/rgb/contrast'</tt>. This method and
82
- the value it returns should be considered experimental; it requires further
83
- examination to ensure that the results produced are consistent with the
84
- contrast comparisons used in Color::Palette::MonoContrast.
85
- * Reducing duplicated code.
86
-
87
- * Bug Fixes:
88
- * Moved +lib/color/rgb-colors.rb+ to +lib/color/rgb/colors.rb+. This should
89
- have no impact in general.
90
- * Improved the way that named colors are specified internally.
91
- * Fixed bugs with Ruby 1.8.7 that may have been introduced in color 1.4.2.
92
-
93
- * Tooling Changes:
94
- * Added simplecov for test coverage analysis.
95
- * Modernized Travis CI support.
96
-
97
- == 1.4.2 / 2013-06-30
98
-
99
- Maintenance:
100
- * Modernized Hoe installation of Color, removing some dependencies.
101
- * Switched to Minitest.
102
- * Turned on Travis CI.
103
- * Started using Code Climate.
104
- * Small code formatting cleanup that touched pretty much every file.
105
-
106
- == 1.4.1 / 2010-02-03
107
-
108
- * Imported to github.
109
- * Converted to Hoe 2.5 spec format.
110
-
111
- == 1.4.0 / 2007-02-11
112
-
113
- * Merged Austin Ziegler's color-tools library (previously part of the Ruby
114
- PDF Tools project) with Matt Lyon's color library.
115
- - The HSL implementation from the Color class has been merged into
116
- Color::HSL. Color is a module the way it was for color-tools.
117
- - A thin veneer has been written to allow Color::new to return a Color::HSL
118
- instance; Color::HSL supports as many methods as possible that were
119
- previously supported by the Color class.
120
- - Values that were previously rounded by Color are no longer rounded;
121
- fractional values matter.
122
- * Converted to hoe for project management.
123
- * Moved to the next step of deprecating Color::Name values; printing a
124
- warning for each use (see the history for color-tools 1.3.0).
125
- * Print a warning on the access of either VERSION or COLOR_TOOLS_VERSION; the
126
- version constant is now COLOR_VERSION.
127
- * Added humanized versions of accessors (e.g., CMYK colours now have both #cyan
128
- and #c to access the cyan component of the colour; #cyan provides the value
129
- as a percentage).
130
- * Added CSS3 formatters for RGB, RGBA, HSL, and HSLA outputs. Note that the
131
- Color library does not yet have a way of setting alpha opacity, so the
132
- output for RGBA and HSLA are at full alpha opacity (1.0). The values are
133
- output with two decimal places.
134
- * Applied a patch to provide simple arithmetic colour addition and subtraction
135
- to Color::GrayScale and Color::RGB. The patch was contributed by Jeremy
136
- Hinegardner <jeremy@hinegardner.org>. This patch also provides the ability to
137
- return the maximum RGB value as a grayscale colour.
138
- * Fixed two problems reported by Jean Krohn <jb.krohn@free.fr> against
139
- color-tools relating to RGB-to-HSL and HSL-to-RGB conversion. (Color and
140
- color-tools use the same formulas, but the ordering of the calculations is
141
- slightly different with Color and did not suffer from this problem;
142
- color-tools was more sensitive to floating-point values and precision
143
- errors.)
144
- * Fixed an issue with HSL/RGB conversions reported by Adam Johnson
145
- <adam.sven.johnson@gmail.com>.
146
- * Added an Adobe Color swatch (Photoshop) palette reader,
147
- Color::Palette::AdobeColor (for .aco files only).
148
-
149
- == Color 0.1.0 / 2006-08-05
150
-
151
- * Added HSL (degree, percent, percent) interface.
152
- * Removed RGB instance variable; color is managed internally as HSL floating
153
- point.
154
- * Tests!
155
-
156
- == color-tools 1.3.0
157
-
158
- * Added new metallic colours suggested by Jim Freeze <jfn@freeze.org>. These
159
- are in the namespace Color::Metallic.
160
- * Colours that were defined in the Color namespace (e.g., Color::Red,
161
- Color::AliceBlue) are now defined in Color::RGB (e.g., Color::RGB::Red,
162
- Color::RGB::AliceBlue). They are added back to the Color namespace on the
163
- first use of the old colours and a warning is printed. In version 1.4, this
164
- warning will be printed on every use of the old colours. In version 1.5,
165
- the backwards compatible support for colours like Color::Red will be
166
- removed completely.
167
- * Added the Color::CSS module, color/css or Color::CSS that provides a name
168
- lookup of Color::RGB-namespaced constants with Color::CSS[name]. Most of
169
- these colours (which are mirrored from the Color::RGB default colours) are
170
- only "officially" recognised under the CSS3 colour module or SVG.
171
- * Added the Color::HSL colour space and some helper utilities to Color::RGB
172
- for colour manipulation using the HSL value.
173
- * Controlled internal value replacement to be between 0 and 1 for all
174
- colours.
175
- * Updated Color::Palette::Gimp to more meaningfully deal with duplicate named
176
- colours. Named colours now return an array of colours.
177
- * Indicated the plans for some methods and constants out to color-tools 2.0.
178
- * Added unit tests and fixed a number of hidden bugs because of them.
179
-
180
- == color-tools 1.2.0
181
- * Changed installer from a custom-written install.rb to setup.rb
182
- 3.3.1-modified.
183
- * Added Color::GreyScale (or Color::GrayScale).
184
- * Added Color::YIQ. This colour definition is incomplete; it does not have
185
- conversions from YIQ to other colour spaces.
186
-
187
- == color-tools 1.1.0
188
-
189
- * Added color/palette/gimp to support the reading and use of GIMP color
190
- palettes.
191
-
192
- == color-tools 1.0.0
193
-
194
- * Initial release.
data/Licence.rdoc DELETED
@@ -1,27 +0,0 @@
1
- == Licence
2
-
3
- This software is available under an MIT-style licence.
4
-
5
- * Copyright 2005–2014 Austin Ziegler, Matt Lyon, and other contributors
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining a copy of
8
- this software and associated documentation files (the "Software"), to deal in
9
- the Software without restriction, including without limitation the rights to
10
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11
- of the Software, and to permit persons to whom the Software is furnished to do
12
- so, subject to the following conditions:
13
-
14
- * The names of its contributors may not be used to endorse or promote
15
- products derived from this software without specific prior written
16
- permission.
17
-
18
- The above copyright notice and this permission notice shall be included in all
19
- copies or substantial portions of the Software.
20
-
21
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
- SOFTWARE.
data/README.rdoc DELETED
@@ -1,52 +0,0 @@
1
- = Color
2
-
3
- home :: https://github.com/halostatue/color
4
- code :: https://github.com/halostatue/color
5
- bugs :: https://github.com/halostatue/color/issues
6
- rdoc :: http://rubydoc.info/github/halostatue/color
7
- continuous integration :: {<img src="https://travis-ci.org/halostatue/color.png" />}[https://travis-ci.org/halostatue/color]
8
- test coverage :: {<img src="https://coveralls.io/repos/halostatue/color/badge.png" alt="Coverage Status" />}[https://coveralls.io/r/halostatue/color]
9
-
10
- == Description
11
-
12
- Color is a Ruby library to provide basic RGB, CMYK, HSL, and other colourspace
13
- manipulation support to applications that require it. It also provides 152
14
- named RGB colours (184 with spelling variations) that are commonly supported in
15
- HTML, SVG, and X11 applications. A technique for generating monochromatic
16
- contrasting palettes is also included.
17
-
18
- The Color library performs purely mathematical manipulation of the colours
19
- based on colour theory without reference to colour profiles (such as sRGB or
20
- Adobe RGB). For most purposes, when working with RGB and HSL colour spaces,
21
- this won't matter. Absolute colour spaces (like CIE L*a*b* and XYZ) and cannot
22
- be reliably converted to relative colour spaces (like RGB) without colour
23
- profiles.
24
-
25
- Color 1.8 adds an alpha parameter to all <tt>#css_rgba</tt> calls, fixes a bug
26
- exposed by new constant lookup semantics in Ruby 2, and ensures that
27
- <tt>Color.equivalent?</tt> can only be called on Color instances.
28
-
29
- Barring bugs introduced in this release, this (really) is the last version of
30
- color that supports Ruby 1.8, so make sure that your gem specification is set
31
- properly (to <tt>~> 1.8</tt>) if that matters for your application. This
32
- version will no longer be supported one year after the release of color 2.0.
33
-
34
- == History
35
-
36
- Color is the result of a project merge between color.rb 0.1.0 by
37
- Matt Lyon and color-tools 1.3 by Austin Ziegler. Please see History.txt for
38
- details on the changes this merge brings.
39
-
40
- Color::Palette::MonoContrast was developed based on techniques described by
41
- Andy “Malarkey” Clarke[1], implemented in JavaScript by Steve G. Chipman at
42
- SlayerOffice[2] and by Patrick Fitzgerald of BarelyFitz[3] in PHP.
43
-
44
- [1] http://www.stuffandnonsense.co.uk/archives/creating_colour_palettes.html
45
- [2] http://slayeroffice.com/tools/color_palette/
46
- [3] http://www.barelyfitz.com/projects/csscolor/
47
-
48
- :include: Code-of-Conduct.rdoc
49
-
50
- :include: Contributing.rdoc
51
-
52
- :include: Licence.rdoc
data/lib/color/css.rb DELETED
@@ -1,7 +0,0 @@
1
- # This namespace contains some CSS colour names.
2
- module Color::CSS
3
- # Returns the RGB colour for name or +nil+ if the name is not valid.
4
- def self.[](name)
5
- Color::RGB.by_name(name) { nil }
6
- end
7
- end
@@ -1,260 +0,0 @@
1
- require 'color/palette'
2
-
3
- # A class that can read an Adobe Color palette file (used for Photoshop
4
- # swatches) and provide a Hash-like interface to the contents. Not all
5
- # colour formats in ACO files are supported. Based largely off the
6
- # information found by Larry Tesler[http://www.nomodes.com/aco.html].
7
- #
8
- # Not all Adobe Color files have named colours; all named entries are
9
- # returned as an array.
10
- #
11
- # pal = Color::Palette::AdobeColor.from_file(my_aco_palette)
12
- # pal[0] => Color::RGB<...>
13
- # pal["white"] => [ Color::RGB<...> ]
14
- # pal["unknown"] => [ Color::RGB<...>, Color::RGB<...>, ... ]
15
- #
16
- # AdobeColor palettes are always indexable by insertion order (an integer
17
- # key).
18
- #
19
- # Version 2 palettes use UTF-16 colour names.
20
- class Color::Palette::AdobeColor
21
- include Enumerable
22
-
23
- class << self
24
- # Create an AdobeColor palette object from the named file.
25
- def from_file(filename)
26
- File.open(filename, "rb") { |io| Color::Palette::AdobeColor.from_io(io) }
27
- end
28
-
29
- # Create an AdobeColor palette object from the provided IO.
30
- def from_io(io)
31
- Color::Palette::AdobeColor.new(io.read)
32
- end
33
- end
34
-
35
- # Returns statistics about the nature of the colours loaded.
36
- attr_reader :statistics
37
- # Contains the "lost" colours in the palette. These colours could not be
38
- # properly loaded (e.g., L*a*b* is not supported by Color, so it is
39
- # "lost") or are not understood by the algorithms.
40
- attr_reader :lost
41
-
42
- # Use this to convert the unsigned word to the signed word, if necessary.
43
- UwToSw = proc { |n| (n >= (2 ** 16)) ? n - (2 ** 32) : n } #:nodoc:
44
-
45
- # Create a new AdobeColor palette from the palette file as a string.
46
- def initialize(palette)
47
- @colors = []
48
- @names = {}
49
- @statistics = Hash.new(0)
50
- @lost = []
51
- @order = []
52
- @version = nil
53
-
54
- class << palette
55
- def readwords(count = 1)
56
- @offset ||= 0
57
- raise IndexError if @offset >= self.size
58
- val = self[@offset, count * 2]
59
- raise IndexError if val.nil? or val.size < (count * 2)
60
- val = val.unpack("n" * count)
61
- @offset += count * 2
62
- val
63
- end
64
-
65
- def readutf16(count = 1)
66
- @offset ||= 0
67
- raise IndexError if @offset >= self.size
68
- val = self[@offset, count * 2]
69
- raise IndexError if val.nil? or val.size < (count * 2)
70
- @offset += count * 2
71
- val
72
- end
73
- end
74
-
75
- @version, count = palette.readwords 2
76
-
77
- raise "Unknown AdobeColor palette version #@version." unless @version.between?(1, 2)
78
-
79
- count.times do
80
- space, w, x, y, z = palette.readwords 5
81
- name = nil
82
- if @version == 2
83
- raise IndexError unless palette.readwords == [ 0 ]
84
- len = palette.readwords
85
- name = palette.readutf16(len[0] - 1)
86
- raise IndexError unless palette.readwords == [ 0 ]
87
- end
88
-
89
- color = case space
90
- when 0 then # RGB
91
- @statistics[:rgb] += 1
92
-
93
- Color::RGB.new(w / 256, x / 256, y / 256)
94
- when 1 then # HS[BV] -- Convert to RGB
95
- @statistics[:hsb] += 1
96
-
97
- h = w / 65535.0
98
- s = x / 65535.0
99
- v = y / 65535.0
100
-
101
- if defined?(Color::HSB)
102
- Color::HSB.from_fraction(h, s, v)
103
- else
104
- @statistics[:converted] += 1
105
- if Color.near_zero_or_less?(s)
106
- Color::RGB.from_fraction(v, v, v)
107
- else
108
- if Color.near_one_or_more?(h)
109
- vh = 0
110
- else
111
- vh = h * 6.0
112
- end
113
-
114
- vi = vh.floor
115
- v1 = v.to_f * (1 - s.to_f)
116
- v2 = v.to_f * (1 - s.to_f * (vh - vi))
117
- v3 = v.to_f * (1 - s.to_f * (1 - (vh - vi)))
118
-
119
- case vi
120
- when 0 then Color::RGB.from_fraction(v, v3, v1)
121
- when 1 then Color::RGB.from_fraction(v2, v, v1)
122
- when 2 then Color::RGB.from_fraction(v1, v, v3)
123
- when 3 then Color::RGB.from_fraction(v1, v2, v)
124
- when 4 then Color::RGB.from_fraction(v3, v1, v)
125
- else Color::RGB.from_fraction(v, v1, v2)
126
- end
127
- end
128
- end
129
- when 2 then # CMYK
130
- @statistics[:cmyk] += 1
131
- Color::CMYK.from_percent(100 - (w / 655.35),
132
- 100 - (x / 655.35),
133
- 100 - (y / 655.35),
134
- 100 - (z / 655.35))
135
- when 7 then # L*a*b*
136
- @statistics[:lab] += 1
137
-
138
- l = [w, 10000].min / 100.0
139
- a = [[-12800, UwToSw[x]].max, 12700].min / 100.0
140
- b = [[-12800, UwToSw[x]].max, 12700].min / 100.0
141
-
142
- if defined? Color::Lab
143
- Color::Lab.new(l, a, b)
144
- else
145
- [ space, w, x, y, z ]
146
- end
147
- when 8 then # Grayscale
148
- @statistics[:gray] += 1
149
-
150
- g = [w, 10000].min / 100.0
151
- Color::GrayScale.new(g)
152
- when 9 then # Wide CMYK
153
- @statistics[:wcmyk] += 1
154
-
155
- c = [w, 10000].min / 100.0
156
- m = [x, 10000].min / 100.0
157
- y = [y, 10000].min / 100.0
158
- k = [z, 10000].min / 100.0
159
- Color::CMYK.from_percent(c, m, y, k)
160
- else
161
- @statistics[space] += 1
162
- [ space, w, x, y, z ]
163
- end
164
-
165
- @order << [ color, name ]
166
-
167
- if color.kind_of? Array
168
- @lost << color
169
- else
170
- @colors << color
171
-
172
- if name
173
- @names[name] ||= []
174
- @names[name] << color
175
- end
176
- end
177
- end
178
- end
179
-
180
- # Provides the colour or colours at the provided selectors.
181
- def values_at(*selectors)
182
- @colors.values_at(*selectors)
183
- end
184
-
185
- # If a Numeric +key+ is provided, the single colour value at that position
186
- # will be returned. If a String +key+ is provided, the colour set (an
187
- # array) for that colour name will be returned.
188
- def [](key)
189
- if key.kind_of?(Numeric)
190
- @colors[key]
191
- else
192
- @names[key]
193
- end
194
- end
195
-
196
- # Loops through each colour.
197
- def each
198
- @colors.each { |el| yield el }
199
- end
200
-
201
- # Loops through each named colour set.
202
- def each_name #:yields color_name, color_set:#
203
- @names.each { |color_name, color_set| yield color_name, color_set }
204
- end
205
-
206
- def size
207
- @colors.size
208
- end
209
-
210
- attr_reader :version
211
-
212
- def to_aco(version = @version) #:nodoc:
213
- res = ""
214
-
215
- res << [ version, @order.size ].pack("nn")
216
-
217
- @order.each do |cnpair|
218
- color, name = *cnpair
219
-
220
- # Note: HSB and CMYK formats are lost by the conversions performed on
221
- # import. They are turned into RGB and WCMYK, respectively.
222
-
223
- cstr = case color
224
- when Array
225
- color
226
- when Color::RGB
227
- r = [(color.red * 256).round, 65535].min
228
- g = [(color.green * 256).round, 65535].min
229
- b = [(color.blue * 256).round, 65535].min
230
- [ 0, r, g, b, 0 ]
231
- when Color::GrayScale
232
- g = [(color.gray * 100).round, 10000].min
233
- [ 8, g, 0, 0, 0 ]
234
- when Color::CMYK
235
- c = [(color.cyan * 100).round, 10000].min
236
- m = [(color.magenta * 100).round, 10000].min
237
- y = [(color.yellow * 100).round, 10000].min
238
- k = [(color.black * 100).round, 10000].min
239
- [ 9, c, m, y, k ]
240
- end
241
- cstr = cstr.pack("nnnnn")
242
-
243
- nstr = ""
244
-
245
- if version == 2
246
- if (name.size / 2 * 2) == name.size # only where s[0] == byte!
247
- nstr << [ 0, (name.size / 2) + 1 ].pack("nn")
248
- nstr << name
249
- nstr << [ 0 ].pack("n")
250
- else
251
- nstr << [ 0, 1, 0 ].pack("nnn")
252
- end
253
- end
254
-
255
- res << cstr << nstr
256
- end
257
-
258
- res
259
- end
260
- end
@@ -1,104 +0,0 @@
1
- require 'color/palette'
2
-
3
- # A class that can read a GIMP (GNU Image Manipulation Program) palette file
4
- # and provide a Hash-like interface to the contents. GIMP colour palettes
5
- # are RGB values only.
6
- #
7
- # Because two or more entries in a GIMP palette may have the same name, all
8
- # named entries are returned as an array.
9
- #
10
- # pal = Color::Palette::Gimp.from_file(my_gimp_palette)
11
- # pal[0] => Color::RGB<...>
12
- # pal["white"] => [ Color::RGB<...> ]
13
- # pal["unknown"] => [ Color::RGB<...>, Color::RGB<...>, ... ]
14
- #
15
- # GIMP Palettes are always indexable by insertion order (an integer key).
16
- class Color::Palette::Gimp
17
- include Enumerable
18
-
19
- class << self
20
- # Create a GIMP palette object from the named file.
21
- def from_file(filename)
22
- File.open(filename, "rb") { |io| Color::Palette::Gimp.from_io(io) }
23
- end
24
-
25
- # Create a GIMP palette object from the provided IO.
26
- def from_io(io)
27
- Color::Palette::Gimp.new(io.read)
28
- end
29
- end
30
-
31
- # Create a new GIMP palette from the palette file as a string.
32
- def initialize(palette)
33
- @colors = []
34
- @names = {}
35
- @valid = false
36
- @name = "(unnamed)"
37
-
38
- palette.split($/).each do |line|
39
- line.chomp!
40
- line.gsub!(/\s*#.*\Z/, '')
41
-
42
- next if line.empty?
43
-
44
- if line =~ /\AGIMP Palette\Z/
45
- @valid = true
46
- next
47
- end
48
-
49
- info = /(\w+):\s(.*$)/.match(line)
50
- if info
51
- @name = info.captures[1] if info.captures[0] =~ /name/i
52
- next
53
- end
54
-
55
- line.gsub!(/^\s+/, '')
56
- data = line.split(/\s+/, 4)
57
- name = data.pop.strip
58
- data.map! { |el| el.to_i }
59
-
60
- color = Color::RGB.new(*data)
61
-
62
- @colors << color
63
- @names[name] ||= []
64
- @names[name] << color
65
- end
66
- end
67
-
68
- # Provides the colour or colours at the provided selectors.
69
- def values_at(*selectors)
70
- @colors.values_at(*selectors)
71
- end
72
-
73
- # If a Numeric +key+ is provided, the single colour value at that position
74
- # will be returned. If a String +key+ is provided, the colour set (an
75
- # array) for that colour name will be returned.
76
- def [](key)
77
- if key.kind_of?(Numeric)
78
- @colors[key]
79
- else
80
- @names[key]
81
- end
82
- end
83
-
84
- # Loops through each colour.
85
- def each
86
- @colors.each { |el| yield el }
87
- end
88
-
89
- # Loops through each named colour set.
90
- def each_name #:yields color_name, color_set:#
91
- @names.each { |color_name, color_set| yield color_name, color_set }
92
- end
93
-
94
- # Returns true if this is believed to be a valid GIMP palette.
95
- def valid?
96
- @valid
97
- end
98
-
99
- def size
100
- @colors.size
101
- end
102
-
103
- attr_reader :name
104
- end