color_code 0.1.2 → 0.2.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/bin/console +0 -0
- data/bin/setup +0 -0
- data/lib/color_code/hsv.rb +71 -0
- data/lib/color_code/rgb.rb +19 -3
- data/lib/color_code/version.rb +1 -1
- data/lib/color_code.rb +1 -0
- metadata +15 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f527e031efdd1ce7f01204b4a50d88c17262b4a
|
4
|
+
data.tar.gz: 8f9a266229c2e531967018d668cf920c5857165d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c29053d03da90cb6da8ab339d06a4cb492dff55b24ca959b27bb0be379fd766187d717985a8f6f2bd5b3e48dfd2bcba00d7a09b6575c8c86734ab353b505b6df
|
7
|
+
data.tar.gz: d3fc53dc448cbb57a40bc9d421258a999b41313711490b8ed23036238a6ae550f2f8ac0de1fb60ab9aed66218622a6e8e61d2cd0951548bf6e2874e89e96a2fe
|
data/bin/console
CHANGED
File without changes
|
data/bin/setup
CHANGED
File without changes
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module ColorCode
|
2
|
+
class HSV
|
3
|
+
attr_accessor :h, :s, :v
|
4
|
+
|
5
|
+
def initialize(code=nil, h: 0, s: 0, v: 0)
|
6
|
+
if code
|
7
|
+
hsv = ColorCode::RGB.new(code).to_hsv
|
8
|
+
h, s, v = hsv.to_a
|
9
|
+
end
|
10
|
+
|
11
|
+
@h = h
|
12
|
+
@s = s
|
13
|
+
@v = v
|
14
|
+
raise if @h > 360 || @s > 100 || @v > 100
|
15
|
+
rescue
|
16
|
+
raise ArgumentError.new('invalid value')
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_s
|
20
|
+
rgb = convert_rgb.map { |hue| '%02x' % hue }.join
|
21
|
+
"##{rgb}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_a
|
25
|
+
[@h, @s, @v]
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_hash
|
29
|
+
{ h: @h, s: @s, v: @v }
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_rgb
|
33
|
+
r, g, b = convert_rgb
|
34
|
+
ColorCode::RGB.new(r: r, g: g, b: b)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def convert_rgb
|
39
|
+
max = @v * 255 / 100.to_f
|
40
|
+
min = (@s / 100.to_f * max - max) * -1
|
41
|
+
|
42
|
+
case @h
|
43
|
+
when 0...60
|
44
|
+
r = max
|
45
|
+
g = (@h.quo(60).to_f) * (max - min) + min
|
46
|
+
b = min
|
47
|
+
when 60...120
|
48
|
+
r = ((120 - @h).quo(60).to_f) * (max - min) + min
|
49
|
+
g = max
|
50
|
+
b = min
|
51
|
+
when 120...180
|
52
|
+
r = min
|
53
|
+
g = max
|
54
|
+
b = ((@h - 120).quo(60).to_f) * (max - min) + min
|
55
|
+
when 180...240
|
56
|
+
r = min
|
57
|
+
g = ((240 - @h).quo(60).to_f) * (max - min) + min
|
58
|
+
b = max
|
59
|
+
when 240...300
|
60
|
+
r = ((@h - 240).quo(60).to_f) * (max - min) + min
|
61
|
+
g = min
|
62
|
+
b = max
|
63
|
+
when 300..360
|
64
|
+
r = max
|
65
|
+
g = min
|
66
|
+
b = ((360 - @h).quo(60).to_f) * (max - min) + min
|
67
|
+
end
|
68
|
+
[r.round, g.round, b.round]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/color_code/rgb.rb
CHANGED
@@ -24,7 +24,11 @@ module ColorCode
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def to_hsl
|
27
|
-
ColorCode::HSL.new(h: h, s:
|
27
|
+
ColorCode::HSL.new(h: h, s:hsl_s, l:l)
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_hsv
|
31
|
+
ColorCode::HSV.new(h: h, s:hsv_s, v:v)
|
28
32
|
end
|
29
33
|
|
30
34
|
private
|
@@ -34,7 +38,7 @@ module ColorCode
|
|
34
38
|
end
|
35
39
|
|
36
40
|
def h
|
37
|
-
return 0 if
|
41
|
+
return 0 if max == min
|
38
42
|
hue = case max
|
39
43
|
when @r
|
40
44
|
60 * ((@g - @b).quo(max - min).to_f)
|
@@ -47,7 +51,7 @@ module ColorCode
|
|
47
51
|
hue.round
|
48
52
|
end
|
49
53
|
|
50
|
-
def
|
54
|
+
def hsl_s
|
51
55
|
return 0 if max == min
|
52
56
|
converge = (max + min) / 2
|
53
57
|
|
@@ -59,12 +63,24 @@ module ColorCode
|
|
59
63
|
(saturation * 100).round
|
60
64
|
end
|
61
65
|
|
66
|
+
def hsv_s
|
67
|
+
if max.zero?
|
68
|
+
0
|
69
|
+
else
|
70
|
+
(max - min) / max.to_f * 100
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
62
74
|
def l
|
63
75
|
luminance = (max + min).quo(2).to_f
|
64
76
|
luminance = luminance.quo(255).to_f * 100
|
65
77
|
luminance.round
|
66
78
|
end
|
67
79
|
|
80
|
+
def v
|
81
|
+
(max.quo(255).to_f * 100).round
|
82
|
+
end
|
83
|
+
|
68
84
|
def max
|
69
85
|
to_a.max
|
70
86
|
end
|
data/lib/color_code/version.rb
CHANGED
data/lib/color_code.rb
CHANGED
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: color_code
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- shiro16
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.8'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.8'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description: classes to handle the color code
|
@@ -59,9 +59,9 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
- .gitignore
|
63
|
-
- .rspec
|
64
|
-
- .travis.yml
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
65
|
- Gemfile
|
66
66
|
- README.md
|
67
67
|
- Rakefile
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- color_code.gemspec
|
71
71
|
- lib/color_code.rb
|
72
72
|
- lib/color_code/hsl.rb
|
73
|
+
- lib/color_code/hsv.rb
|
73
74
|
- lib/color_code/rgb.rb
|
74
75
|
- lib/color_code/version.rb
|
75
76
|
homepage: ''
|
@@ -81,17 +82,17 @@ require_paths:
|
|
81
82
|
- lib
|
82
83
|
required_ruby_version: !ruby/object:Gem::Requirement
|
83
84
|
requirements:
|
84
|
-
- -
|
85
|
+
- - ">="
|
85
86
|
- !ruby/object:Gem::Version
|
86
87
|
version: '0'
|
87
88
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
89
|
requirements:
|
89
|
-
- -
|
90
|
+
- - ">="
|
90
91
|
- !ruby/object:Gem::Version
|
91
92
|
version: '0'
|
92
93
|
requirements: []
|
93
94
|
rubyforge_project:
|
94
|
-
rubygems_version: 2.
|
95
|
+
rubygems_version: 2.4.5
|
95
96
|
signing_key:
|
96
97
|
specification_version: 4
|
97
98
|
summary: classes to handle the color code
|