color_picker 0.1.3 → 0.1.4
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 +4 -0
- data/README.md +2 -2
- data/lib/color_picker/color.rb +2 -2
- data/lib/color_picker/palette.rb +43 -2
- data/lib/color_picker/palette/templates.rb +8 -1
- data/lib/color_picker/version.rb +1 -1
- data/test/color_test.rb +8 -3
- data/test/palette_test.rb +9 -4
- 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: 688441a3c2b18df6cb7423c68107ff963d643596
|
4
|
+
data.tar.gz: 95f2dc0db78f718e18ce17751f2c8740fe92de71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bba85bb1fdaa8655c61bb615649b11c9f2196ad9a37fc75f0a5b8b03b622c2e1f3f96ac36239f8d3ed93bb5c0477ae0f517fe2e09bc90b9f807879a7a7f2b35d
|
7
|
+
data.tar.gz: db254e4aa82dcdebc6d215bab337074e93084bd659203f43362b6b788167fd8b5f7cf6b196a2d5fa0d468eef886af873a70415d0171fa6621fb0cd5d14b6f599
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -19,9 +19,9 @@ a tool to get a color based on color palette, also have a method to export to ht
|
|
19
19
|
@palette.sample_color
|
20
20
|
> #<ColorPicker::Color:0x007f4dc236ac10 @hex_code="000000">
|
21
21
|
```
|
22
|
-
* generate a
|
22
|
+
* generate a dark palette
|
23
23
|
```ruby
|
24
|
-
@palette.generate(template: :
|
24
|
+
@palette.generate(template: :dark)
|
25
25
|
> [0..7, 0..9, 0..255]
|
26
26
|
```
|
27
27
|
|
data/lib/color_picker/color.rb
CHANGED
@@ -8,12 +8,12 @@ module ColorPicker
|
|
8
8
|
|
9
9
|
def to_rgb
|
10
10
|
return code if rgb?
|
11
|
-
code.scan(/.{2}/).map { |n| n.to_i(16) }
|
11
|
+
Color.new code.scan(/.{2}/).map { |n| n.to_i(16) }
|
12
12
|
end
|
13
13
|
|
14
14
|
def to_hex
|
15
15
|
return code if hex?
|
16
|
-
code.map { |n| n.to_s(16) }.join.rjust(6, '0')
|
16
|
+
Color.new code.map { |n| n.to_s(16) }.join.rjust(6, '0')
|
17
17
|
end
|
18
18
|
|
19
19
|
def hex?
|
data/lib/color_picker/palette.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module ColorPicker
|
2
2
|
class Palette
|
3
3
|
|
4
|
-
attr_accessor :template, :colors
|
4
|
+
attr_accessor :template, :colors, :html
|
5
5
|
|
6
6
|
def initialize(template: :default, colors: {})
|
7
7
|
@template = template
|
@@ -13,7 +13,48 @@ module ColorPicker
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def sample_color
|
16
|
-
Color.new(ranges.map { |range| range
|
16
|
+
Color.new(ranges.map { |range| get_shade(range) })
|
17
17
|
end
|
18
|
+
|
19
|
+
def max_number_of_colors
|
20
|
+
result = 1
|
21
|
+
ranges.each{|range| result *= range.end}
|
22
|
+
result
|
23
|
+
end
|
24
|
+
|
25
|
+
def html
|
26
|
+
@html ||= to_html
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def get_shade(range)
|
31
|
+
(range.to_a.sample * (SHADES[template] || 1)).round
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_html
|
35
|
+
_times = max_number_of_colors > 100 ? 100 : max_number_of_colors
|
36
|
+
html_content = HTML[:header]
|
37
|
+
html_content += "<head><table cellpadding='0' cellspacing='0' border='0' id='random_colors'><tbody><tr border-top:1px solid #fff; border-bottom:1px solid #fff>"
|
38
|
+
new_tr = nil
|
39
|
+
_times.times do |n|
|
40
|
+
html_content += "<tr border-top:1px solid #fff; border-bottom:1px solid #fff>" if new_tr
|
41
|
+
html_content += "<td style='background:#{sample_color.to_hex};color:#fff;width:55px;border-right:1px solid #fff;border-left:1px solid #fff;text-align:center;line-height:26px'>#{sample_color.to_hex}</td>"
|
42
|
+
if (n+1) % 5 == 0
|
43
|
+
html_content += "</tr>"
|
44
|
+
new_tr = true
|
45
|
+
else
|
46
|
+
new_tr = false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
html_content += "</tbody></table>" + HTML[:footer]
|
50
|
+
generate_file html_content
|
51
|
+
end
|
52
|
+
|
53
|
+
def generate_file(content)
|
54
|
+
file = File.open("palette_#{template}.html", 'w')
|
55
|
+
file.write content
|
56
|
+
file.close
|
57
|
+
file.path
|
58
|
+
end
|
18
59
|
end
|
19
60
|
end
|
@@ -2,7 +2,14 @@ module ColorPicker
|
|
2
2
|
class Palette
|
3
3
|
TEMPLATES = {
|
4
4
|
default: { red: 0..255, green: 0..255, blue: 0..255 },
|
5
|
-
|
5
|
+
dark: { red: 0..76, green: 0..94, blue: 0..255}
|
6
|
+
}
|
7
|
+
SHADES = {
|
8
|
+
dark: 0.6
|
9
|
+
}
|
10
|
+
HTML = {
|
11
|
+
header: '<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><title>Palete</title><body>',
|
12
|
+
footer: '</body></html>'
|
6
13
|
}
|
7
14
|
end
|
8
15
|
end
|
data/lib/color_picker/version.rb
CHANGED
data/test/color_test.rb
CHANGED
@@ -13,13 +13,18 @@ module ColorPicker
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_to_rgb
|
16
|
-
|
16
|
+
assert_kind_of Color, @hex_color.to_rgb
|
17
17
|
assert_equal [0,0,0], @rgb_color.to_rgb
|
18
18
|
end
|
19
19
|
|
20
20
|
def test_to_hex
|
21
|
-
assert_equal
|
22
|
-
|
21
|
+
assert_equal "000000", @hex_color.to_hex
|
22
|
+
assert_kind_of Color, @rgb_color.to_hex
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_method_chain
|
26
|
+
assert_equal "000000", @rgb_color.to_hex.code
|
27
|
+
assert_equal [0,0,0], @hex_color.to_rgb.code
|
23
28
|
end
|
24
29
|
|
25
30
|
def test_to_s
|
data/test/palette_test.rb
CHANGED
@@ -4,7 +4,7 @@ module ColorPicker
|
|
4
4
|
class PaletteTest < Minitest::Test
|
5
5
|
def setup
|
6
6
|
@palette = ColorPicker::Palette.new
|
7
|
-
@
|
7
|
+
@dark_palette = ColorPicker::Palette.new(template: :dark)
|
8
8
|
end
|
9
9
|
|
10
10
|
def test_default_palette
|
@@ -12,9 +12,9 @@ module ColorPicker
|
|
12
12
|
assert_equal ({ red: 0..255, green: 0..255, blue: 0..255 }), @palette.colors
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
16
|
-
assert_equal :
|
17
|
-
assert_equal ({ red: 0..
|
15
|
+
def test_dark_palette
|
16
|
+
assert_equal :dark, @dark_palette.template
|
17
|
+
assert_equal ({ red: 0..76, green: 0..94, blue: 0..255 }), @dark_palette.colors
|
18
18
|
end
|
19
19
|
|
20
20
|
def test_custom_palette
|
@@ -26,5 +26,10 @@ module ColorPicker
|
|
26
26
|
def test_sample_color
|
27
27
|
assert_kind_of Color, @palette.sample_color
|
28
28
|
end
|
29
|
+
|
30
|
+
def test_html
|
31
|
+
@custom_palette = ColorPicker::Palette.new(colors: { red: 0..1, green: 1..2, blue: 5..14 })
|
32
|
+
assert_equal "palette_default.html", @palette.html
|
33
|
+
end
|
29
34
|
end
|
30
35
|
end
|