color_picker 0.0.7 → 0.1.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +23 -0
- data/lib/color_picker/color.rb +20 -21
- data/lib/color_picker/palette.rb +81 -49
- data/lib/color_picker/version.rb +1 -1
- data/test/color_test.rb +14 -23
- data/test/palette_test.rb +40 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f97b3009f023d99ae7115eb7bae83e352dcd0b51
|
4
|
+
data.tar.gz: 689323df56d734437535f39a972c44fb8e780caf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0171d1a53993edfe89b2c961cd1a394f020795c96adf5cd1867d9eac6b451a2b1f513505337870d2166b07dbc494423f71b1896449633b96ed8be4f98408dabe
|
7
|
+
data.tar.gz: 4bad23c2243d324fac2760dfb6ef42f8ef0511ad907cc593314451ba70db706f7b92569cca0e23355afba7455c73a25903d163a3067304e37ad9980e1a42800d
|
data/CHANGELOG.md
CHANGED
@@ -23,3 +23,26 @@
|
|
23
23
|
* add coveralls gem and require in test suite
|
24
24
|
* add custom palette method in order to allow ser you own range
|
25
25
|
* rgb ranges only can be read
|
26
|
+
|
27
|
+
### VERSION 0.1.0
|
28
|
+
* change gem behavior, know Palette class is the interface to access and use the gem
|
29
|
+
* Colorpicker::Palette.new know receives options template red_color_range
|
30
|
+
green_color_branch and blue_color_range
|
31
|
+
======
|
32
|
+
* New methods in Palette
|
33
|
+
* generate (interface to create a new palette base on templates or custom)
|
34
|
+
* :strong (a owner default palette with range of strong colors)
|
35
|
+
* :complete (the complete palette of hex colors 255*255*255)
|
36
|
+
* rgb_range (return the range )
|
37
|
+
* html (generate an html Warning! with large range of colors)
|
38
|
+
* __sample color__ (select a color of the range a return Color.new)
|
39
|
+
* new (also can receive options to generate a palette)
|
40
|
+
======
|
41
|
+
* New methods in Color
|
42
|
+
* new receive a code an string or and array
|
43
|
+
* to_rgb (convert and hex code to rgb representation return an array)
|
44
|
+
* to_hex (convert and rgb code to hex code representation return an string)
|
45
|
+
* to_s (print color like hexadecimal string or rgb with css style)
|
46
|
+
|
47
|
+
|
48
|
+
|
data/lib/color_picker/color.rb
CHANGED
@@ -1,30 +1,29 @@
|
|
1
1
|
module ColorPicker
|
2
2
|
class Color
|
3
|
-
|
4
|
-
|
3
|
+
attr_reader :hex_code, :rgb_code
|
4
|
+
attr_accessor :hex_code, :rgb_code
|
5
5
|
|
6
|
-
def initialize
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
6
|
+
def initialize(code)
|
7
|
+
@hex_code = code.to_str if code.respond_to?(:to_str)
|
8
|
+
@rgb_code = code.to_ary unless @hex_code
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_rgb
|
12
|
+
return self.to_s unless @hex_code
|
13
|
+
@hex_code.scan(/.{2}/).map{|color| color.to_i(16)}
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
@code
|
22
|
-
end
|
16
|
+
def to_hex
|
17
|
+
return self.to_s unless @rgb_code
|
18
|
+
@rgb_code.map{|number| number.to_s(16)}.join.rjust(6, '0')
|
19
|
+
end
|
23
20
|
|
24
|
-
|
25
|
-
|
26
|
-
|
21
|
+
def to_s(type=nil)
|
22
|
+
return "rgb(#{rgb_code.join(', ')})" if type == :rgb || !rgb_code.nil?
|
23
|
+
"##{hex_code}"
|
24
|
+
end
|
27
25
|
|
26
|
+
private :rgb_code=, :hex_code=
|
28
27
|
|
29
28
|
end
|
30
29
|
end
|
data/lib/color_picker/palette.rb
CHANGED
@@ -1,62 +1,94 @@
|
|
1
1
|
module ColorPicker
|
2
|
-
class Palette
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
eval "#{type}"
|
14
|
-
end
|
2
|
+
class Palette
|
3
|
+
#palette templates
|
4
|
+
TEMPLATES = {
|
5
|
+
complete: {template: :complete, red_color_range: 0..255, green_color_range: 0..255, blue_color_range: 0..255 },
|
6
|
+
strong: {template: :strong, red_color_range: 0..7, green_color_range: 0..9, blue_color_range: 0..255},
|
7
|
+
skeleton: {template: nil, red_color_range: 0..0, green_color_range: 0..0, blue_color_range: 0..0}
|
8
|
+
}
|
9
|
+
|
10
|
+
attr_reader :rgb_range, :max_number_of_colors, :template,
|
11
|
+
:blue_color_range, :green_color_range, :red_color_range
|
12
|
+
attr_accessor :blue_color_range, :green_color_range, :red_color_range, :template
|
15
13
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
generate_palette
|
21
|
-
end
|
22
|
-
|
23
|
-
def strong_colors
|
24
|
-
@red_color_range = 0 .. 7
|
25
|
-
@green_color_range = 0 .. 9
|
26
|
-
@blue_color_range = 0 .. 159
|
27
|
-
generate_palette
|
28
|
-
end
|
29
|
-
|
30
|
-
def custom_palette(red, green, blue)
|
31
|
-
@red_color_range = red
|
32
|
-
@green_color_range = green
|
33
|
-
@blue_color_range = blue
|
34
|
-
generate_palette
|
14
|
+
def initialize(options = {})
|
15
|
+
default_options.merge(options).each_pair do |key, value|
|
16
|
+
eval "@#{key}= value"
|
17
|
+
end
|
35
18
|
end
|
36
19
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
20
|
+
def rgb_range
|
21
|
+
@rgb_range = [red_color_range,green_color_range,blue_color_range]
|
22
|
+
end
|
23
|
+
|
24
|
+
def html
|
25
|
+
html = "<div style='width:600px;height:auto;float:left;clear:both'>"
|
26
|
+
range = max_number_of_colors
|
27
|
+
range = 0 if max_number_of_colors == 1
|
28
|
+
(0 .. range).each do |color_code|
|
29
|
+
color_container = "<div style='background:##{color_code.to_s(16).rjust(6, '0')};width:60px;height:60px;border:1px solid #000;'> </div><div style='float:left'>#{color_code.to_s(16).rjust(6, '0')}</div>"
|
30
|
+
html += color_container
|
43
31
|
end
|
44
|
-
|
32
|
+
html += "</div>"
|
33
|
+
to_html { html }
|
45
34
|
end
|
35
|
+
|
36
|
+
def generate(options={})
|
37
|
+
opts(options).each_pair do |key, value|
|
38
|
+
eval "self.#{key}=value"
|
39
|
+
end
|
40
|
+
rgb_range
|
41
|
+
end
|
42
|
+
|
43
|
+
def max_number_of_colors
|
44
|
+
(red_color_range.end+1) * (green_color_range.end+1) * (blue_color_range.end+1)
|
45
|
+
end
|
46
|
+
|
47
|
+
def sample_color
|
48
|
+
ary_code = rgb_range.map do |range_color|
|
49
|
+
range_color.to_a.sample.to_s(16).rjust(2, "0")
|
50
|
+
end
|
51
|
+
Color.new(ary_code.join)
|
52
|
+
end
|
46
53
|
|
47
54
|
def method_missing(m, *args, &block)
|
48
|
-
|
55
|
+
"Colors and stars something wrong. Palette types available :complete and :strong via template option"
|
49
56
|
end
|
57
|
+
|
58
|
+
private :red_color_range=, :blue_color_range=, :green_color_range=, :template=
|
50
59
|
|
51
|
-
private
|
52
|
-
|
53
|
-
|
54
|
-
@range = [@red_color_range,@green_color_range,@blue_color_range]
|
55
|
-
end
|
56
|
-
|
57
|
-
def max_colors
|
58
|
-
@max_number_of_colors = (@red_color_range.end+1) * (@green_color_range.end+1) * (@blue_color_range.end+1)
|
60
|
+
private
|
61
|
+
def default_options
|
62
|
+
TEMPLATES[:skeleton]
|
59
63
|
end
|
60
|
-
|
64
|
+
|
65
|
+
def opts(options={})
|
66
|
+
case options[:template]
|
67
|
+
when :strong
|
68
|
+
TEMPLATES[:strong].merge(options)
|
69
|
+
when :complete
|
70
|
+
TEMPLATES[:complete].merge(options)
|
71
|
+
else
|
72
|
+
TEMPLATES[:skeleton].merge(options)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def to_html
|
77
|
+
"<!DOCTYPE HTML>
|
78
|
+
|
79
|
+
<html>
|
80
|
+
|
81
|
+
<head>
|
82
|
+
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
|
83
|
+
<title>Palette Html</title>
|
84
|
+
</head>
|
85
|
+
|
86
|
+
<body>
|
87
|
+
#{yield}
|
88
|
+
</body>
|
89
|
+
</html>
|
90
|
+
"
|
91
|
+
end
|
92
|
+
|
61
93
|
end
|
62
94
|
end
|
data/lib/color_picker/version.rb
CHANGED
data/test/color_test.rb
CHANGED
@@ -2,41 +2,32 @@ require "test_helper"
|
|
2
2
|
module ColorPicker
|
3
3
|
class ColorTest < Minitest::Test
|
4
4
|
def setup
|
5
|
-
@color = ColorPicker::Color.new
|
5
|
+
@color = ColorPicker::Color.new("000000")
|
6
6
|
end
|
7
7
|
|
8
|
-
def
|
9
|
-
|
8
|
+
def test_initialize_color
|
9
|
+
assert_equal "000000", @color.hex_code
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
|
14
|
-
assert_equal pre_generate_code, @color.code
|
12
|
+
def test_convert_to_rgb
|
13
|
+
assert_equal [0,0,0], ColorPicker::Color.new([0,0,0]).rgb_code
|
15
14
|
end
|
16
15
|
|
17
|
-
def
|
18
|
-
@color.
|
19
|
-
assert_equal [0..255, 0..255, 0..255], @color.palette.range
|
16
|
+
def test_to_s_hexadecimal
|
17
|
+
assert_equal "#000000", @color.to_s
|
20
18
|
end
|
21
19
|
|
22
|
-
def
|
23
|
-
|
24
|
-
assert_equal [0..7, 0..9, 0..159], @color.palette.range
|
20
|
+
def test_to_s_rgb
|
21
|
+
assert_equal "rgb(0, 0, 0)", ColorPicker::Color.new([0,0,0]).to_s(:rgb)
|
25
22
|
end
|
26
23
|
|
27
|
-
def
|
28
|
-
@color.
|
29
|
-
max_range = (@color.palette.red_color_range.end+1) * (@color.palette.green_color_range.end+1) * (@color.palette.blue_color_range.end+1)
|
30
|
-
colors = []
|
31
|
-
10.times { colors << @color.generate }
|
32
|
-
colors.each do |color|
|
33
|
-
assert_match (/[0-7]{2}[0-9]{2}[0-9|a-fA-F]{2}/), color
|
34
|
-
end
|
35
|
-
assert_equal @color.palette.max_number_of_colors, max_range
|
24
|
+
def test_hex_to_rgb
|
25
|
+
assert_equal [0,0,0], @color.to_rgb
|
36
26
|
end
|
37
27
|
|
38
|
-
def
|
39
|
-
|
28
|
+
def test_rgb_to_hex
|
29
|
+
assert_equal "000000", ColorPicker::Color.new([0,0,0]).to_hex
|
40
30
|
end
|
31
|
+
|
41
32
|
end
|
42
33
|
end
|
data/test/palette_test.rb
CHANGED
@@ -5,23 +5,54 @@ module ColorPicker
|
|
5
5
|
@palette = ColorPicker::Palette.new
|
6
6
|
end
|
7
7
|
|
8
|
+
def test_initialize_complete_range
|
9
|
+
assert_equal [0..0, 0..0, 0..0], @palette.rgb_range
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_initialize_complete_palette_template
|
13
|
+
assert_equal nil, @palette.template
|
14
|
+
end
|
15
|
+
|
8
16
|
def test_complete_palette
|
9
|
-
assert_equal [0..255, 0 .. 255, 0 .. 255], @palette.
|
17
|
+
assert_equal [0..255, 0 .. 255, 0 .. 255], @palette.generate(template: :complete)
|
10
18
|
end
|
11
19
|
|
12
|
-
def
|
13
|
-
assert_equal [0..7, 0 .. 9, 0
|
20
|
+
def test_strong_paletter
|
21
|
+
assert_equal [0..7, 0 .. 9, 0..255], @palette.generate(template: :strong)
|
14
22
|
end
|
15
23
|
|
16
24
|
def test_to_html
|
17
|
-
|
18
|
-
@palette.
|
19
|
-
assert_match
|
20
|
-
<div style='float=left'>010101<\/div><\/div>/), @palette.to_html
|
25
|
+
@palette.generate(template: :html)
|
26
|
+
assert_match /DOCTYPE HTML/, @palette.html
|
27
|
+
#assert_match /<div style='width:600px;height:auto;float:left;clear:both'><div style='background:#000000;width:60px;height:60px;border:1px solid #000'> <\/div><div style='float:left'>000000<\/div><\/div>/, @palette.to_html
|
21
28
|
end
|
22
29
|
|
23
|
-
def
|
24
|
-
assert_equal [0..1, 0..1, 0..1], @palette.
|
30
|
+
def test_generate_custom_palette
|
31
|
+
assert_equal [0..1, 0..1, 0..1], @palette.generate(template: :custom, red_color_range: 0..1, green_color_range: 0..1, blue_color_range: 0..1)
|
25
32
|
end
|
33
|
+
|
34
|
+
def test_sample_color_return_color_instance
|
35
|
+
assert_kind_of Color, @palette.sample_color
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_sample_color
|
39
|
+
assert_match /([0-9]|[a-fA-F]){5,6}/, @palette.sample_color.to_s
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_strong_colors_palette_range
|
43
|
+
@palette.generate(template: :strong)
|
44
|
+
max_range = (@palette.red_color_range.end+1) * (@palette.green_color_range.end+1) * (@palette.blue_color_range.end+1)
|
45
|
+
colors = []
|
46
|
+
10.times { colors << @palette.sample_color.to_s }
|
47
|
+
colors.each do |color|
|
48
|
+
assert_match (/[0-7]{2}[0-9]{2}[0-9|a-fA-F]{2}/), color
|
49
|
+
end
|
50
|
+
assert_equal @palette.max_number_of_colors, max_range
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_method_missing
|
54
|
+
assert_match "Colors and stars something wrong. Palette types available :complete and :strong via template option", @palette.undefined
|
55
|
+
end
|
56
|
+
|
26
57
|
end
|
27
58
|
end
|