rmthemegen 0.0.23 → 0.0.25

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,221 @@
1
+ #--
2
+ # Color
3
+ # Colour management with Ruby
4
+ # http://rubyforge.org/projects/color
5
+ # Version 1.4.1
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 - 2010 Austin Ziegler and Matt Lyon
11
+ #++
12
+
13
+ # An HSL colour object. Internally, the hue (#h), saturation (#s), and
14
+ # luminosity/lightness (#l) values are dealt with as fractional values in
15
+ # the range 0..1.
16
+ class Color::HSL
17
+ class << self
18
+ # Creates an HSL colour object from fractional values 0..1.
19
+ def from_fraction(h = 0.0, s = 0.0, l = 0.0)
20
+ colour = Color::HSL.new
21
+ colour.h = h
22
+ colour.s = s
23
+ colour.l = l
24
+ colour
25
+ end
26
+ end
27
+
28
+ # Compares the other colour to this one. The other colour will be
29
+ # converted to HSL before comparison, so the comparison between a HSL
30
+ # colour and a non-HSL colour will be approximate and based on the other
31
+ # colour's #to_hsl conversion. If there is no #to_hsl conversion, this
32
+ # will raise an exception. This will report that two HSL values are
33
+ # equivalent if all component values are within Color::COLOR_TOLERANCE of
34
+ # each other.
35
+ def ==(other)
36
+ other = other.to_hsl
37
+ other.kind_of?(Color::HSL) and
38
+ ((@h - other.h).abs <= Color::COLOR_TOLERANCE) and
39
+ ((@s - other.s).abs <= Color::COLOR_TOLERANCE) and
40
+ ((@l - other.l).abs <= Color::COLOR_TOLERANCE)
41
+ end
42
+
43
+ # Creates an HSL colour object from the standard values of degrees and
44
+ # percentages (e.g., 145 deg, 30%, 50%).
45
+ def initialize(h = 0, s = 0, l = 0)
46
+ @h = h / 360.0
47
+ @s = s / 100.0
48
+ @l = l / 100.0
49
+ end
50
+
51
+ # Present the colour as an HTML/CSS colour string.
52
+ def html
53
+ to_rgb.html
54
+ end
55
+
56
+ # Present the colour as an RGB HTML/CSS colour string (e.g., "rgb(0%, 50%,
57
+ # 100%)"). Note that this will perform a #to_rgb operation using the
58
+ # default conversion formula.
59
+ def css_rgb
60
+ to_rgb.css_rgb
61
+ end
62
+
63
+ # Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g.,
64
+ # "rgb(0%, 50%, 100%, 1)"). Note that this will perform a #to_rgb
65
+ # operation using the default conversion formula.
66
+ def css_rgba
67
+ to_rgb.css_rgba
68
+ end
69
+
70
+ # Present the colour as an HSL HTML/CSS colour string (e.g., "hsl(180,
71
+ # 25%, 35%)").
72
+ def css_hsl
73
+ "hsl(%3.2f, %3.2f%%, %3.2f%%)" % [ hue, saturation, luminosity ]
74
+ end
75
+
76
+ # Present the colour as an HSLA (with alpha) HTML/CSS colour string (e.g.,
77
+ # "hsla(180, 25%, 35%, 1)").
78
+ def css_hsla
79
+ "hsla(%3.2f, %3.2f%%, %3.2f%%, %3.2f)" % [ hue, saturation, luminosity, 1 ]
80
+ end
81
+
82
+ # Converting to HSL as adapted from Foley and Van-Dam from
83
+ # http://www.bobpowell.net/RGBHSB.htm.
84
+ #
85
+ # NOTE:
86
+ # * If the colour's luminosity is near zero, the colour is always black.
87
+ # * If the colour's luminosity is near one, the colour is always white.
88
+ # * If the colour's saturation is near zero, the colour is always a shade
89
+ # of grey and is based only on the luminosity of the colour.
90
+ #
91
+ def to_rgb(ignored = nil)
92
+ return Color::RGB.new if Color.near_zero_or_less?(@l)
93
+ return Color::RGB.new(0xff, 0xff, 0xff) if Color.near_one_or_more?(@l)
94
+ return Color::RGB.from_fraction(@l, @l, @l) if Color.near_zero?(@s)
95
+
96
+ # Is the value less than 0.5?
97
+ if Color.near_zero_or_less?(@l - 0.5)
98
+ tmp2 = @l * (1.0 + @s.to_f)
99
+ else
100
+ tmp2 = @l + @s - (@l * @s.to_f)
101
+ end
102
+ tmp1 = 2.0 * @l - tmp2
103
+
104
+ tmp3 = [ @h + (1.0 / 3.0), @h, @h - (1.0 / 3.0) ]
105
+
106
+ rgb = tmp3.map { |hue|
107
+ hue += 1.0 if Color.near_zero_or_less?(hue)
108
+ hue -= 1.0 if Color.near_one_or_more?(hue)
109
+
110
+ if Color.near_zero_or_less?((6.0 * hue) - 1.0)
111
+ tmp1 + ((tmp2 - tmp1) * hue * 6.0)
112
+ elsif Color.near_zero_or_less?((2.0 * hue) - 1.0)
113
+ tmp2
114
+ elsif Color.near_zero_or_less?((3.0 * hue) - 2.0)
115
+ tmp1 + (tmp2 - tmp1) * ((2 / 3.0) - hue) * 6.0
116
+ else
117
+ tmp1
118
+ end
119
+ }
120
+
121
+ Color::RGB.from_fraction(*rgb)
122
+ end
123
+
124
+ # Converts to RGB then YIQ.
125
+ def to_yiq
126
+ to_rgb.to_yiq
127
+ end
128
+
129
+ # Converts to RGB then CMYK.
130
+ def to_cmyk
131
+ to_rgb.to_cmyk
132
+ end
133
+
134
+ # Returns the luminosity (#l) of the colour.
135
+ def brightness
136
+ @l
137
+ end
138
+ def to_greyscale
139
+ Color::GrayScale.from_fraction(@l)
140
+ end
141
+ alias to_grayscale to_greyscale
142
+
143
+ # Returns the hue of the colour in degrees.
144
+ def hue
145
+ @h * 360.0
146
+ end
147
+ # Returns the hue of the colour in the range 0.0 .. 1.0.
148
+ def h
149
+ @h
150
+ end
151
+ # Sets the hue of the colour in degrees. Colour is perceived as a wheel,
152
+ # so values should be set properly even with negative degree values.
153
+ def hue=(hh)
154
+ hh = hh / 360.0
155
+
156
+ hh += 1.0 if hh < 0.0
157
+ hh -= 1.0 if hh > 1.0
158
+
159
+ @h = Color.normalize(hh)
160
+ end
161
+ # Sets the hue of the colour in the range 0.0 .. 1.0.
162
+ def h=(hh)
163
+ @h = Color.normalize(hh)
164
+ end
165
+ # Returns the percentage of saturation of the colour.
166
+ def saturation
167
+ @s * 100.0
168
+ end
169
+ # Returns the saturation of the colour in the range 0.0 .. 1.0.
170
+ def s
171
+ @s
172
+ end
173
+ # Sets the percentage of saturation of the colour.
174
+ def saturation=(ss)
175
+ @s = Color.normalize(ss / 100.0)
176
+ end
177
+ # Sets the saturation of the colour in the ragne 0.0 .. 1.0.
178
+ def s=(ss)
179
+ @s = Color.normalize(ss)
180
+ end
181
+
182
+ # Returns the percentage of luminosity of the colour.
183
+ def luminosity
184
+ @l * 100.0
185
+ end
186
+ alias lightness luminosity
187
+ # Returns the luminosity of the colour in the range 0.0 .. 1.0.
188
+ def l
189
+ @l
190
+ end
191
+ # Sets the percentage of luminosity of the colour.
192
+ def luminosity=(ll)
193
+ @l = Color.normalize(ll / 100.0)
194
+ end
195
+ alias lightness= luminosity= ;
196
+ # Sets the luminosity of the colour in the ragne 0.0 .. 1.0.
197
+ def l=(ll)
198
+ @l = Color.normalize(ll)
199
+ end
200
+
201
+ def to_hsl
202
+ self
203
+ end
204
+
205
+ def inspect
206
+ "HSL [%.2f deg, %.2f%%, %.2f%%]" % [ hue, saturation, luminosity ]
207
+ end
208
+
209
+ # Mix the mask colour (which will be converted to an HSL colour) with the
210
+ # current colour at the stated mix percentage as a decimal value.
211
+ #
212
+ # NOTE:: This differs from Color::RGB#mix_with.
213
+ def mix_with(color, mix_percent = 0.5)
214
+ color = color.to_hsl
215
+ _h = ((color.h - self.h) * mix_percent) + self.h
216
+ _s = ((color.s - self.s) * mix_percent) + self.s
217
+ _l = ((color.l - self.l) * mix_percent) + self.l
218
+
219
+ self.class.from_fraction(_h, _s, _l)
220
+ end
221
+ end
@@ -0,0 +1,272 @@
1
+ #--
2
+ # Color
3
+ # Colour management with Ruby
4
+ # http://rubyforge.org/projects/color
5
+ # Version 1.4.1
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 - 2010 Austin Ziegler and Matt Lyon
11
+ #++
12
+
13
+ require 'color/palette'
14
+
15
+ # A class that can read an Adobe Color palette file (used for Photoshop
16
+ # swatches) and provide a Hash-like interface to the contents. Not all
17
+ # colour formats in ACO files are supported. Based largely off the
18
+ # information found by Larry Tesler[http://www.nomodes.com/aco.html].
19
+ #
20
+ # Not all Adobe Color files have named colours; all named entries are
21
+ # returned as an array.
22
+ #
23
+ # pal = Color::Palette::AdobeColor.from_file(my_aco_palette)
24
+ # pal[0] => Color::RGB<...>
25
+ # pal["white"] => [ Color::RGB<...> ]
26
+ # pal["unknown"] => [ Color::RGB<...>, Color::RGB<...>, ... ]
27
+ #
28
+ # AdobeColor palettes are always indexable by insertion order (an integer
29
+ # key).
30
+ #
31
+ # Version 2 palettes use UTF-16 colour names.
32
+ class Color::Palette::AdobeColor
33
+ include Enumerable
34
+
35
+ class << self
36
+ # Create an AdobeColor palette object from the named file.
37
+ def from_file(filename)
38
+ File.open(filename, "rb") { |io| Color::Palette::AdobeColor.from_io(io) }
39
+ end
40
+
41
+ # Create an AdobeColor palette object from the provided IO.
42
+ def from_io(io)
43
+ Color::Palette::AdobeColor.new(io.read)
44
+ end
45
+ end
46
+
47
+ # Returns statistics about the nature of the colours loaded.
48
+ attr_reader :statistics
49
+ # Contains the "lost" colours in the palette. These colours could not be
50
+ # properly loaded (e.g., L*a*b* is not supported by Color, so it is
51
+ # "lost") or are not understood by the algorithms.
52
+ attr_reader :lost
53
+
54
+ # Use this to convert the unsigned word to the signed word, if necessary.
55
+ UwToSw = proc { |n| (n >= (2 ** 16)) ? n - (2 ** 32) : n } #:nodoc:
56
+
57
+ # Create a new AdobeColor palette from the palette file as a string.
58
+ def initialize(palette)
59
+ @colors = []
60
+ @names = {}
61
+ @statistics = Hash.new(0)
62
+ @lost = []
63
+ @order = []
64
+ @version = nil
65
+
66
+ class << palette
67
+ def readwords(count = 1)
68
+ @offset ||= 0
69
+ raise IndexError if @offset >= self.size
70
+ val = self[@offset, count * 2]
71
+ raise IndexError if val.nil? or val.size < (count * 2)
72
+ val = val.unpack("n" * count)
73
+ @offset += count * 2
74
+ val
75
+ end
76
+
77
+ def readutf16(count = 1)
78
+ @offset ||= 0
79
+ raise IndexError if @offset >= self.size
80
+ val = self[@offset, count * 2]
81
+ raise IndexError if val.nil? or val.size < (count * 2)
82
+ @offset += count * 2
83
+ val
84
+ end
85
+ end
86
+
87
+ @version, count = palette.readwords 2
88
+
89
+ raise "Unknown AdobeColor palette version #@version." unless @version.between?(1, 2)
90
+
91
+ count.times do
92
+ space, w, x, y, z = palette.readwords 5
93
+ name = nil
94
+ if @version == 2
95
+ raise IndexError unless palette.readwords == [ 0 ]
96
+ len = palette.readwords
97
+ name = palette.readutf16(len[0] - 1)
98
+ raise IndexError unless palette.readwords == [ 0 ]
99
+ end
100
+
101
+ color = case space
102
+ when 0 then # RGB
103
+ @statistics[:rgb] += 1
104
+
105
+ Color::RGB.new(w / 256, x / 256, y / 256)
106
+ when 1 then # HS[BV] -- Convert to RGB
107
+ @statistics[:hsb] += 1
108
+
109
+ h = w / 65535.0
110
+ s = x / 65535.0
111
+ v = y / 65535.0
112
+
113
+ if defined?(Color::HSB)
114
+ Color::HSB.from_fraction(h, s, v)
115
+ else
116
+ @statistics[:converted] += 1
117
+ if Color.near_zero_or_less?(s)
118
+ Color::RGB.from_fraction(v, v, v)
119
+ else
120
+ if Color.near_one_or_more?(h)
121
+ vh = 0
122
+ else
123
+ vh = h * 6.0
124
+ end
125
+
126
+ vi = vh.floor
127
+ v1 = v.to_f * (1 - s.to_f)
128
+ v2 = v.to_f * (1 - s.to_f * (vh - vi))
129
+ v3 = v.to_f * (1 - s.to_f * (1 - (vh - vi)))
130
+
131
+ case vi
132
+ when 0 then Color::RGB.from_fraction(v, v3, v1)
133
+ when 1 then Color::RGB.from_fraction(v2, v, v1)
134
+ when 2 then Color::RGB.from_fraction(v1, v, v3)
135
+ when 3 then Color::RGB.from_fraction(v1, v2, v)
136
+ when 4 then Color::RGB.from_fraction(v3, v1, v)
137
+ else Color::RGB.from_fraction(v, v1, v2)
138
+ end
139
+ end
140
+ end
141
+ when 2 then # CMYK
142
+ @statistics[:cmyk] += 1
143
+ Color::CMYK.from_percent(100 - (w / 655.35),
144
+ 100 - (x / 655.35),
145
+ 100 - (y / 655.35),
146
+ 100 - (z / 655.35))
147
+ when 7 then # L*a*b*
148
+ @statistics[:lab] += 1
149
+
150
+ l = [w, 10000].min / 100.0
151
+ a = [[-12800, UwToSw[x]].max, 12700].min / 100.0
152
+ b = [[-12800, UwToSw[x]].max, 12700].min / 100.0
153
+
154
+ if defined? Color::Lab
155
+ Color::Lab.new(l, a, b)
156
+ else
157
+ [ space, w, x, y, z ]
158
+ end
159
+ when 8 then # Grayscale
160
+ @statistics[:gray] += 1
161
+
162
+ g = [w, 10000].min / 100.0
163
+ Color::GrayScale.new(g)
164
+ when 9 then # Wide CMYK
165
+ @statistics[:wcmyk] += 1
166
+
167
+ c = [w, 10000].min / 100.0
168
+ m = [x, 10000].min / 100.0
169
+ y = [y, 10000].min / 100.0
170
+ k = [z, 10000].min / 100.0
171
+ Color::CMYK.from_percent(c, m, y, k)
172
+ else
173
+ @statistics[space] += 1
174
+ [ space, w, x, y, z ]
175
+ end
176
+
177
+ @order << [ color, name ]
178
+
179
+ if color.kind_of? Array
180
+ @lost << color
181
+ else
182
+ @colors << color
183
+
184
+ if name
185
+ @names[name] ||= []
186
+ @names[name] << color
187
+ end
188
+ end
189
+ end
190
+ end
191
+
192
+ # Provides the colour or colours at the provided selectors.
193
+ def values_at(*selectors)
194
+ @colors.values_at(*selectors)
195
+ end
196
+
197
+ # If a Numeric +key+ is provided, the single colour value at that position
198
+ # will be returned. If a String +key+ is provided, the colour set (an
199
+ # array) for that colour name will be returned.
200
+ def [](key)
201
+ if key.kind_of?(Numeric)
202
+ @colors[key]
203
+ else
204
+ @names[key]
205
+ end
206
+ end
207
+
208
+ # Loops through each colour.
209
+ def each
210
+ @colors.each { |el| yield el }
211
+ end
212
+
213
+ # Loops through each named colour set.
214
+ def each_name #:yields color_name, color_set:#
215
+ @names.each { |color_name, color_set| yield color_name, color_set }
216
+ end
217
+
218
+ def size
219
+ @colors.size
220
+ end
221
+
222
+ attr_reader :version
223
+
224
+ def to_aco(version = @version) #:nodoc:
225
+ res = ""
226
+
227
+ res << [ version, @order.size ].pack("nn")
228
+
229
+ @order.each do |cnpair|
230
+ color, name = *cnpair
231
+
232
+ # Note: HSB and CMYK formats are lost by the conversions performed on
233
+ # import. They are turned into RGB and WCMYK, respectively.
234
+
235
+ cstr = case color
236
+ when Array
237
+ color
238
+ when Color::RGB
239
+ r = [(color.red * 256).round, 65535].min
240
+ g = [(color.green * 256).round, 65535].min
241
+ b = [(color.blue * 256).round, 65535].min
242
+ [ 0, r, g, b, 0 ]
243
+ when Color::GrayScale
244
+ g = [(color.gray * 100).round, 10000].min
245
+ [ 8, g, 0, 0, 0 ]
246
+ when Color::CMYK
247
+ c = [(color.cyan * 100).round, 10000].min
248
+ m = [(color.magenta * 100).round, 10000].min
249
+ y = [(color.yellow * 100).round, 10000].min
250
+ k = [(color.black * 100).round, 10000].min
251
+ [ 9, c, m, y, k ]
252
+ end
253
+ cstr = cstr.pack("nnnnn")
254
+
255
+ nstr = ""
256
+
257
+ if version == 2
258
+ if (name.size / 2 * 2) == name.size # only where s[0] == byte!
259
+ nstr << [ 0, (name.size / 2) + 1 ].pack("nn")
260
+ nstr << name
261
+ nstr << [ 0 ].pack("n")
262
+ else
263
+ nstr << [ 0, 1, 0 ].pack("nnn")
264
+ end
265
+ end
266
+
267
+ res << cstr << nstr
268
+ end
269
+
270
+ res
271
+ end
272
+ end
@@ -0,0 +1,116 @@
1
+ #--
2
+ # Color
3
+ # Colour management with Ruby
4
+ # http://rubyforge.org/projects/color
5
+ # Version 1.4.1
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 - 2010 Austin Ziegler and Matt Lyon
11
+ #++
12
+
13
+ require 'color/palette'
14
+
15
+ # A class that can read a GIMP (GNU Image Manipulation Program) palette file
16
+ # and provide a Hash-like interface to the contents. GIMP colour palettes
17
+ # are RGB values only.
18
+ #
19
+ # Because two or more entries in a GIMP palette may have the same name, all
20
+ # named entries are returned as an array.
21
+ #
22
+ # pal = Color::Palette::Gimp.from_file(my_gimp_palette)
23
+ # pal[0] => Color::RGB<...>
24
+ # pal["white"] => [ Color::RGB<...> ]
25
+ # pal["unknown"] => [ Color::RGB<...>, Color::RGB<...>, ... ]
26
+ #
27
+ # GIMP Palettes are always indexable by insertion order (an integer key).
28
+ class Color::Palette::Gimp
29
+ include Enumerable
30
+
31
+ class << self
32
+ # Create a GIMP palette object from the named file.
33
+ def from_file(filename)
34
+ File.open(filename, "rb") { |io| Color::Palette::Gimp.from_io(io) }
35
+ end
36
+
37
+ # Create a GIMP palette object from the provided IO.
38
+ def from_io(io)
39
+ Color::Palette::Gimp.new(io.read)
40
+ end
41
+ end
42
+
43
+ # Create a new GIMP palette from the palette file as a string.
44
+ def initialize(palette)
45
+ @colors = []
46
+ @names = {}
47
+ @valid = false
48
+ @name = "(unnamed)"
49
+
50
+ palette.split($/).each do |line|
51
+ line.chomp!
52
+ line.gsub!(/\s*#.*\Z/, '')
53
+
54
+ next if line.empty?
55
+
56
+ if line =~ /\AGIMP Palette\Z/
57
+ @valid = true
58
+ next
59
+ end
60
+
61
+ info = /(\w+):\s(.*$)/.match(line)
62
+ if info
63
+ @name = info.captures[1] if info.captures[0] =~ /name/i
64
+ next
65
+ end
66
+
67
+ line.gsub!(/^\s+/, '')
68
+ data = line.split(/\s+/, 4)
69
+ name = data.pop.strip
70
+ data.map! { |el| el.to_i }
71
+
72
+ color = Color::RGB.new(*data)
73
+
74
+ @colors << color
75
+ @names[name] ||= []
76
+ @names[name] << color
77
+ end
78
+ end
79
+
80
+ # Provides the colour or colours at the provided selectors.
81
+ def values_at(*selectors)
82
+ @colors.values_at(*selectors)
83
+ end
84
+
85
+ # If a Numeric +key+ is provided, the single colour value at that position
86
+ # will be returned. If a String +key+ is provided, the colour set (an
87
+ # array) for that colour name will be returned.
88
+ def [](key)
89
+ if key.kind_of?(Numeric)
90
+ @colors[key]
91
+ else
92
+ @names[key]
93
+ end
94
+ end
95
+
96
+ # Loops through each colour.
97
+ def each
98
+ @colors.each { |el| yield el }
99
+ end
100
+
101
+ # Loops through each named colour set.
102
+ def each_name #:yields color_name, color_set:#
103
+ @names.each { |color_name, color_set| yield color_name, color_set }
104
+ end
105
+
106
+ # Returns true if this is believed to be a valid GIMP palette.
107
+ def valid?
108
+ @valid
109
+ end
110
+
111
+ def size
112
+ @colors.size
113
+ end
114
+
115
+ attr_reader :name
116
+ end