color_picker 0.1.0 → 0.1.2
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 +5 -0
- data/README.md +42 -1
- data/lib/color_picker/color.rb +17 -17
- data/lib/color_picker/palette.rb +69 -73
- data/lib/color_picker/version.rb +1 -1
- data/test/color_test.rb +23 -23
- data/test/palette_test.rb +67 -55
- 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: 0fe4fbbbf1235475e1501e9751dd54881671b0b0
|
4
|
+
data.tar.gz: 5789d0af9689a52b59d489e074272bf4d4cd4d18
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9905256a5b440772a402e07be46c7198b3c2d9231e93586c2253ae94105706d628449113900722ea974a6d89751dff7fac277be91efaef512826e79a009a8a3e
|
7
|
+
data.tar.gz: 1a0949de602310fd32759618932218abdca41db450418351519b72a2a7b3cf48e64e66fa098f5d9019498612cedbf5561dea5327f9c3c09a95f898acc6959904
|
data/CHANGELOG.md
CHANGED
@@ -43,6 +43,11 @@
|
|
43
43
|
* to_rgb (convert and hex code to rgb representation return an array)
|
44
44
|
* to_hex (convert and rgb code to hex code representation return an string)
|
45
45
|
* to_s (print color like hexadecimal string or rgb with css style)
|
46
|
+
### VERSION 0.1.1
|
47
|
+
* fix initialize with options
|
48
|
+
* change indentation tabs to spaces
|
49
|
+
### VERSION 0.1.2
|
50
|
+
* depreacate default options in palette
|
46
51
|
|
47
52
|
|
48
53
|
|
data/README.md
CHANGED
@@ -1,5 +1,46 @@
|
|
1
1
|
Color Picker
|
2
2
|
============
|
3
3
|
[](https://travis-ci.org/bernardogalindo/color_picker)
|
4
|
-
|
4
|
+
[](https://coveralls.io/r/bernardogalindo/color_picker?branch=master)
|
5
5
|
a tool to get a color based on color palette, also have a method to export to html all the color palette
|
6
|
+
|
7
|
+
-----------
|
8
|
+
### Example
|
9
|
+
* generate a complete palette
|
10
|
+
```ruby
|
11
|
+
@palette= ColorPicker::Palette.new(template: :complete)
|
12
|
+
```
|
13
|
+
* then you have a complete range of colors
|
14
|
+
```ruby
|
15
|
+
@palette.rgb
|
16
|
+
```
|
17
|
+
* You can get a sample color
|
18
|
+
```ruby
|
19
|
+
@palette.sample_color
|
20
|
+
> #<ColorPicker::Color:0x007f4dc236ac10 @hex_code="000000">
|
21
|
+
```
|
22
|
+
* generate a strong palette
|
23
|
+
```ruby
|
24
|
+
@palette.generate(template: :strong)
|
25
|
+
> [0..7, 0..9, 0..255]
|
26
|
+
```
|
27
|
+
|
28
|
+
#### Color
|
29
|
+
*initialize with string or array of rgb number
|
30
|
+
```ruby
|
31
|
+
@color = ColorPicker::Color.new("000000")
|
32
|
+
```
|
33
|
+
* initialize with rgb array range
|
34
|
+
```ruby
|
35
|
+
@color = ColorPicker::Color.new([255, 255,255])
|
36
|
+
```
|
37
|
+
*converters
|
38
|
+
```ruby
|
39
|
+
@color.to_rgb
|
40
|
+
@color.to_hex
|
41
|
+
```
|
42
|
+
*printers
|
43
|
+
```ruby
|
44
|
+
@color.to_s
|
45
|
+
```
|
46
|
+
|
data/lib/color_picker/color.rb
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
module ColorPicker
|
2
2
|
class Color
|
3
3
|
attr_reader :hex_code, :rgb_code
|
4
|
-
|
4
|
+
attr_accessor :hex_code, :rgb_code
|
5
5
|
|
6
6
|
def initialize(code)
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
@hex_code = code.to_str if code.respond_to?(:to_str)
|
8
|
+
@rgb_code = code.to_ary unless @hex_code
|
9
|
+
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
-
|
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
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
def to_s(type=nil)
|
22
|
+
return "rgb(#{rgb_code.join(', ')})" if type == :rgb || !rgb_code.nil?
|
23
|
+
"##{hex_code}"
|
24
|
+
end
|
25
25
|
|
26
|
-
|
26
|
+
private :rgb_code=, :hex_code=
|
27
27
|
|
28
28
|
end
|
29
29
|
end
|
data/lib/color_picker/palette.rb
CHANGED
@@ -1,94 +1,90 @@
|
|
1
1
|
module ColorPicker
|
2
2
|
class Palette
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
10
|
attr_reader :rgb_range, :max_number_of_colors, :template,
|
11
|
-
|
12
|
-
|
11
|
+
:blue_color_range, :green_color_range, :red_color_range
|
12
|
+
attr_accessor :blue_color_range, :green_color_range, :red_color_range, :template
|
13
13
|
|
14
14
|
def initialize(options = {})
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
opts(options).each_pair do |key, value|
|
16
|
+
eval "@#{key}= value"
|
17
|
+
end
|
18
|
+
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
def rgb_range
|
21
|
+
@rgb_range = [red_color_range,green_color_range,blue_color_range]
|
22
|
+
end
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
28
|
(0 .. range).each do |color_code|
|
29
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
30
|
html += color_container
|
31
31
|
end
|
32
32
|
html += "</div>"
|
33
|
-
|
33
|
+
to_html { html }
|
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)
|
34
45
|
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
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
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
|
53
53
|
|
54
54
|
def method_missing(m, *args, &block)
|
55
|
-
|
55
|
+
"Colors and stars something wrong. Palette types available :complete and :strong via template option"
|
56
56
|
end
|
57
|
-
|
58
|
-
|
57
|
+
|
58
|
+
private :red_color_range=, :blue_color_range=, :green_color_range=, :template=
|
59
59
|
|
60
|
-
private
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
else
|
72
|
-
TEMPLATES[:skeleton].merge(options)
|
73
|
-
end
|
74
|
-
end
|
60
|
+
private
|
61
|
+
def opts(options={})
|
62
|
+
case options[:template]
|
63
|
+
when :strong
|
64
|
+
TEMPLATES[:strong].merge(options)
|
65
|
+
when :complete
|
66
|
+
TEMPLATES[:complete].merge(options)
|
67
|
+
else
|
68
|
+
TEMPLATES[:skeleton].merge(options)
|
69
|
+
end
|
70
|
+
end
|
75
71
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
72
|
+
def to_html
|
73
|
+
"<!DOCTYPE HTML>
|
74
|
+
|
75
|
+
<html>
|
76
|
+
|
77
|
+
<head>
|
78
|
+
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
|
79
|
+
<title>Palette Html</title>
|
80
|
+
</head>
|
81
|
+
|
82
|
+
<body>
|
83
|
+
#{yield}
|
84
|
+
</body>
|
85
|
+
</html>
|
86
|
+
"
|
87
|
+
end
|
88
|
+
|
93
89
|
end
|
94
90
|
end
|
data/lib/color_picker/version.rb
CHANGED
data/test/color_test.rb
CHANGED
@@ -1,33 +1,33 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
module ColorPicker
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
class ColorTest < Minitest::Test
|
4
|
+
def setup
|
5
|
+
@color = ColorPicker::Color.new("000000")
|
6
|
+
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
def test_initialize_color
|
9
|
+
assert_equal "000000", @color.hex_code
|
10
|
+
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
def test_convert_to_rgb
|
13
|
+
assert_equal [0,0,0], ColorPicker::Color.new([0,0,0]).rgb_code
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
def test_to_s_hexadecimal
|
17
|
+
assert_equal "#000000", @color.to_s
|
18
|
+
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
def test_to_s_rgb
|
21
|
+
assert_equal "rgb(0, 0, 0)", ColorPicker::Color.new([0,0,0]).to_s(:rgb)
|
22
|
+
end
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
def test_hex_to_rgb
|
25
|
+
assert_equal [0,0,0], @color.to_rgb
|
26
|
+
end
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
def test_rgb_to_hex
|
29
|
+
assert_equal "000000", ColorPicker::Color.new([0,0,0]).to_hex
|
30
|
+
end
|
31
31
|
|
32
|
-
|
32
|
+
end
|
33
33
|
end
|
data/test/palette_test.rb
CHANGED
@@ -1,58 +1,70 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
module ColorPicker
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
3
|
+
class PaletteTest < Minitest::Test
|
4
|
+
def setup
|
5
|
+
@palette = ColorPicker::Palette.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_initialize_default
|
9
|
+
assert_equal [0..0, 0..0, 0..0], @palette.rgb_range
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_initialize_with_strong_template
|
13
|
+
assert_equal [0..7, 0..9, 0..255], ColorPicker::Palette.new(template: :strong).rgb_range
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_initialize_with_complete_template
|
17
|
+
assert_equal [0..255, 0..255, 0..255], ColorPicker::Palette.new(template: :complete).rgb_range
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_initialize_complete_range
|
21
|
+
assert_equal [0..0, 0..0, 0..0], @palette.rgb_range
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_initialize_complete_palette_template
|
25
|
+
assert_equal nil, @palette.template
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_complete_palette
|
29
|
+
assert_equal [0..255, 0 .. 255, 0 .. 255], @palette.generate(template: :complete)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_strong_paletter
|
33
|
+
assert_equal [0..7, 0 .. 9, 0..255], @palette.generate(template: :strong)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_to_html
|
37
|
+
@palette.generate(template: :html)
|
38
|
+
assert_match /DOCTYPE HTML/, @palette.html
|
39
|
+
#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
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_generate_custom_palette
|
43
|
+
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)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_sample_color_return_color_instance
|
47
|
+
assert_kind_of Color, @palette.sample_color
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_sample_color
|
51
|
+
assert_match /([0-9]|[a-fA-F]){5,6}/, @palette.sample_color.to_s
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_strong_colors_palette_range
|
55
|
+
@palette.generate(template: :strong)
|
56
|
+
max_range = (@palette.red_color_range.end+1) * (@palette.green_color_range.end+1) * (@palette.blue_color_range.end+1)
|
57
|
+
colors = []
|
58
|
+
10.times { colors << @palette.sample_color.to_s }
|
59
|
+
colors.each do |color|
|
60
|
+
assert_match (/[0-7]{2}[0-9]{2}[0-9|a-fA-F]{2}/), color
|
61
|
+
end
|
62
|
+
assert_equal @palette.max_number_of_colors, max_range
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_method_missing
|
66
|
+
assert_match "Colors and stars something wrong. Palette types available :complete and :strong via template option", @palette.undefined
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
58
70
|
end
|