colorist 0.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.
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2008 Michael Bleigh and Intridea, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,27 @@
1
+ = Colorist
2
+
3
+ Colorist is a library built to handle the conversion, comparison, and
4
+ manipulation of colors in Ruby projects with an emphasis on W3C standards
5
+ and CSS-style hex notation. See the Color class for additional details
6
+ on the available methods.
7
+
8
+ == Example
9
+
10
+ require 'colorist'
11
+ include Colorist
12
+
13
+ gray = Color.new(0x333333)
14
+ gray + gray # => #<Color #666666>
15
+ gray - "#333" # => #<Color #000000>
16
+
17
+ gray.invert # => #<Color #cccccc>
18
+ gray.brightness # => 0.2
19
+
20
+ gray.contrast_with("#f00")
21
+
22
+ == Resources
23
+
24
+ * GitHub Source: http://github.com/mbleigh/colorist
25
+ * Lighthouse (for bugs): http://mbleigh.lighthouseapp.com/projects/15686-colorist
26
+
27
+ Copyright (c) 2008 Michael Bleigh and Intridea, Inc. Released under the MIT open source license.
@@ -0,0 +1,2 @@
1
+ require 'colorist/color'
2
+ require 'colorist/core_extensions'
@@ -0,0 +1,232 @@
1
+ module Colorist
2
+ # Color is the general class for storing and manipulating a color with the
3
+ # Colorist gem. It provides methods to add, subtract, and calculate aspects
4
+ # of the color based on W3C and other standards.
5
+ class Color
6
+ attr_accessor :r, :g, :b
7
+
8
+ CSS_COLOR_NAMES = { "maroon" => 0x800000,
9
+ "red" => 0xff0000,
10
+ "orange" => 0xffa500,
11
+ "yellow" => 0xffff00,
12
+ "olive" => 0x808000,
13
+ "purple" => 0x800080,
14
+ "fuchsia" => 0xff00ff,
15
+ "white" => 0xffffff,
16
+ "lime" => 0x00ff00,
17
+ "green" => 0x008000,
18
+ "navy" => 0x000080,
19
+ "blue" => 0x0000ff,
20
+ "aqua" => 0x00ffff,
21
+ "teal" => 0x008080,
22
+ "black" => 0x000000,
23
+ "silver" => 0xc0c0c0,
24
+ "gray" => 0x808080 }
25
+
26
+ # Creates a new color with the hex color provided as a number (i.e. 0x112233)
27
+ def initialize(color=0x000000)
28
+ string = "%.6x" % color
29
+ @r = string[0..1].hex
30
+ @g = string[2..3].hex
31
+ @b = string[4..5].hex
32
+ end
33
+
34
+ # Initialize a color based on RGB values. By default, the values
35
+ # should be between 0 and 255. If you use the option <tt>:percent => true</tt>,
36
+ # the values should then be between 0.0 and 1.0.
37
+ def self.from_rgb(r,g,b,options={})
38
+ color = Colorist::Color.new
39
+ # convert from 0.0 to 1.0 to 0 to 255 if the :percent option is used
40
+ if options[:percent]
41
+ color.r, color.g, color.b = r * 255, g * 255, b * 255
42
+ end
43
+ color.r, color.g, color.b = r, g, b
44
+ color
45
+ end
46
+
47
+ # Converts a CSS hex string into a color. Works both with the
48
+ # full form (i.e. <tt>#ffffff</tt>) and the abbreviated form (<tt>#fff</tt>). Can
49
+ # also take any of the 16 named CSS colors.
50
+ def self.from_string(some_string)
51
+ if matched = some_string.match(/\A#([0-9a-f]{3})\z/i)
52
+ color = Colorist::Color.from_rgb(*matched[1].split(//).collect{|v| "#{v}#{v}".hex })
53
+ elsif matched = some_string.match(/\A#([0-9a-f]{6})\z/)
54
+ color = Colorist::Color.new
55
+ color.r = matched[1][0..1].hex
56
+ color.g = matched[1][2..3].hex
57
+ color.b = matched[1][4..5].hex
58
+ elsif CSS_COLOR_NAMES.key?(some_string)
59
+ color = Colorist::Color.new(CSS_COLOR_NAMES[some_string])
60
+ else
61
+ raise ArgumentError, "Must provide a valid CSS hex color or color name.", caller
62
+ end
63
+ color
64
+ end
65
+
66
+ # Create a new color from the provided object. Duplicates Color objects
67
+ # and attempts to call <tt>to_color</tt> on other objects. Will raise
68
+ # an ArgumentError if it is unable to coerce the color.
69
+ def self.from(some_entity)
70
+ case some_entity
71
+ when Colorist::Color
72
+ some_entity.dup
73
+ else
74
+ raise ArgumentError, "#{some_entity.class.to_s} cannot be coerced into a color.", caller unless some_entity.respond_to?(:to_color)
75
+ some_entity.to_color
76
+ end
77
+ end
78
+
79
+ # Create a duplicate of this color.
80
+ def dup
81
+ Colorist::Color.from_rgb(@r,@g,@b)
82
+ end
83
+
84
+ # Add the individual RGB values of two colors together. You
85
+ # may also use an equivalent numeric or string color representation.
86
+ #
87
+ # Examples:
88
+ #
89
+ # gray = Colorist::Color.new(0x333333)
90
+ # gray + "#300" # => <Color #663333>
91
+ # gray + 0x000000 # => <Color #333333>
92
+ # white = "white".to_color
93
+ # gray + white # => <Color #ffffff>
94
+ def +(other_color)
95
+ other_color = Colorist::Color.from(other_color)
96
+ color = self.dup
97
+ color.r += other_color.r
98
+ color.g += other_color.g
99
+ color.b += other_color.b
100
+ color
101
+ end
102
+
103
+ # Subtract the individual RGB values of the two colors together.
104
+ # You may also use an equivalent numeric or string color representation.
105
+ def -(other_color)
106
+ other_color = Colorist::Color.from(other_color)
107
+ color = self.dup
108
+ color.r -= other_color.r
109
+ color.g -= other_color.g
110
+ color.b -= other_color.b
111
+ color
112
+ end
113
+
114
+ # Compares colors based on brightness.
115
+ def <=>(other_color)
116
+ other_color = Colorist::Color.from(other_color)
117
+ brightness <=> other_color.brightness
118
+ end
119
+
120
+ # Compares colors based on brightness.
121
+ def < (other_color)
122
+ other_color = Colorist::Color.from(other_color)
123
+ brightness < other_color.brightness
124
+ end
125
+
126
+ # Compares colors based on brightness.
127
+ def > (other_color)
128
+ other_color = Colorist::Color.from(other_color)
129
+ brightness > other_color.brightness
130
+ end
131
+
132
+ # Equal if the red, green, and blue values are identical.
133
+ def ==(other_color)
134
+ other_color = Colorist::Color.from(other_color)
135
+ other_color.r == self.r && other_color.g == self.g && other_color.b == self.b
136
+ end
137
+
138
+ # Equal if the brightnesses of the two colors are identical.
139
+ def ===(other_color)
140
+ other_color = Colorist::Color.from(other_color)
141
+ other_color.brightness == brightness
142
+ end
143
+
144
+ def r=(value) #:nodoc:
145
+ @r = value; normalize; end
146
+ def g=(value) #:nodoc:
147
+ @g = value; normalize; end
148
+ def b=(value) #:nodoc:
149
+ @b = value; normalize; end
150
+
151
+ # Outputs a string representation of the color in the desired format.
152
+ # The available formats are:
153
+ #
154
+ # * <tt>:css</tt> - As a CSS hex string (i.e. <tt>#ffffff</tt>) (default)
155
+ # * <tt>:css_rgb</tt> - As a CSS RGB value string (i.e. <tt>rgb(255,255,255)</tt>)
156
+ # * <tt>:rgb</tt> - As an RGB triplet (i.e. <tt>1.0, 1.0, 1.0</tt>)
157
+ def to_s(format=:css)
158
+ case format
159
+ when :css
160
+ "#%.2x%.2x%.2x" % [r, g, b]
161
+ when :css_rgb
162
+ "rgb(%.2f,%.2f,%.2f)" % [r, g, b]
163
+ when :rgb
164
+ "%.3f, %.3f, %.3f" % [r / 255, g / 255, b / 255]
165
+ end
166
+ end
167
+
168
+ def inspect
169
+ "#<Color #{to_s(:css)}>"
170
+ end
171
+
172
+ # Returns the perceived brightness of the provided color on a
173
+ # scale of 0.0 to 1.0 based on the formula provided. The formulas
174
+ # available are:
175
+ #
176
+ # * <tt>:w3c</tt> - <tt>((r * 299 + g * 587 + b * 114) / 1000 / 255</tt>
177
+ # * <tt>:standard</tt> - <tt>sqrt(0.241 * r^2 + 0.691 * g^2 + 0.068 * b^2) / 255</tt>
178
+ def brightness(formula=:w3c)
179
+ case formula
180
+ when :standard
181
+ Math.sqrt(0.241 * r**2 + 0.691 * g**2 + 0.068 * b**2) / 255
182
+ when :w3c
183
+ ((r * 299 + g * 587 + b * 114) / 255000.0)
184
+ end
185
+ end
186
+
187
+ # Contrast this color with another color using the provided formula. The
188
+ # available formulas are:
189
+ #
190
+ # * <tt>:w3c</tt> - <tt>(max(r1 r2) - min(r1 r2)) + (max(g1 g2) - min(g1 g2)) + (max(b1 b2) - min(b1 b2))</tt>
191
+ def contrast_with(other_color, formula=:w3c)
192
+ other_color = Color.from(other_color)
193
+ case formula
194
+ when :w3c
195
+ (([self.r, other_color.r].max - [self.r, other_color.r].min) +
196
+ ([self.g, other_color.g].max - [self.g, other_color.g].min) +
197
+ ([self.b, other_color.b].max - [self.b, other_color.b].min)) / 765.0
198
+ end
199
+ end
200
+
201
+ # Returns the opposite of the current color.
202
+ def invert
203
+ Color.from_rgb(255 - r, 255 - g, 255 - b)
204
+ end
205
+
206
+ # Converts the current color to grayscale using the brightness
207
+ # formula provided. See #brightness for a description of the
208
+ # available formulas.
209
+ def to_grayscale(formula=:w3c)
210
+ b = brightness(formula)
211
+ Color.from_rgb(255 * b, 255 * b, 255 * b)
212
+ end
213
+
214
+ # Returns an appropriate text color (either black or white) based on
215
+ # the brightness of this color. The +threshold+ specifies the brightness
216
+ # cutoff point.
217
+ def text_color(threshold=0.6, formula=:standard)
218
+ brightness(formula) > threshold ? Colorist::Color.new(0x000000) : Colorist::Color.new(0xffffff)
219
+ end
220
+
221
+ protected
222
+
223
+ def normalize #:nodoc:
224
+ @r = 255 if @r > 255
225
+ @g = 255 if @g > 255
226
+ @b = 255 if @b > 255
227
+ @r = 0 if @r < 0
228
+ @g = 0 if @g < 0
229
+ @b = 0 if @b < 0
230
+ end
231
+ end
232
+ end
@@ -0,0 +1,26 @@
1
+ class Integer
2
+ # Converts a hexadecimal number into a Color. Must be
3
+ # the equivalent of the full hexadecimal form (for example,
4
+ # <tt>0x123456</tt>).
5
+ def to_color
6
+ Colorist::Color.new(self)
7
+ end
8
+ end
9
+
10
+ class Float
11
+ # Converts a number from 0.0 to 1.0 to the grayscale equivalent
12
+ # of that brightness value. Especially useful for adding percentages
13
+ # to a color.
14
+ def to_color
15
+ Colorist::Color.from_rgb(self * 255, self * 255, self * 255)
16
+ end
17
+ end
18
+
19
+ class String
20
+ # Converts a CSS-style color string to a Color. Can be
21
+ # in the full form (<tt>\#112233</tt>), the abbreviated form
22
+ # (<tt>\#123</tt>) or a CSS named color (<tt>"black"</tt> or <tt>"maroon"</tt>).
23
+ def to_color
24
+ Colorist::Color.from_string(self)
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: colorist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Bleigh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-16 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Colorist is a library built to handle the easy conversion and manipulation of colors with a special emphasis on W3C standards and CSS-style hex color notation.
17
+ email: michael@intridea.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - MIT_LICENSE.rdoc
24
+ - README.rdoc
25
+ files:
26
+ - MIT_LICENSE.rdoc
27
+ - README.rdoc
28
+ - lib/colorist.rb
29
+ - lib/colorist/color.rb
30
+ - lib/colorist/core_extensions.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/mbleigh/colorist
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --main
36
+ - README.rdoc
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.2.0
55
+ signing_key:
56
+ specification_version: 2
57
+ summary: A library built to handle the easy conversion and manipulation of colors.
58
+ test_files: []
59
+