Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 373
- Log:
Initial import of Radiant 0.9.1, which is now packaged as a gem. This is an
import of the tagged 0.9.1 source checked out from GitHub, which isn't quite
the same as the gem distribution - but it doesn't seem to be available in an
archived form and the installed gem already has modifications, so this is
the closest I can get.
- Author:
- rool
- Date:
- Mon Mar 21 13:40:05 +0000 2011
- Size:
- 7036 Bytes
1 | require 'sass/script/literal' |
2 | |
3 | module Sass::Script |
4 | # A SassScript object representing a CSS color. |
5 | class Color < Literal |
6 | class << self; include Haml::Util; end |
7 | |
8 | # A hash from color names to `[red, green, blue]` value arrays. |
9 | # @private |
10 | HTML4_COLORS = map_vals({ |
11 | 'black' => 0x000000, |
12 | 'silver' => 0xc0c0c0, |
13 | 'gray' => 0x808080, |
14 | 'white' => 0xffffff, |
15 | 'maroon' => 0x800000, |
16 | 'red' => 0xff0000, |
17 | 'purple' => 0x800080, |
18 | 'fuchsia' => 0xff00ff, |
19 | 'green' => 0x008000, |
20 | 'lime' => 0x00ff00, |
21 | 'olive' => 0x808000, |
22 | 'yellow' => 0xffff00, |
23 | 'navy' => 0x000080, |
24 | 'blue' => 0x0000ff, |
25 | 'teal' => 0x008080, |
26 | 'aqua' => 0x00ffff |
27 | }) {|color| (0..2).map {|n| color >> (n << 3) & 0xff}.reverse} |
28 | # A hash from `[red, green, blue]` value arrays to color names. |
29 | # @private |
30 | HTML4_COLORS_REVERSE = map_hash(HTML4_COLORS) {|k, v| [v, k]} |
31 | |
32 | # Creates a new color from RGB components. |
33 | # *Note*: when modifying the components of an existing color, |
34 | # use \{#with} rather than creating a new color object. |
35 | # This preserves forwards-compatiblity for alpha channels and such. |
36 | # |
37 | # @param rgb [Array<Fixnum>] A three-element array of the red, green, and blue values (respectively) |
38 | # of the color |
39 | # @raise [Sass::SyntaxError] if any color value isn't between 0 and 255 |
40 | def initialize(rgb) |
41 | rgb = rgb.map {|c| c.to_i} |
42 | raise Sass::SyntaxError.new("Color values must be between 0 and 255") if rgb.any? {|c| c < 0 || c > 255} |
43 | super(rgb.freeze) |
44 | end |
45 | |
46 | # @deprecated This will be removed in version 3.2. |
47 | # @see #rgb |
48 | def value |
49 | warn <<END |
50 | DEPRECATION WARNING: |
51 | The Sass::Script::Color #value attribute is deprecated and will be |
52 | removed in version 3.2. Use the #rgb attribute instead. |
53 | END |
54 | rgb |
55 | end |
56 | |
57 | # Returns the red, green, and blue components of the color. |
58 | # |
59 | # @return [Array<Fixnum>] A frozen three-element array of the red, green, and blue |
60 | # values (respectively) of the color |
61 | def rgb |
62 | @value |
63 | end |
64 | |
65 | # Returns a copy of this color with one or more channels changed. |
66 | # |
67 | # For example: |
68 | # |
69 | # Color.new([10, 20, 30]).with(:blue => 40) |
70 | # #=> rgb(10, 40, 30) |
71 | # Color.new([126, 126, 126]).with(:red => 0, :green => 255) |
72 | # #=> rgb(0, 255, 126) |
73 | # |
74 | # @param attrs [{Symbol => Fixnum}] |
75 | # A map of channel names (`:red`, `:green`, or `:blue`) to values |
76 | # @return [Color] The new Color object |
77 | def with(attrs) |
78 | Color.new([ |
79 | attrs[:red] || rgb[0], |
80 | attrs[:green] || rgb[1], |
81 | attrs[:blue] || rgb[2], |
82 | ]) |
83 | end |
84 | |
85 | # The SassScript `+` operation. |
86 | # Its functionality depends on the type of its argument: |
87 | # |
88 | # {Number} |
89 | # : Adds the number to each of the RGB color channels. |
90 | # |
91 | # {Color} |
92 | # : Adds each of the RGB color channels together. |
93 | # |
94 | # {Literal} |
95 | # : See {Literal#plus}. |
96 | # |
97 | # @param other [Literal] The right-hand side of the operator |
98 | # @return [Color] The resulting color |
99 | # @raise [Sass::SyntaxError] if `other` is a number with units |
100 | def plus(other) |
101 | if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color) |
102 | piecewise(other, :+) |
103 | else |
104 | super |
105 | end |
106 | end |
107 | |
108 | # The SassScript `-` operation. |
109 | # Its functionality depends on the type of its argument: |
110 | # |
111 | # {Number} |
112 | # : Subtracts the number from each of the RGB color channels. |
113 | # |
114 | # {Color} |
115 | # : Subtracts each of the other color's RGB color channels from this color's. |
116 | # |
117 | # {Literal} |
118 | # : See {Literal#minus}. |
119 | # |
120 | # @param other [Literal] The right-hand side of the operator |
121 | # @return [Color] The resulting color |
122 | # @raise [Sass::SyntaxError] if `other` is a number with units |
123 | def minus(other) |
124 | if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color) |
125 | piecewise(other, :-) |
126 | else |
127 | super |
128 | end |
129 | end |
130 | |
131 | # The SassScript `*` operation. |
132 | # Its functionality depends on the type of its argument: |
133 | # |
134 | # {Number} |
135 | # : Multiplies the number by each of the RGB color channels. |
136 | # |
137 | # {Color} |
138 | # : Multiplies each of the RGB color channels together. |
139 | # |
140 | # @param other [Number, Color] The right-hand side of the operator |
141 | # @return [Color] The resulting color |
142 | # @raise [Sass::SyntaxError] if `other` is a number with units |
143 | def times(other) |
144 | if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color) |
145 | piecewise(other, :*) |
146 | else |
147 | raise NoMethodError.new(nil, :times) |
148 | end |
149 | end |
150 | |
151 | # The SassScript `/` operation. |
152 | # Its functionality depends on the type of its argument: |
153 | # |
154 | # {Number} |
155 | # : Divides each of the RGB color channels by the number. |
156 | # |
157 | # {Color} |
158 | # : Divides each of this color's RGB color channels by the other color's. |
159 | # |
160 | # {Literal} |
161 | # : See {Literal#div}. |
162 | # |
163 | # @param other [Literal] The right-hand side of the operator |
164 | # @return [Color] The resulting color |
165 | # @raise [Sass::SyntaxError] if `other` is a number with units |
166 | def div(other) |
167 | if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color) |
168 | piecewise(other, :/) |
169 | else |
170 | super |
171 | end |
172 | end |
173 | |
174 | # The SassScript `%` operation. |
175 | # Its functionality depends on the type of its argument: |
176 | # |
177 | # {Number} |
178 | # : Takes each of the RGB color channels module the number. |
179 | # |
180 | # {Color} |
181 | # : Takes each of this color's RGB color channels modulo the other color's. |
182 | # |
183 | # @param other [Number, Color] The right-hand side of the operator |
184 | # @return [Color] The resulting color |
185 | # @raise [Sass::SyntaxError] if `other` is a number with units |
186 | def mod(other) |
187 | if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color) |
188 | piecewise(other, :%) |
189 | else |
190 | raise NoMethodError.new(nil, :mod) |
191 | end |
192 | end |
193 | |
194 | # Returns a string representation of the color. |
195 | # This is usually the color's hex value, |
196 | # but if the color has a name that's used instead. |
197 | # |
198 | # @return [String] The string representation |
199 | def to_s |
200 | return HTML4_COLORS_REVERSE[rgb] if HTML4_COLORS_REVERSE[rgb] |
201 | red, green, blue = rgb.map { |num| num.to_s(16).rjust(2, '0') } |
202 | "##{red}#{green}#{blue}" |
203 | end |
204 | alias_method :inspect, :to_s |
205 | |
206 | private |
207 | |
208 | def piecewise(other, operation) |
209 | other_num = other.is_a? Number |
210 | if other_num && !other.unitless? |
211 | raise Sass::SyntaxError.new("Cannot add a number with units (#{other}) to a color (#{self}).") |
212 | end |
213 | |
214 | result = [] |
215 | for i in (0...3) |
216 | res = rgb[i].send(operation, other_num ? other.value : other.rgb[i]) |
217 | result[i] = [ [res, 255].min, 0 ].max |
218 | end |
219 | with(:red => result[0], :green => result[1], :blue => result[2]) |
220 | end |
221 | end |
222 | end |
223 |