paleta 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +6 -0
- data/lib/paleta.rb +6 -0
- data/lib/paleta/color.rb +117 -0
- data/lib/paleta/palette.rb +25 -0
- data/lib/paleta/version.rb +3 -0
- data/paleta.gemspec +17 -0
- data/spec/models/color_spec.rb +59 -0
- data/spec/models/palette_spec.rb +31 -0
- data/spec/spec_helper.rb +2 -0
- metadata +58 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/lib/paleta.rb
ADDED
data/lib/paleta/color.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
module Paleta
|
2
|
+
class Color
|
3
|
+
|
4
|
+
attr_reader :red, :green, :blue, :hue, :saturation, :lightness
|
5
|
+
|
6
|
+
def initialize(red = 0, green = 0, blue = 0)
|
7
|
+
self.red = red
|
8
|
+
self.green = green
|
9
|
+
self.blue = blue
|
10
|
+
end
|
11
|
+
|
12
|
+
def red=(val)
|
13
|
+
@red = range_validator(val, 0..255)
|
14
|
+
update_hsl
|
15
|
+
end
|
16
|
+
|
17
|
+
def green=(val)
|
18
|
+
@green = range_validator(val, 0..255)
|
19
|
+
update_hsl
|
20
|
+
end
|
21
|
+
|
22
|
+
def blue=(val)
|
23
|
+
@blue = range_validator(val, 0..255)
|
24
|
+
update_hsl
|
25
|
+
end
|
26
|
+
|
27
|
+
def lightness=(val)
|
28
|
+
@lightness = range_validator(val, 0..100)
|
29
|
+
update_rgb
|
30
|
+
end
|
31
|
+
|
32
|
+
def saturation=(val)
|
33
|
+
@saturation = range_validator(val, 0..100)
|
34
|
+
update_rgb
|
35
|
+
end
|
36
|
+
|
37
|
+
def hue=(val)
|
38
|
+
@hue = range_validator(val, 0..360)
|
39
|
+
update_rgb
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def update_hsl
|
45
|
+
r = @red / 255.0 rescue 0
|
46
|
+
g = @green / 255.0 rescue 0
|
47
|
+
b = @blue / 255.0 rescue 0
|
48
|
+
|
49
|
+
min = [r, g, b].min
|
50
|
+
max = [r, g, b].max
|
51
|
+
delta = max - min
|
52
|
+
|
53
|
+
@hue = 0
|
54
|
+
@saturation = 0
|
55
|
+
@lightness = (max + min) / 2.0
|
56
|
+
|
57
|
+
if delta != 0
|
58
|
+
@saturation = (@lightness < 0.5) ? delta / (max + min) : delta / (2.0 - max - min)
|
59
|
+
case max
|
60
|
+
when r; @hue = (g - b) / delta
|
61
|
+
when g; @hue = 2 + (b - r) / delta
|
62
|
+
when b; @hue = 4 + (r - g) / delta
|
63
|
+
end
|
64
|
+
end
|
65
|
+
@hue *= 60
|
66
|
+
@hue += 360 if @hue < 0
|
67
|
+
@saturation *= 100
|
68
|
+
@lightness *= 100
|
69
|
+
end
|
70
|
+
|
71
|
+
def update_rgb
|
72
|
+
|
73
|
+
h = @hue / 360.0
|
74
|
+
s = @saturation / 100.0
|
75
|
+
l = @lightness / 100.0
|
76
|
+
|
77
|
+
if s == 0
|
78
|
+
r = l * 255
|
79
|
+
g = l * 255
|
80
|
+
b = l * 255
|
81
|
+
else
|
82
|
+
th = h / 6.0
|
83
|
+
if l < 0.5
|
84
|
+
t2 = l * (s + 1)
|
85
|
+
else
|
86
|
+
t2 = (l + s) - (l * s)
|
87
|
+
end
|
88
|
+
t1 = 2 * l - t2
|
89
|
+
|
90
|
+
tr = th + (1.0 / 3.0)
|
91
|
+
tg = th
|
92
|
+
tb = th - (1.0 / 3.0)
|
93
|
+
|
94
|
+
tr = hue_calc(tr, t1, t2)
|
95
|
+
tg = hue_calc(tg, t1, t2)
|
96
|
+
tb = hue_calc(tb, t1, t2)
|
97
|
+
|
98
|
+
@red = tr * 255.0
|
99
|
+
@green = tg * 255.0
|
100
|
+
@blue = tb * 255.0
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def hue_calc(value, t1, t2)
|
105
|
+
value += 1 if value < 0
|
106
|
+
value -= 1 if value > 1
|
107
|
+
return (t1 + (t2 - t1) * 6 * value) if 6 * value < 1
|
108
|
+
return t2 if 2 * value < 1
|
109
|
+
return (t1 + (t2 - t1) * (2.0 / 3.0 - value) * 6) if 3 * value < 2
|
110
|
+
return t1;
|
111
|
+
end
|
112
|
+
|
113
|
+
def range_validator(val, range)
|
114
|
+
range.include?(val) ? val : raise(ArgumentError, "Component range exceeded")
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Paleta
|
2
|
+
class Palette
|
3
|
+
|
4
|
+
attr_accessor :colors
|
5
|
+
|
6
|
+
def initialize(*colors)
|
7
|
+
@colors = []
|
8
|
+
colors.each do |color|
|
9
|
+
self << color
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def <<(color)
|
14
|
+
color.is_a?(Color) ? @colors << color : raise(ArgumentError, "Passed argument is not a Color")
|
15
|
+
end
|
16
|
+
|
17
|
+
def [](i)
|
18
|
+
@colors[i]
|
19
|
+
end
|
20
|
+
|
21
|
+
def include?(color)
|
22
|
+
@colors.include?(color)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/paleta.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/paleta/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ['Jordan Stephens']
|
6
|
+
gem.email = ['iam@jordanstephens.net']
|
7
|
+
gem.description = 'color palette gem'
|
8
|
+
gem.summary = 'color palette gem'
|
9
|
+
gem.homepage = 'http://jordanstephens.net'
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = 'paleta'
|
15
|
+
gem.require_paths = ['lib']
|
16
|
+
gem.version = Paleta::VERSION
|
17
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Paleta::Color do
|
4
|
+
it "should initialize with components in 0..255" do
|
5
|
+
color = Paleta::Color.new(94, 161, 235)
|
6
|
+
color.red.should == 94
|
7
|
+
color.green.should == 161
|
8
|
+
color.blue.should == 235
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should not initialize with components not in 0..255" do
|
12
|
+
expect{ Paleta::Color.new(-74, 333, 4321) }.to raise_error
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should calculate its HSL value on itialization" do
|
16
|
+
color = Paleta::Color.new(237, 172, 33)
|
17
|
+
color.hue.to_i.should == 40
|
18
|
+
color.saturation.to_i.should == 85
|
19
|
+
color.lightness.to_i.should == 52
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should update its HSL value when its RGB value is updated" do
|
23
|
+
color = Paleta::Color.new(237, 172, 33)
|
24
|
+
|
25
|
+
color.red = 0
|
26
|
+
color.hue.to_i.should == 131
|
27
|
+
color.saturation.to_i.should == 100
|
28
|
+
color.lightness.to_i.should == 33
|
29
|
+
|
30
|
+
color.green = 123
|
31
|
+
color.hue.to_i.should == 136
|
32
|
+
color.saturation.to_i.should == 100
|
33
|
+
color.lightness.to_i.should == 24
|
34
|
+
|
35
|
+
color.blue = 241
|
36
|
+
color.hue.to_i.should == 209
|
37
|
+
color.saturation.to_i.should == 100
|
38
|
+
color.lightness.to_i.should == 47
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should update its RGB value when its HSL value is updated" do
|
42
|
+
color = Paleta::Color.new(0, 0, 255)
|
43
|
+
|
44
|
+
color.hue = 120
|
45
|
+
color.red.to_i.should == 255
|
46
|
+
color.green.to_i.should == 85
|
47
|
+
color.blue.to_i.should == 0
|
48
|
+
|
49
|
+
color.saturation = 50
|
50
|
+
color.red.to_i.should == 191
|
51
|
+
color.green.to_i.should == 106
|
52
|
+
color.blue.to_i.should == 63
|
53
|
+
|
54
|
+
color.lightness = 80
|
55
|
+
color.red.to_i.should == 229
|
56
|
+
color.green.to_i.should == 195
|
57
|
+
color.blue.to_i.should == 178
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Paleta::Palette do
|
4
|
+
|
5
|
+
it "should initialize with a set of Colors" do
|
6
|
+
c1 = Paleta::Color.new(13, 57, 182)
|
7
|
+
c2 = Paleta::Color.new(94, 161, 235)
|
8
|
+
Paleta::Palette.new(c1, c2)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should not initialize if an object in the set is not a Color" do
|
12
|
+
c1 = Paleta::Color.new(13, 57, 182)
|
13
|
+
c2 = 13
|
14
|
+
expect{ Paleta::Palette.new(c1, c2) }.to raise_error
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should add colors to an initialized palette" do
|
18
|
+
c1 = Paleta::Color.new(13, 57, 182)
|
19
|
+
c2 = Paleta::Color.new(94, 161, 235)
|
20
|
+
palette = Paleta::Palette.new(c1)
|
21
|
+
palette << c2
|
22
|
+
palette.include?(c2).should be_true
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should allow array-style accessing of colors" do
|
26
|
+
c1 = Paleta::Color.new(13, 57, 182)
|
27
|
+
palette = Paleta::Palette.new(c1)
|
28
|
+
palette[0].should == c1
|
29
|
+
palette[1].should be_nil
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: paleta
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jordan Stephens
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-02 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: color palette gem
|
15
|
+
email:
|
16
|
+
- iam@jordanstephens.net
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- lib/paleta.rb
|
24
|
+
- lib/paleta/color.rb
|
25
|
+
- lib/paleta/palette.rb
|
26
|
+
- lib/paleta/version.rb
|
27
|
+
- paleta.gemspec
|
28
|
+
- spec/models/color_spec.rb
|
29
|
+
- spec/models/palette_spec.rb
|
30
|
+
- spec/spec_helper.rb
|
31
|
+
homepage: http://jordanstephens.net
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.10
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: color palette gem
|
55
|
+
test_files:
|
56
|
+
- spec/models/color_spec.rb
|
57
|
+
- spec/models/palette_spec.rb
|
58
|
+
- spec/spec_helper.rb
|