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.
data/spec/numbers_spec.rb CHANGED
@@ -16,6 +16,10 @@ describe Fixnum do
16
16
  it '#pt converts to a string with % at the end' do
17
17
  42.pt.should == '42pt'
18
18
  end
19
+
20
+ it '#degrees converts to a snring with degrees at the end' do
21
+ 42.degrees.should == '42degrees'
22
+ end
19
23
  end
20
24
 
21
25
  describe Float do
@@ -23,7 +27,15 @@ describe Float do
23
27
  1.5.em.should == '1.5em'
24
28
  end
25
29
 
26
- it '#pt converts to a string with % at the end' do
30
+ it '#pt converts to a string with pt at the end' do
27
31
  12.5.pt.should == '12.5pt'
28
32
  end
33
+
34
+ it '#percent converts to a string with % at the end' do
35
+ 30.25.percent.should == '30.25%'
36
+ end
37
+
38
+ it '#degrees converts to a snring with degrees at the end' do
39
+ 42.5.degrees.should == '42.5degrees'
40
+ end
29
41
  end
data/spec/sheet_spec.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  Sheet = StyleTrain::Sheet unless defined?( Sheet )
4
+ Color = StyleTrain::Color unless defined?( Color )
4
5
 
5
6
  describe Sheet do
6
7
  class StyleSheet < StyleTrain::Sheet
@@ -194,7 +195,7 @@ CSS
194
195
 
195
196
  describe 'background' do
196
197
  it ':color will create a background-color property' do
197
- @sheet.background(:color => :white).should include 'background-color: white;'
198
+ @sheet.background(:color => :white).should include "background-color: #{Color.new(:white)};"
198
199
  end
199
200
 
200
201
  it ':image will create a background-image property' do
@@ -584,7 +585,7 @@ CSS
584
585
 
585
586
  describe 'duplicate declarations' do
586
587
  it 'color' do
587
- @sheet.color(StyleTrain::Color.new('#456')).should include 'color: #456'
588
+ @sheet.color(StyleTrain::Color.new('#456')).should include 'color: #445566'
588
589
  end
589
590
 
590
591
  it 'overflow' do
@@ -621,10 +622,23 @@ CSS
621
622
  @sheet.visibility(:hidden).should include 'visibility: hidden'
622
623
  end
623
624
 
624
- it 'opacity' do
625
- str = @sheet.opacity(0.5)
626
- str.should include 'opacity: 0.5'
627
- str.should include 'filter: alpha(opacity=50)'
625
+ describe 'opacity' do
626
+ it 'has a simple declaration' do
627
+ str = @sheet.opacity(0.5)
628
+ str.should include 'opacity: 0.5'
629
+ str.should_not include 'filter: alpha(opacity=50)' # IE creates a gray box with alpha
630
+ end
631
+
632
+ it 'has a separate alpha declaration' do
633
+ str = @sheet.alpha(50)
634
+ str.should include 'filter: alpha(opacity=50)'
635
+ end
636
+
637
+ it '#opacity can take :alpha option as an argument' do
638
+ str = @sheet.opacity(0.5, :alpha => true)
639
+ str.should include 'opacity: 0.5'
640
+ str.should include 'filter: alpha(opacity=50)'
641
+ end
628
642
  end
629
643
  end
630
644
  end
@@ -810,11 +824,11 @@ CSS
810
824
  it 'works' do
811
825
  StyleSheet.render.should include(
812
826
  "body {
813
- background-color: #666;
827
+ background-color: #666666;
814
828
  font-family: verdana;
815
829
  }
816
830
  #wrapper {
817
- background-color: white;
831
+ background-color: #FFFFFF;
818
832
  margin: 1em auto;
819
833
  }
820
834
  .foo {
@@ -885,7 +899,7 @@ CSS
885
899
  .first,
886
900
  .second,
887
901
  .third {
888
- background-color: black;
902
+ background-color: #000000;
889
903
  }
890
904
  CSS
891
905
  end
@@ -1020,7 +1034,7 @@ form {
1020
1034
  end
1021
1035
 
1022
1036
  it 'should render embedded properties' do
1023
- @str.should include "background-color: red"
1037
+ @str.should include "background-color: #{Color.new(:red)}"
1024
1038
  end
1025
1039
  end
1026
1040
 
@@ -9,12 +9,12 @@ describe ThemedSheet do
9
9
 
10
10
  FooeyTheme.new(:default, {
11
11
  :foo => '#666',
12
- :bar => 'white'
12
+ :bar => :white
13
13
  })
14
14
 
15
15
  FooeyTheme.new(:inverted, {
16
16
  :foo => '#CCC',
17
- :bar => 'black'
17
+ :bar => :black
18
18
  })
19
19
 
20
20
  class FooeySheet < ThemedSheet
data/style_train.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{style_train}
8
- s.version = "0.3.2"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kane Baccigalupi"]
12
- s.date = %q{2011-03-03}
12
+ s.date = %q{2011-03-23}
13
13
  s.description = %q{style_train builds CSS using pure Ruby, not a DSL interpreted via Ruby. This allows inheritance, modules, instance level calculations and all the goodness Ruby can offer.}
14
14
  s.email = %q{baccigalupi@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -22,11 +22,6 @@ Gem::Specification.new do |s|
22
22
  "VERSION",
23
23
  "lib/style_train.rb",
24
24
  "lib/style_train/color.rb",
25
- "lib/style_train/color_types/color_type.rb",
26
- "lib/style_train/color_types/hex_color.rb",
27
- "lib/style_train/color_types/hsl_color.rb",
28
- "lib/style_train/color_types/keyword_color.rb",
29
- "lib/style_train/color_types/rgb_color.rb",
30
25
  "lib/style_train/sheet.rb",
31
26
  "lib/style_train/style.rb",
32
27
  "lib/style_train/support/gnash.rb",
@@ -38,11 +33,7 @@ Gem::Specification.new do |s|
38
33
  "research/contexting.rb",
39
34
  "research/overview.txt",
40
35
  "research/stylesheet.txt",
41
- "spec/color/color_spec.rb",
42
- "spec/color/color_type_spec.rb",
43
- "spec/color/hex_color_spec.rb",
44
- "spec/color/keyword_color_spec.rb",
45
- "spec/color/rgb_color_spec.rb",
36
+ "spec/color_spec.rb",
46
37
  "spec/generated_files/.gitkeep",
47
38
  "spec/integration_spec.rb",
48
39
  "spec/numbers_spec.rb",
@@ -60,11 +51,7 @@ Gem::Specification.new do |s|
60
51
  s.rubygems_version = %q{1.3.7}
61
52
  s.summary = %q{style_train builds CSS with Ruby}
62
53
  s.test_files = [
63
- "spec/color/color_spec.rb",
64
- "spec/color/color_type_spec.rb",
65
- "spec/color/hex_color_spec.rb",
66
- "spec/color/keyword_color_spec.rb",
67
- "spec/color/rgb_color_spec.rb",
54
+ "spec/color_spec.rb",
68
55
  "spec/integration_spec.rb",
69
56
  "spec/numbers_spec.rb",
70
57
  "spec/sheet_spec.rb",
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: style_train
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 2
10
- version: 0.3.2
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kane Baccigalupi
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-03 00:00:00 -08:00
18
+ date: 2011-03-23 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -47,11 +47,6 @@ files:
47
47
  - VERSION
48
48
  - lib/style_train.rb
49
49
  - lib/style_train/color.rb
50
- - lib/style_train/color_types/color_type.rb
51
- - lib/style_train/color_types/hex_color.rb
52
- - lib/style_train/color_types/hsl_color.rb
53
- - lib/style_train/color_types/keyword_color.rb
54
- - lib/style_train/color_types/rgb_color.rb
55
50
  - lib/style_train/sheet.rb
56
51
  - lib/style_train/style.rb
57
52
  - lib/style_train/support/gnash.rb
@@ -63,11 +58,7 @@ files:
63
58
  - research/contexting.rb
64
59
  - research/overview.txt
65
60
  - research/stylesheet.txt
66
- - spec/color/color_spec.rb
67
- - spec/color/color_type_spec.rb
68
- - spec/color/hex_color_spec.rb
69
- - spec/color/keyword_color_spec.rb
70
- - spec/color/rgb_color_spec.rb
61
+ - spec/color_spec.rb
71
62
  - spec/generated_files/.gitkeep
72
63
  - spec/integration_spec.rb
73
64
  - spec/numbers_spec.rb
@@ -114,11 +105,7 @@ signing_key:
114
105
  specification_version: 3
115
106
  summary: style_train builds CSS with Ruby
116
107
  test_files:
117
- - spec/color/color_spec.rb
118
- - spec/color/color_type_spec.rb
119
- - spec/color/hex_color_spec.rb
120
- - spec/color/keyword_color_spec.rb
121
- - spec/color/rgb_color_spec.rb
108
+ - spec/color_spec.rb
122
109
  - spec/integration_spec.rb
123
110
  - spec/numbers_spec.rb
124
111
  - spec/sheet_spec.rb
@@ -1,233 +0,0 @@
1
- module StyleTrain
2
- class ColorType
3
- # ERROR CLASSES
4
- class PercentageError < ArgumentError
5
- def message
6
- @message ||= 'Percentages must be between 0 and 100'
7
- end
8
- end
9
-
10
- class ByteNumberError < ArgumentError
11
- def message
12
- @message ||= 'Byte numbers must be between 0 and 255'
13
- end
14
- end
15
-
16
- class AlphaError < ArgumentError
17
- def message
18
- @message ||= 'Alpha must be between 0.0 and 1.0'
19
- end
20
- end
21
-
22
- # ATTRIBUTES
23
- attr_accessor :r, :g, :b
24
- attr_reader :alpha
25
-
26
- # INITIALIZATION
27
- def initialize( opts )
28
- if self.class.is_color?(opts)
29
- build_from_color(opts)
30
- else
31
- opts = Gnash.new(opts)
32
- color = opts[:color]
33
- self.alpha = opts[:alpha]
34
- type_initialize( color, opts )
35
- end
36
- end
37
-
38
- def type_initialize( color, opts )
39
- raise NotYetImplemented, "type_initialize must be implemented for #{self}"
40
- end
41
-
42
- def alpha=( value )
43
- value = (value || 1.0).to_f
44
- raise AlphaError if value > 1.0 || value < 0.0
45
- @alpha = value
46
- end
47
-
48
- def build_from_color( color )
49
- self.r = color.r
50
- self.g = color.g
51
- self.b = color.b
52
- self.alpha = color.alpha
53
- build
54
- end
55
-
56
- def build
57
- raise NotImplementedError, "#build must be implemented for #{self.class}"
58
- end
59
-
60
- def self.is_color?( color )
61
- color.is_a? ColorType
62
- end
63
-
64
- # INSTANCE METHODS
65
- # conversion
66
- def to( color_type )
67
- klass = self.class.color_type_class(color_type)
68
- raise ArgumentError, 'color type not supported' unless klass
69
- self.class == klass ? self : klass.new( self )
70
- end
71
-
72
- # comparison
73
- def =~( color )
74
- if color.is_a?( ColorType )
75
- self.r == color.r && self.g == color.g && self.b == color.b
76
- elsif color.is_a?( Color )
77
- self =~ color.delegate
78
- end
79
- end
80
-
81
- def ==( color )
82
- (self =~ color) && self.alpha == color.alpha
83
- end
84
-
85
- def ===( color )
86
- (self == color) && self.class == color.class
87
- end
88
-
89
- # blending
90
- def mix( color, mix_ratio=0.5 )
91
- mix_ratio = 1-mix_ratio
92
- mixed = self.dup
93
- mixed.r = ratio(self.r, color.r, mix_ratio)
94
- mixed.g = ratio(self.g, color.g, mix_ratio)
95
- mixed.b = ratio(self.b, color.b, mix_ratio)
96
- mixed.alpha = ratio(self.alpha, color.alpha, mix_ratio, false)
97
- mixed.build
98
- mixed
99
- end
100
-
101
- def layer( color )
102
- layered = self.dup
103
- ratio = 1 - color.alpha
104
- layered.r = self.class.blend( self.r, color.r, ratio )
105
- layered.g = self.class.blend( self.g, color.g, ratio )
106
- layered.b = self.class.blend( self.b, color.b, ratio )
107
- layered.alpha = self.class.blend_alphas( self.alpha, color.alpha )
108
- layered.build
109
- layered
110
- end
111
-
112
- def self.blend(first, second, ratio)
113
- ratio(first, second, ratio)
114
- end
115
-
116
- def self.average(first, second, round=true)
117
- ratio(first, second, round)
118
- end
119
-
120
- def self.ratio(first, second, first_ratio=0.5, round=true)
121
- second_ratio = 1-first_ratio
122
- value = first*first_ratio + second*second_ratio
123
- round ? value.round : value
124
- end
125
-
126
- def ratio *args
127
- self.class.ratio *args
128
- end
129
-
130
- def average *args
131
- self.class.average *args
132
- end
133
-
134
-
135
- def self.blend_alphas(first, second)
136
- base, blender = first > second ? [first, second] : [second, first]
137
- difference = 1.0 - base
138
- base + difference * blender
139
- end
140
-
141
- # rendering
142
- def render(opts=nil)
143
- alpha? ? render_as_rgba : render_as_given
144
- end
145
-
146
- def to_s(opts=nil)
147
- render(opts)
148
- end
149
-
150
- def render_as_given
151
- raise NotImplementedError, "#render_as_given must be implemented for #{self.class}"
152
- end
153
-
154
- def render_as_rgba
155
- "rgba( #{self.r}, #{self.g}, #{self.b}, #{self.alpha} )"
156
- end
157
-
158
- def alpha?
159
- self.alpha != 1.0
160
- end
161
-
162
- # CLASS METHODS
163
-
164
- def self.make(opts)
165
- begin
166
- new(opts)
167
- rescue
168
- nil
169
- end
170
- end
171
-
172
- # These can't contain real classes because it will create a dependency loop.
173
- def self.color_opts
174
- @color_opts ||= Gnash.new(
175
- :rgb => 'StyleTrain::RGBcolor',
176
- :keyword => 'StyleTrain::KeywordColor',
177
- :hex => 'StyleTrain::HexColor'
178
- # :hsl => 'HSLcolor' # when this class exists
179
- )
180
- end
181
-
182
- # So they are replaced on usage with the actual constants, as needed
183
- def self.color_type_class( color_type )
184
- klass = color_opts[ color_type ]
185
- if klass.class == String
186
- color_opts[color_type] = klass.constantize
187
- else
188
- klass
189
- end
190
- end
191
-
192
- def self.color_types
193
- unless @types_built
194
- color_opts.each do |key, klass|
195
- self.color_type_class( key )
196
- end
197
- @types_built = true
198
- end
199
- color_opts.values
200
- end
201
-
202
- def self.percentage( str )
203
- if str.class == String && str.match(/^([\d.]*)%$/)
204
- number = $1.to_f
205
- raise PercentageError if number > 100.0 || number < 0.0
206
- number
207
- else
208
- false
209
- end
210
- end
211
-
212
- def self.byte( str )
213
- str.to_i if valid_byte?( str, true )
214
- end
215
-
216
- def self.valid_byte?( number, raise_exe=false )
217
- num = number.to_i
218
- v = num <= 255 && num >= 0
219
- raise ByteNumberError if v != true && raise_exe
220
- v
221
- end
222
-
223
- def self.percent_to_byte( number )
224
- raise PercentageError if number > 100.0 || number < 0.0
225
- ( number * 2.55 ).round
226
- end
227
-
228
- def self.byte_to_percentage( number )
229
- raise ByteNumberError if number > 255.0 || number < 0.0
230
- ( number/2.55 ).round
231
- end
232
- end
233
- end