color_picker 0.1.2 → 0.1.3
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 +2 -0
- data/Gemfile +4 -3
- data/lib/color_picker.rb +1 -0
- data/lib/color_picker/color.rb +28 -13
- data/lib/color_picker/palette.rb +9 -80
- data/lib/color_picker/palette/templates.rb +8 -0
- data/lib/color_picker/version.rb +1 -1
- data/test/color_test.rb +21 -14
- data/test/palette_test.rb +14 -54
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 429a2e8286798677c460a201fdd4450fbf2fb4b8
|
4
|
+
data.tar.gz: f136bbb6b36d74e3487f7cab7807e4eb33485466
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 067cc616b32cd0406e11360f568b0a31edbfc53be6df20fc81267d1919dba6b2d855734af0555b04d10c369c3caf0fcea58f942d135d1422522a8ab053e8f26f
|
7
|
+
data.tar.gz: 2c9f23cd121f6522c076a9ecccfe24e2ba0611b0bf51a4c7789734aeddc27aac59e3707789813d44cc4af1be5291d0d280a52ae2b171c10a46a0521758340fb5
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/lib/color_picker.rb
CHANGED
data/lib/color_picker/color.rb
CHANGED
@@ -1,29 +1,44 @@
|
|
1
1
|
module ColorPicker
|
2
2
|
class Color
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
attr_accessor :code
|
4
|
+
|
6
5
|
def initialize(code)
|
7
|
-
@
|
8
|
-
@rgb_code = code.to_ary unless @hex_code
|
6
|
+
@code = code
|
9
7
|
end
|
10
8
|
|
11
9
|
def to_rgb
|
12
|
-
return
|
13
|
-
|
10
|
+
return code if rgb?
|
11
|
+
code.scan(/.{2}/).map { |n| n.to_i(16) }
|
14
12
|
end
|
15
13
|
|
16
14
|
def to_hex
|
17
|
-
return
|
18
|
-
|
15
|
+
return code if hex?
|
16
|
+
code.map { |n| n.to_s(16) }.join.rjust(6, '0')
|
17
|
+
end
|
18
|
+
|
19
|
+
def hex?
|
20
|
+
code.respond_to?(:to_str)
|
19
21
|
end
|
20
22
|
|
21
|
-
def
|
22
|
-
|
23
|
-
"##{hex_code}"
|
23
|
+
def rgb?
|
24
|
+
code.respond_to?(:to_ary)
|
24
25
|
end
|
25
26
|
|
26
|
-
|
27
|
+
def to_s
|
28
|
+
send("to_s_#{type}")
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def type
|
33
|
+
rgb? ? :rgb : :hex
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_s_hex
|
37
|
+
"##{self.to_hex}"
|
38
|
+
end
|
27
39
|
|
40
|
+
def to_s_rgb
|
41
|
+
"rgb(#{self.to_rgb.join(', ')})"
|
42
|
+
end
|
28
43
|
end
|
29
44
|
end
|
data/lib/color_picker/palette.rb
CHANGED
@@ -1,90 +1,19 @@
|
|
1
1
|
module ColorPicker
|
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
|
2
|
+
class Palette
|
13
3
|
|
14
|
-
|
15
|
-
opts(options).each_pair do |key, value|
|
16
|
-
eval "@#{key}= value"
|
17
|
-
end
|
18
|
-
end
|
4
|
+
attr_accessor :template, :colors
|
19
5
|
|
20
|
-
def
|
21
|
-
@
|
22
|
-
|
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
|
31
|
-
end
|
32
|
-
html += "</div>"
|
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)
|
6
|
+
def initialize(template: :default, colors: {})
|
7
|
+
@template = template
|
8
|
+
@colors = TEMPLATES[template].merge(colors)
|
45
9
|
end
|
46
10
|
|
47
|
-
def
|
48
|
-
|
49
|
-
range_color.to_a.sample.to_s(16).rjust(2, "0")
|
50
|
-
end
|
51
|
-
Color.new(ary_code.join)
|
11
|
+
def ranges
|
12
|
+
colors.values
|
52
13
|
end
|
53
14
|
|
54
|
-
def
|
55
|
-
|
15
|
+
def sample_color
|
16
|
+
Color.new(ranges.map { |range| range.to_a.sample })
|
56
17
|
end
|
57
|
-
|
58
|
-
private :red_color_range=, :blue_color_range=, :green_color_range=, :template=
|
59
|
-
|
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
|
71
|
-
|
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
|
-
|
89
18
|
end
|
90
19
|
end
|
data/lib/color_picker/version.rb
CHANGED
data/test/color_test.rb
CHANGED
@@ -1,33 +1,40 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
|
+
|
2
3
|
module ColorPicker
|
3
4
|
class ColorTest < Minitest::Test
|
4
5
|
def setup
|
5
|
-
@
|
6
|
+
@hex_color = ColorPicker::Color.new("000000")
|
7
|
+
@rgb_color = ColorPicker::Color.new([0,0,0])
|
6
8
|
end
|
7
9
|
|
8
10
|
def test_initialize_color
|
9
|
-
assert_equal
|
11
|
+
assert_equal '000000', @hex_color.code
|
12
|
+
assert_equal [0,0,0], @rgb_color.code
|
10
13
|
end
|
11
14
|
|
12
|
-
def
|
13
|
-
assert_equal [0,0,0],
|
15
|
+
def test_to_rgb
|
16
|
+
assert_equal [0,0,0], @hex_color.to_rgb
|
17
|
+
assert_equal [0,0,0], @rgb_color.to_rgb
|
14
18
|
end
|
15
19
|
|
16
|
-
def
|
17
|
-
assert_equal
|
20
|
+
def test_to_hex
|
21
|
+
assert_equal '000000', @hex_color.to_hex
|
22
|
+
assert_equal '000000', @rgb_color.to_hex
|
18
23
|
end
|
19
24
|
|
20
|
-
def
|
21
|
-
assert_equal "rgb(0, 0, 0)",
|
25
|
+
def test_to_s
|
26
|
+
assert_equal "rgb(0, 0, 0)", @rgb_color.to_s
|
27
|
+
assert_equal "#000000", @hex_color.to_s
|
22
28
|
end
|
23
29
|
|
24
|
-
def
|
25
|
-
assert_equal
|
30
|
+
def test_rgb?
|
31
|
+
assert_equal false, @hex_color.rgb?
|
32
|
+
assert_equal true, @rgb_color.rgb?
|
26
33
|
end
|
27
34
|
|
28
|
-
def
|
29
|
-
assert_equal
|
35
|
+
def test_hex?
|
36
|
+
assert_equal true, @hex_color.hex?
|
37
|
+
assert_equal false, @hex_color.rgb?
|
30
38
|
end
|
31
|
-
|
32
39
|
end
|
33
40
|
end
|
data/test/palette_test.rb
CHANGED
@@ -1,70 +1,30 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
|
+
|
2
3
|
module ColorPicker
|
3
4
|
class PaletteTest < Minitest::Test
|
4
5
|
def setup
|
5
6
|
@palette = ColorPicker::Palette.new
|
7
|
+
@strong_palette = ColorPicker::Palette.new(template: :strong)
|
6
8
|
end
|
7
9
|
|
8
|
-
def
|
9
|
-
assert_equal
|
10
|
-
|
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)
|
10
|
+
def test_default_palette
|
11
|
+
assert_equal :default, @palette.template
|
12
|
+
assert_equal ({ red: 0..255, green: 0..255, blue: 0..255 }), @palette.colors
|
30
13
|
end
|
31
14
|
|
32
|
-
def
|
33
|
-
assert_equal
|
15
|
+
def test_strong_palette
|
16
|
+
assert_equal :strong, @strong_palette.template
|
17
|
+
assert_equal ({ red: 0..7, green: 0..9, blue: 0..255 }), @strong_palette.colors
|
34
18
|
end
|
35
19
|
|
36
|
-
def
|
37
|
-
@
|
38
|
-
|
39
|
-
|
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
|
20
|
+
def test_custom_palette
|
21
|
+
@custom_palette = ColorPicker::Palette.new(colors: { red: 5..10, green: 15..20, blue: 25..30 })
|
22
|
+
assert_equal :default, @custom_palette.template
|
23
|
+
assert_equal ({ red: 5..10, green: 15..20, blue: 25..30 }), @custom_palette.colors
|
48
24
|
end
|
49
25
|
|
50
26
|
def test_sample_color
|
51
|
-
|
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
|
27
|
+
assert_kind_of Color, @palette.sample_color
|
67
28
|
end
|
68
|
-
|
69
29
|
end
|
70
30
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: color_picker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bernardo Galindo
|
@@ -56,6 +56,7 @@ files:
|
|
56
56
|
- lib/color_picker.rb
|
57
57
|
- lib/color_picker/color.rb
|
58
58
|
- lib/color_picker/palette.rb
|
59
|
+
- lib/color_picker/palette/templates.rb
|
59
60
|
- lib/color_picker/version.rb
|
60
61
|
- test/color_test.rb
|
61
62
|
- test/palette_test.rb
|