paleta 0.1.0 → 0.2.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/lib/paleta/color.rb CHANGED
@@ -212,6 +212,21 @@ module Paleta
212
212
  distance({ :r => @red, :g => @green, :b => @blue}, { :r => color.red, :g => color.green, :b => color.blue}) / sqrt(3 * (255 ** 2))
213
213
  end
214
214
 
215
+ # Return an array representation of a {Color} instance,
216
+ # @param [Symbol] model the color model, should be :rgb or :hsl
217
+ # @return [Array] an array of component values
218
+ def to_array(color_model = :rgb)
219
+ color_model = color_model.to_sym unless color_model.is_a? Symbol
220
+ if color_model == :rgb
221
+ array = [self.red, self.green, self.blue]
222
+ elsif color_model == :hsl
223
+ array = [self.hue, self.saturation, self.lightness]
224
+ else
225
+ raise(ArgumentError, "Argument must be :rgb or :hsl")
226
+ end
227
+ array
228
+ end
229
+
215
230
  private
216
231
 
217
232
  def rgb_init(red = 0, green = 0, blue = 0)
@@ -26,6 +26,7 @@ module Math
26
26
  dx.zip(dy, dz).each do |x, y, z|
27
27
  sxx += x ** 2
28
28
  syy += y ** 2
29
+ szz += z ** 2
29
30
  sxy += x * y
30
31
  szx += z * x
31
32
  syz += y * z
@@ -213,6 +213,21 @@ module Paleta
213
213
  end
214
214
  end
215
215
 
216
+ # Return an array representation of a {Palette} instance,
217
+ # @param [Symbol] model the color model, should be :rgb, :hsl, or :hex
218
+ # @return [Array] an Array of Arrays where each sub-Array is a representation of a {Color} object in a {Palette} instance
219
+ def to_array(color_model = :rgb)
220
+ color_model = color_model.to_sym unless color_model.is_a? Symbol
221
+ if [:rgb, :hsl].include?(color_model)
222
+ array = colors.map { |c| c.to_array(color_model) }
223
+ elsif color_model == :hex
224
+ array = colors.map{ |c| c.hex }
225
+ else
226
+ raise(ArgumentError, "Argument must be :rgb, :hsl, or :hex")
227
+ end
228
+ array
229
+ end
230
+
216
231
  private
217
232
 
218
233
  def self.generate_analogous_from_color(color, size)
@@ -1,3 +1,3 @@
1
1
  module Paleta
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
data/readme.markdown CHANGED
@@ -45,6 +45,12 @@ Individual component values can be accessed by name
45
45
  color.lightness # => 64.50980392156862
46
46
  color.hex # => "5EA1EB"
47
47
 
48
+
49
+ Get an array representation of a Color
50
+
51
+ c = Paleta::Color.new(30, 90, 120)
52
+ c.to_array(:rgb) # => [30, 90, 120]
53
+
48
54
  #### Manipulating Colors
49
55
 
50
56
  Colors can be lightened or darkened by a percentage
@@ -100,6 +106,14 @@ Colors can be accessed and removed by index.
100
106
  palette[1] # => color2
101
107
 
102
108
  palette.delete_at(2)
109
+
110
+ Get an array representation of a Palette
111
+
112
+ c1 = Paleta::Color.new(13, 57, 182)
113
+ c2 = Paleta::Color.new(94, 161, 235)
114
+ palette = Paleta::Palette.new(c1, c2)
115
+
116
+ palette.to_array(:rgb) # => [[13, 57, 182], [94, 161, 235]]
103
117
 
104
118
  #### Manipulating Palettes
105
119
 
@@ -241,4 +241,13 @@ describe Paleta::Color do
241
241
  color.saturation.should == 0
242
242
  color.lightness.should == 100
243
243
  end
244
+
245
+ it "should return an array of component values" do
246
+ c = Paleta::Color.new(30, 90, 120)
247
+ rgb_array = [c.red, c.green, c.blue]
248
+ hsl_array = [c.hue, c.saturation, c.lightness]
249
+ c.to_array.should == rgb_array
250
+ c.to_array(:rgb).should == rgb_array
251
+ c.to_array(:hsl).should == hsl_array
252
+ end
244
253
  end
@@ -99,12 +99,12 @@ describe Paleta::Palette do
99
99
  c3 = Paleta::Color.new(237, 172, 33)
100
100
  palette = Paleta::Palette.new(c1, c2, c3)
101
101
  r = palette.fit
102
- r[:slope][:x].should == 0.19585157930194594
103
- r[:slope][:y].should == 0.5276697919048708
104
- r[:slope][:z].should == 0.09102564102564102
105
- r[:offset][:x].should == 127.54235224004354
106
- r[:offset][:y].should == 50.849531214269376
107
- r[:offset][:z].should == 102.83333333333333
102
+ r[:slope][:x].round(5).should == 0.19585
103
+ r[:slope][:y].round(5).should == 0.52767
104
+ r[:slope][:z].round(5).should == -0.11913
105
+ r[:offset][:x].round(5).should == 127.54235
106
+ r[:offset][:y].round(5).should == 50.84953
107
+ r[:offset][:z].round(5).should == 130.15404
108
108
  end
109
109
 
110
110
  it "should calculate its similarity to another Palette" do
@@ -132,7 +132,7 @@ describe Paleta::Palette do
132
132
  c9 = Paleta::Color.new(13, 57, 182)
133
133
  c10 = Paleta::Color.new(94, 161, 235)
134
134
  p6 = Paleta::Palette.new(c9, c10)
135
- p5.similarity(p6).round(5).should == 0.00627
135
+ p5.similarity(p6).round(5).should == 0.00669
136
136
  end
137
137
 
138
138
  it "should generate a new Palette of shades of a single Color" do
@@ -235,4 +235,16 @@ describe Paleta::Palette do
235
235
  it "should raise an error when generating a Palette from an invalid image" do
236
236
  expect{ Paleta::Palette.generate(:from => :image, :image => "/no/image.here") }.to raise_error(RuntimeError)
237
237
  end
238
+
239
+ it "should return an array of colors, where each color is represented as an array of component values" do
240
+ c1 = Paleta::Color.new(13, 57, 182)
241
+ c2 = Paleta::Color.new(94, 161, 235)
242
+ palette = Paleta::Palette.new(c1, c2)
243
+ rgb_palette_array = [c1.to_array, c2.to_array]
244
+ hsl_palette_array = [c1.to_array(:hsl), c2.to_array(:hsl)]
245
+ palette.to_array.should == rgb_palette_array
246
+ palette.to_array(:rgb).should == rgb_palette_array
247
+ palette.to_array(:hsl).should == hsl_palette_array
248
+ palette.to_array(:hex).should == ['0D39B6', '5EA1EB']
249
+ end
238
250
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paleta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-02 00:00:00.000000000Z
12
+ date: 2012-03-13 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: A gem for working with color palettes
15
15
  email: