style_train 0.3.2 → 0.4.0

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.
@@ -1,44 +0,0 @@
1
- module StyleTrain
2
- class HexColor < ColorType
3
- class HexError < ArgumentError
4
- def message
5
- @message ||= 'Hexidecimal colors should be 3 or 6 hexidecimal digits and can be preceded by a # sign'
6
- end
7
- end
8
-
9
- attr_accessor :hex, :hex_6
10
-
11
- def type_initialize( color, opts )
12
- hex = color.to_s
13
- raise HexError unless hex.match(/^#?([a-f0-9]{3})$/i) || hex.match(/^#?([a-f0-9]{6})$/i)
14
- self.hex = "#{$1}"
15
- self.hex_6 = self.class.expand( self.hex )
16
- self.r = (self.hex_6 / 0x10000) & 0xff
17
- self.g = (self.hex_6 / 0x100) & 0xff
18
- self.b = (self.hex_6) & 0xff
19
- end
20
-
21
- def build
22
- self.hex = "%.2x" % self.r + "%.2x" % self.g + "%.2x" % self.b
23
- self.hex_6 = ('0x' + self.hex).to_i(16)
24
- end
25
-
26
- def self.expand( hex )
27
- expanded = if (hex.size == 3)
28
- str = ''
29
- (0..2).each do |index|
30
- char = hex[index..index]
31
- str << char*2
32
- end
33
- str
34
- else
35
- hex
36
- end
37
- "0x#{expanded}".to_i(16)
38
- end
39
-
40
- def render_as_given
41
- "##{self.hex}"
42
- end
43
- end
44
- end
@@ -1,5 +0,0 @@
1
- # todo: when needed
2
- module StyleTrain
3
- class HSLcolor < ColorType
4
- end
5
- end
@@ -1,192 +0,0 @@
1
- module StyleTrain
2
- class KeywordColor < ColorType
3
- class KeywordError < ArgumentError
4
- def message
5
- @message ||= 'Keyword not found'
6
- end
7
- end
8
-
9
- KEYWORD_MAP = {
10
- :aliceblue => [240, 248, 255],
11
- :antiquewhite => [250, 235, 215],
12
- :aqua => [0, 255, 255],
13
- :aquamarine => [127, 255, 212],
14
- :azure => [240, 255, 255],
15
- :beige => [245, 245, 220],
16
- :bisque => [255, 228, 196],
17
- :black => [0, 0, 0],
18
- :blanchedalmond => [255, 235, 205],
19
- :blue => [0, 0, 255],
20
- :blueviolet => [138, 43, 226],
21
- :brown => [165, 42, 42],
22
- :burlywood => [222, 184, 135],
23
- :cadetblue => [95, 158, 160],
24
- :chartreuse => [127, 255, 0],
25
- :chocolate => [210, 105, 30],
26
- :coral => [255, 127, 80],
27
- :cornflowerblue => [100, 149, 237],
28
- :cornsilk => [255, 248, 220],
29
- :crimson => [220, 20, 60],
30
- :cyan => [0, 255, 255],
31
- :darkblue => [0, 0, 139],
32
- :darkcyan => [0, 139, 139],
33
- :darkgoldenrod => [184, 134, 11],
34
- :darkgray => [169, 169, 169],
35
- :darkgreen => [0, 100, 0],
36
- :darkgrey => [169, 169, 169],
37
- :darkkhaki => [189, 183, 107],
38
- :darkmagenta => [139, 0, 139],
39
- :darkolivegreen => [85, 107, 47],
40
- :darkorange => [255, 140, 0],
41
- :darkorchid => [153, 50, 204],
42
- :darkred => [139, 0, 0],
43
- :darksalmon => [233, 150, 122],
44
- :darkseagreen => [143, 188, 143],
45
- :darkslateblue => [72, 61, 139],
46
- :darkslategray => [47, 79, 79],
47
- :darkslategrey => [47, 79, 79],
48
- :darkturquoise => [0, 206, 209],
49
- :darkviolet => [148, 0, 211],
50
- :deeppink => [255, 20, 147],
51
- :deepskyblue => [0, 191, 255],
52
- :dimgray => [105, 105, 105],
53
- :dimgrey => [105, 105, 105],
54
- :dodgerblue => [30, 144, 255],
55
- :firebrick => [178, 34, 34],
56
- :floralwhite => [255, 250, 240],
57
- :forestgreen => [34, 139, 34],
58
- :fuchsia => [255, 0, 255],
59
- :gainsboro => [220, 220, 220],
60
- :ghostwhite => [248, 248, 255],
61
- :gold => [255, 215, 0],
62
- :goldenrod => [218, 165, 32],
63
- :gray => [128, 128, 128],
64
- :green => [0, 128, 0],
65
- :greenyellow => [173, 255, 47],
66
- :grey => [128, 128, 128],
67
- :honeydew => [240, 255, 240],
68
- :hotpink => [255, 105, 180],
69
- :indianred => [205, 92, 92],
70
- :indigo => [75, 0, 130],
71
- :ivory => [255, 255, 240],
72
- :khaki => [240, 230, 140],
73
- :lavender => [230, 230, 250],
74
- :lavenderblush => [255, 240, 245],
75
- :lawngreen => [124, 252, 0],
76
- :lemonchiffon => [255, 250, 205],
77
- :lightblue => [173, 216, 230],
78
- :lightcoral => [240, 128, 128],
79
- :lightcyan => [224, 255, 255],
80
- :lightgoldenrodyellow => [250, 250, 210],
81
- :lightgray => [211, 211, 211],
82
- :lightgreen => [144, 238, 144],
83
- :lightgrey => [211, 211, 211],
84
- :lightpink => [255, 182, 193],
85
- :lightsalmon => [255, 160, 122],
86
- :lightseagreen => [32, 178, 170],
87
- :lightskyblue => [135, 206, 250],
88
- :lightslategray => [119, 136, 153],
89
- :lightslategrey => [119, 136, 153],
90
- :lightsteelblue => [176, 196, 222],
91
- :lightyellow => [255, 255, 224],
92
- :lime => [0, 255, 0],
93
- :limegreen => [50, 205, 50],
94
- :linen => [250, 240, 230],
95
- :magenta => [255, 0, 255],
96
- :maroon => [128, 0, 0],
97
- :mediumaquamarine => [102, 205, 170],
98
- :mediumblue => [0, 0, 205],
99
- :mediumorchid => [186, 85, 211],
100
- :mediumpurple => [147, 112, 219],
101
- :mediumseagreen => [60, 179, 113],
102
- :mediumslateblue => [123, 104, 238],
103
- :mediumspringgreen => [0, 250, 154],
104
- :mediumturquoise => [72, 209, 204],
105
- :mediumvioletred => [199, 21, 133],
106
- :midnightblue => [25, 25, 112],
107
- :mintcream => [245, 255, 250],
108
- :mistyrose => [255, 228, 225],
109
- :moccasin => [255, 228, 181],
110
- :navajowhite => [255, 222, 173],
111
- :navy => [0, 0, 128],
112
- :oldlace => [253, 245, 230],
113
- :olive => [128, 128, 0],
114
- :olivedrab => [107, 142, 35],
115
- :orange => [255, 165, 0],
116
- :orangered => [255, 69, 0],
117
- :orchid => [218, 112, 214],
118
- :palegoldenrod => [238, 232, 170],
119
- :palegreen => [152, 251, 152],
120
- :paleturquoise => [175, 238, 238],
121
- :palevioletred => [219, 112, 147],
122
- :papayawhip => [255, 239, 213],
123
- :peachpuff => [255, 218, 185],
124
- :peru => [205, 133, 63],
125
- :pink => [255, 192, 203],
126
- :plum => [221, 160, 221],
127
- :powderblue => [176, 224, 230],
128
- :purple => [128, 0, 128],
129
- :red => [255, 0, 0],
130
- :rosybrown => [188, 143, 143],
131
- :royalblue => [65, 105, 225],
132
- :saddlebrown => [139, 69, 19],
133
- :salmon => [250, 128, 114],
134
- :sandybrown => [244, 164, 96],
135
- :seagreen => [46, 139, 87],
136
- :seashell => [255, 245, 238],
137
- :sienna => [160, 82, 45],
138
- :silver => [192, 192, 192],
139
- :skyblue => [135, 206, 235],
140
- :slateblue => [106, 90, 205],
141
- :slategray => [112, 128, 144],
142
- :slategrey => [112, 128, 144],
143
- :snow => [255, 250, 250],
144
- :springgreen => [0, 255, 127],
145
- :steelblue => [70, 130, 180],
146
- :tan => [210, 180, 140],
147
- :teal => [0, 128, 128],
148
- :thistle => [216, 191, 216],
149
- :tomato => [255, 99, 71],
150
- :turquoise => [64, 224, 208],
151
- :violet => [238, 130, 238],
152
- :wheat => [245, 222, 179],
153
- :white => [255, 255, 255],
154
- :whitesmoke => [245, 245, 245],
155
- :yellow => [255, 255, 0],
156
- :yellowgreen => [154, 205, 50],
157
- :transparent => [0,0,0]
158
- }
159
-
160
- attr_reader :keyword
161
-
162
- def keyword=( color )
163
- color = color.to_sym
164
- raise KeywordError, "'#{color}' not found" unless KEYWORD_MAP.keys.include?( color )
165
- @keyword = color.to_sym
166
- mapped = KEYWORD_MAP[color]
167
- self.r = mapped[0]
168
- self.g = mapped[1]
169
- self.b = mapped[2]
170
- end
171
-
172
- def type_initialize( color, opts )
173
- self.keyword = color
174
- end
175
-
176
- def build
177
- # TODO: map to closest color instead of throwing an error
178
- found = false
179
- KEYWORD_MAP.each do |keyword, rgb|
180
- if self.r == rgb[0] && self.g == rgb[1] && self.b == rgb[2]
181
- found = true
182
- self.keyword = keyword
183
- end
184
- end
185
- raise KeywordError, "Keyword match not found for color matching rgb( #{self.r}, #{self.g}, #{self.b} )" unless found
186
- end
187
-
188
- def render_as_given
189
- self.keyword.to_s
190
- end
191
- end
192
- end
@@ -1,57 +0,0 @@
1
- module StyleTrain
2
- class RGBcolor < ColorType
3
- attr_reader :red, :green, :blue # these are the entered values, not the calculated values
4
-
5
- METHOD_MAP ={
6
- 'r' => 'red',
7
- 'g' => 'green',
8
- 'b' => 'blue'
9
- }
10
-
11
- ['red', 'green', 'blue'].each do |clr|
12
- class_eval "
13
- def #{clr}=( value )
14
- @#{clr} = value
15
- self.#{clr[0..0]} = value
16
- end
17
- "
18
- end
19
-
20
- ['r', 'g', 'b'].each do |clr|
21
- class_eval "
22
- def #{clr}=( value )
23
- @#{clr} = normalize_internal_rgb( value )
24
- end
25
- "
26
- end
27
-
28
- def type_initialize( color, opts )
29
- if color.is_a? Array
30
- self.red = color[0]
31
- self.green = color[1]
32
- self.blue = color[2]
33
- else
34
- raise ArgumentError, 'RGB color must be initialized with a :color opts argument that is an array' unless color && color.class == Array
35
- end
36
- end
37
-
38
- def build
39
- self.red = self.r
40
- self.green = self.g
41
- self.blue = self.b
42
- end
43
-
44
- def normalize_internal_rgb( value )
45
- if percentage = self.class.percentage( value )
46
- percentage = value.to_i
47
- self.class.percent_to_byte( percentage )
48
- else
49
- value.to_i if self.class.valid_byte?( value, true )
50
- end
51
- end
52
-
53
- def render_as_given
54
- "rgb( #{self.red}, #{self.green}, #{self.blue} )"
55
- end
56
- end
57
- end
@@ -1,252 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- Color = StyleTrain::Color unless defined?(Color)
4
- HexColor = StyleTrain::HexColor unless defined?( HexColor )
5
- KeywordColor = StyleTrain::KeywordColor unless defined?( KeywordColor )
6
- RGBcolor = StyleTrain::RGBcolor unless defined?( RGBcolor )
7
-
8
- describe Color do
9
- def build_color_type( color_class, args )
10
- color_class.new(:color => args)
11
- end
12
-
13
- describe 'alpha' do
14
- it 'should be delegated' do
15
- color = Color.new(:black, :alpha => 0.5)
16
- color.alpha.should == 0.5
17
- end
18
-
19
- it 'setter should be delegated' do
20
- color = Color.new(:black)
21
- color.alpha = 0.5
22
- color.delegate.alpha.should == 0.5
23
- end
24
- end
25
-
26
- describe 'initialization' do
27
- describe 'with an existing color' do
28
- it 'should build a delegate of the existing type' do
29
- Color.new(Color.new(:white)).delegate.should == build_color_type(KeywordColor, :white)
30
- Color.new(Color.new('#FFF')).delegate.should == build_color_type(HexColor, '#FFF')
31
- Color.new(Color.new([255,255,255])).delegate.should == build_color_type(RGBcolor, [255,255,255])
32
- end
33
-
34
- it 'should set the background if the argument color has a background' do
35
- Color.new(Color.new(:white), :background => :gray).background.should == Color.new(:gray)
36
- Color.new(Color.new(:white)).background_set.should_not be_true
37
- end
38
-
39
- it 'after the new color is created changes to the delegate should not change the original' do
40
- original = Color.new(:white)
41
- new_color = Color.new(original, :background => :black, :alpha => 0.5)
42
- original.alpha.should == 1.0
43
- end
44
-
45
- it 'should set the alpha' do
46
- color = Color.new(Color.new(:white), :alpha => 0.5)
47
- color.alpha.should == 0.5
48
- end
49
- end
50
-
51
- describe 'assigns a delegate' do
52
- describe 'with a keyword argument' do
53
- it 'should build an html keyword color' do
54
- Color.new("gray").delegate.should == build_color_type(KeywordColor, 'gray')
55
- end
56
-
57
- it 'should build a svg keyword color' do
58
- Color.new(:linen).delegate.should == build_color_type(KeywordColor, :linen)
59
- end
60
- end
61
-
62
- describe 'with a hex string argument' do
63
- it '(3-digit no #) should build a hex color' do
64
- Color.new('333').delegate.should == build_color_type(HexColor, '333')
65
- end
66
-
67
- it '(3-digit with #) should build a hex color' do
68
- Color.new('#333').delegate.should == build_color_type(HexColor, '#333')
69
- end
70
-
71
- it '(6-digit no #) should build a hex color' do
72
- Color.new('333333').delegate.should == build_color_type(HexColor, '333333')
73
- end
74
-
75
- it '(6-digit with #) should build a hex color' do
76
- Color.new('#333333').delegate.should == build_color_type(HexColor, '#333333')
77
- end
78
- end
79
-
80
- describe 'with an rgb array argument' do
81
- it '(with a byte array) should build a rgb color' do
82
- Color.new([127,127,127]).delegate.should == build_color_type(RGBcolor, [127,127,127])
83
- end
84
-
85
- it '(with a percentage array) should build a rgb color' do
86
- Color.new(['50%','50%','50%']).delegate.should == build_color_type(RGBcolor, [127,127,127])
87
- end
88
- end
89
-
90
- describe 'hsl colors'
91
- end
92
-
93
- describe 'options' do
94
- it 'should pass on the alpha argument' do
95
- color = Color.new(:black, :alpha => 0.5)
96
- color.alpha.should == 0.5
97
- end
98
- end
99
- end
100
-
101
- describe 'comparison' do
102
- before do
103
- @color = Color.new(:black)
104
- @color_type = KeywordColor.new(:color => :black)
105
- @color_type_too = RGBcolor.new(:color => [0,0,0])
106
- @color_too = Color.new(:black)
107
- end
108
-
109
- describe '=~' do
110
- describe 'with another Color object' do
111
- it 'should not be true if the delegates don\'t match' do
112
- (@color =~ Color.new(:white)).should_not be_true
113
- end
114
-
115
- it 'should be indifferent to background' do
116
- (@color =~ Color.new(:black, :background => :yellow)).should be_true
117
- end
118
-
119
- it 'should be true if background and delegate match' do
120
- (@color =~ @color_too).should be_true
121
- end
122
- end
123
-
124
- describe 'with a ColorType object' do
125
- it 'should be true if the delegate matches the ColorType' do
126
- (@color =~ @color_type).should be_true
127
- (@color =~ @color_type_too).should be_true
128
- end
129
- end
130
- end
131
-
132
- describe '==' do
133
- describe 'with another Color object' do
134
- it 'should not be true if the delegates don\'t match' do
135
- (@color == Color.new(:white)).should_not be_true
136
- end
137
-
138
- it 'should be indifferent to background' do
139
- (@color == Color.new(:black, :background => :yellow)).should_not be_true
140
- end
141
-
142
- it 'should be true if background and delegate match' do
143
- (@color == @color_too).should be_true
144
- end
145
- end
146
-
147
- describe 'with a ColorType object' do
148
- it 'should be false' do
149
- (@color == @color_type).should be_false
150
- end
151
- end
152
- end
153
- end
154
-
155
- describe 'background' do
156
- before do
157
- @color = Color.new(:black, :alpha => 0.5)
158
- end
159
-
160
- it 'should have a white opaque background by default' do
161
- @color.background.should =~ Color.new(:white)
162
- end
163
-
164
- it 'should be settable' do
165
- @color.background = :black
166
- @color.background.should =~ Color.new(:black)
167
- end
168
-
169
- it 'should ignore alpha' do
170
- @color.background = {:color => :black, :alpha => 0.5 }
171
- @color.background.delegate.should == Color.new(:black, :alpha => 1.0).delegate
172
- end
173
-
174
- it 'should be initalized into the instance' do
175
- color = Color.new(:black, :background => :yellow)
176
- color.background.should =~ Color.new(:yellow)
177
- end
178
-
179
- it 'should set a flag when it is set not by default' do
180
- color = Color.new(:black)
181
- color.background
182
- color.background_set.should_not == true
183
-
184
- color = Color.new(:black, :background => :yellow)
185
- color.background_set.should == true
186
- end
187
- end
188
-
189
- describe 'rendering' do
190
- it 'should render via the delegates default method by default' do
191
- Color.new(:white).render.should == build_color_type(KeywordColor, :white).render_as_given
192
- Color.new(['100%', '100%', '100%']).render.should == build_color_type(RGBcolor, ['100%', '100%', '100%']).render_as_given
193
- Color.new([255,255,255]).render.should == build_color_type(RGBcolor, [255,255,255]).render_as_given
194
- Color.new('#FFF').render.should == build_color_type(HexColor, '#FFF').render_as_given
195
- end
196
-
197
- it 'should render rgba if it has transparency' do
198
- Color.new(:white, :alpha => 0.5).render.should =~ /rgba/
199
- Color.new(['100%', '100%', '100%'], :alpha => 0.5).render.should =~ /rgba/
200
- Color.new([255,255,255], :alpha => 0.5).render.should =~ /rgba/
201
- Color.new('#FFF', :alpha => 0.5).render.should =~ /rgba/
202
- end
203
-
204
- describe 'ie' do
205
- it 'should render as a hex color' do
206
- Color.new(:white).render(:ie).should == build_color_type(HexColor, '#ffffff').render_as_given
207
- Color.new(['100%', '100%', '100%']).render(:ie).should == build_color_type(HexColor, '#ffffff').render_as_given
208
- Color.new([255,255,255]).render(:ie).should == build_color_type(HexColor, '#ffffff').render_as_given
209
- Color.new('#FFF').render(:ie).should == build_color_type(HexColor, '#ffffff').render_as_given
210
- end
211
-
212
- it 'should render delegate layered on the background if there is transparency and as a hex color' do
213
- Color.new(:black, :alpha => 0.5).render(:ie).should == build_color_type( HexColor, '#808080' ).render
214
- Color.new(['0%', '0%', '0%'], :alpha => 0.5).render(:ie).should == build_color_type( HexColor, '#808080' ).render
215
- Color.new([255,255,255], :alpha => 0.5, :background => :black).render(:ie).should == build_color_type( HexColor, '#808080' ).render
216
- Color.new('#FFF', :alpha => 0.5, :background => :black).render(:ie).should == build_color_type( HexColor, '#808080' ).render
217
- end
218
- end
219
-
220
- it 'should alias #to_s to render' do
221
- Color.new(:black, :alpha => 0.5).to_s.should == Color.new(:black, :alpha => 0.5).render
222
- end
223
- end
224
-
225
- describe 'stepping' do
226
- before :all do
227
- @black = Color.new('#000')
228
- @yellow = Color.new(:lightyellow, :alpha => 0.5)
229
- @steps = @black.step(@yellow)
230
- end
231
-
232
- it 'returs an array' do
233
- @steps.is_a?(Array).should be_true
234
- end
235
-
236
- it 'has 10 steps by default' do
237
- @steps.size.should == 10
238
- end
239
-
240
- it 'has takes optional number of steps' do
241
- @black.step(@yellow, 5).size.should == 5
242
- end
243
-
244
- it 'first color should be the starting point' do
245
- @steps.first.should == @black.delegate
246
- end
247
-
248
- it 'last color should be the last point' do
249
- @steps.last.should == @yellow.delegate
250
- end
251
- end
252
- end