color_code 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f527e031efdd1ce7f01204b4a50d88c17262b4a
4
- data.tar.gz: 8f9a266229c2e531967018d668cf920c5857165d
3
+ metadata.gz: 63030412515c10648352e99699cad7a3f198967d
4
+ data.tar.gz: f16b9030f80f7cbea5c1ee06a8cb97977a40f182
5
5
  SHA512:
6
- metadata.gz: c29053d03da90cb6da8ab339d06a4cb492dff55b24ca959b27bb0be379fd766187d717985a8f6f2bd5b3e48dfd2bcba00d7a09b6575c8c86734ab353b505b6df
7
- data.tar.gz: d3fc53dc448cbb57a40bc9d421258a999b41313711490b8ed23036238a6ae550f2f8ac0de1fb60ab9aed66218622a6e8e61d2cd0951548bf6e2874e89e96a2fe
6
+ metadata.gz: 146ac28ae23934309aa38d3af96fa4e1304509ae0c0d14f3e8e029eeeeb937d42c37e09d007d6e1cde1c490dbea6165aae1c585f6597a914766be2c52e12a86b
7
+ data.tar.gz: 60b0573a3819dccbc785d4714a4fb07a35324ae924e83c7a5de38bd89a28bf52f5f40e63f637ccc67d9f0499b86a3af864cbcfedc0ff05247d718d30b3303098
data/README.md CHANGED
@@ -20,7 +20,7 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- RGB class
23
+ ### RGB class
24
24
 
25
25
  ```ruby
26
26
  rgb = ColorCode::RGB.new(r: 255, g: 0, b: 0)
@@ -28,7 +28,15 @@ rgb.to_s # => '#ff0000'
28
28
  rbg.to_hash # => { r: 255, g: 0, b: 0 }
29
29
  ```
30
30
 
31
- HSL class
31
+ ### HSV class
32
+
33
+ ```ruby
34
+ hsv = ColorCode::HSV.new(h: 0, s: 100, v: 100)
35
+ hsv.to_s # => '#ff0000'
36
+ hsv.to_hash # => { h: 0, s: 100, v: 100 }
37
+ ```
38
+
39
+ ### HSL class
32
40
 
33
41
  ```ruby
34
42
  hsl = ColorCode::HSL.new(h: 0, s: 100, l: 50)
@@ -36,24 +44,43 @@ hsl.to_s # => '#ff0000'
36
44
  hsl.to_hash # => { h: 0, s: 100, l: 50 }
37
45
  ```
38
46
 
39
- convert RGB to HSL
47
+ ### convert RGB to HSV
48
+
49
+ ```ruby
50
+ rgb = ColorCode::RGB.new(r: 255, g: 0, b: 0)
51
+ rgb.to_s # => '#ff0000'
52
+ hsv = rgb.to_hsv
53
+ hsv.to_s # => '#ff0000'
54
+ ```
55
+
56
+ ### convert HSL to RGB
40
57
 
41
58
  ```ruby
42
- rgb = ColorCode::RGB.new(r: 255, g: 0, b: 0)
59
+ hsv = ColorCode::HSV.new(h: 0, s: 100, v: 100)
60
+ hsv.to_s # => '#ff0000'
61
+ rgb = hsv.to_rgb
62
+ rgb.to_s # => '#ff0000'
63
+ ```
64
+
65
+ ### convert RGB to HSL
66
+
67
+ ```ruby
68
+ rgb = ColorCode::RGB.new(r: 255, g: 0, b: 0)
43
69
  rgb.to_s # => '#ff0000'
44
70
  hsl = rgb.to_hsl
45
- hsl.to_s # => '#ff0000'
71
+ hsl.to_s # => '#ff0000'
46
72
  ```
47
73
 
48
- convert HSL to RGB
74
+ ### convert HSL to RGB
49
75
 
50
76
  ```ruby
51
- hsl = ColorCode::HSL.new(h: 0, s: 100, l: 50)
77
+ hsl = ColorCode::HSL.new(h: 0, s: 100, l: 50)
52
78
  hsl.to_s # => '#ff0000'
53
79
  rgb = hsl.to_rgb
54
80
  rgb.to_s # => '#ff0000'
55
81
  ```
56
82
 
83
+
57
84
  ## Contributing
58
85
 
59
86
  1. Fork it ( https://github.com/shiro16/color_code/fork )
@@ -1,5 +1,7 @@
1
1
  module ColorCode
2
2
  class HSL
3
+ include HueSaturation
4
+
3
5
  attr_accessor :h, :s, :l
4
6
 
5
7
  def initialize(code=nil, h: 0, s: 0, l: 0)
@@ -16,11 +18,6 @@ module ColorCode
16
18
  raise ArgumentError.new('invalid value')
17
19
  end
18
20
 
19
- def to_s
20
- rgb = convert_rgb.map { |hue| '%02x' % hue }.join
21
- "##{rgb}"
22
- end
23
-
24
21
  def to_a
25
22
  [@h, @s, @l]
26
23
  end
@@ -29,48 +26,21 @@ module ColorCode
29
26
  { h: @h, s: @s, l: @l }
30
27
  end
31
28
 
32
- def to_rgb
33
- r, g, b = convert_rgb
34
- ColorCode::RGB.new(r: r, g: g, b: b)
35
- end
36
-
37
29
  private
38
- def convert_rgb
30
+ def convert_max
39
31
  if @l < 50
40
- max = 2.55 * (@l + @l * @s.quo(100).to_f)
41
- min = 2.55 * (@l - @l * @s.quo(100).to_f)
32
+ 2.55 * (@l + @l * @s.quo(100).to_f)
42
33
  else
43
- max = 2.55 * (@l + (100 - @l) * @s.quo(100).to_f)
44
- min = 2.55 * (@l - (100 - @l) * @s.quo(100).to_f)
34
+ 2.55 * (@l + (100 - @l) * @s.quo(100).to_f)
45
35
  end
36
+ end
46
37
 
47
- case @h
48
- when 0...60
49
- r = max
50
- g = (@h.quo(60).to_f) * (max - min) + min
51
- b = min
52
- when 60...120
53
- r = ((120 - @h).quo(60).to_f) * (max - min) + min
54
- g = max
55
- b = min
56
- when 120...180
57
- r = min
58
- g = max
59
- b = ((@h - 120).quo(60).to_f) * (max - min) + min
60
- when 180...240
61
- r = min
62
- g = ((240 - @h).quo(60).to_f) * (max - min) + min
63
- b = max
64
- when 240...300
65
- r = ((@h - 240).quo(60).to_f) * (max - min) + min
66
- g = min
67
- b = max
68
- when 300..360
69
- r = max
70
- g = min
71
- b = ((360 - @h).quo(60).to_f) * (max - min) + min
38
+ def convert_min
39
+ if @l < 50
40
+ 2.55 * (@l - @l * @s.quo(100).to_f)
41
+ else
42
+ 2.55 * (@l - (100 - @l) * @s.quo(100).to_f)
72
43
  end
73
- [r.round, g.round, b.round]
74
44
  end
75
45
  end
76
46
  end
@@ -1,5 +1,7 @@
1
1
  module ColorCode
2
2
  class HSV
3
+ include HueSaturation
4
+
3
5
  attr_accessor :h, :s, :v
4
6
 
5
7
  def initialize(code=nil, h: 0, s: 0, v: 0)
@@ -16,11 +18,6 @@ module ColorCode
16
18
  raise ArgumentError.new('invalid value')
17
19
  end
18
20
 
19
- def to_s
20
- rgb = convert_rgb.map { |hue| '%02x' % hue }.join
21
- "##{rgb}"
22
- end
23
-
24
21
  def to_a
25
22
  [@h, @s, @v]
26
23
  end
@@ -29,43 +26,22 @@ module ColorCode
29
26
  { h: @h, s: @s, v: @v }
30
27
  end
31
28
 
32
- def to_rgb
33
- r, g, b = convert_rgb
34
- ColorCode::RGB.new(r: r, g: g, b: b)
29
+ def distance(hsv)
30
+ diff = if @h > hsv.h
31
+ [@h - hsv.h, hsv.h - @h + 360].min
32
+ else
33
+ [hsv.h - @h, @h - hsv.h + 360].min
34
+ end
35
+ Math.sqrt(diff**2 + (@s - hsv.s)**2 + (@v - hsv.v)**2)
35
36
  end
36
37
 
37
38
  private
38
- def convert_rgb
39
- max = @v * 255 / 100.to_f
40
- min = (@s / 100.to_f * max - max) * -1
39
+ def convert_max
40
+ @v * 255 / 100.to_f
41
+ end
41
42
 
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]
43
+ def convert_min
44
+ (@s / 100.to_f * convert_max - convert_max) * -1
69
45
  end
70
46
  end
71
47
  end
@@ -0,0 +1,48 @@
1
+ module ColorCode
2
+ module HueSaturation
3
+ def to_s
4
+ rgb = convert_rgb.map { |hue| '%02x' % hue }.join
5
+ "##{rgb}"
6
+ end
7
+
8
+ def to_rgb
9
+ r, g, b = convert_rgb
10
+ ColorCode::RGB.new(r: r, g: g, b: b)
11
+ end
12
+
13
+ private
14
+ def convert_rgb
15
+ max = convert_max
16
+ min = convert_min
17
+
18
+ case @h
19
+ when 0...60
20
+ r = max
21
+ g = (@h.quo(60).to_f) * (max - min) + min
22
+ b = min
23
+ when 60...120
24
+ r = ((120 - @h).quo(60).to_f) * (max - min) + min
25
+ g = max
26
+ b = min
27
+ when 120...180
28
+ r = min
29
+ g = max
30
+ b = ((@h - 120).quo(60).to_f) * (max - min) + min
31
+ when 180...240
32
+ r = min
33
+ g = ((240 - @h).quo(60).to_f) * (max - min) + min
34
+ b = max
35
+ when 240...300
36
+ r = ((@h - 240).quo(60).to_f) * (max - min) + min
37
+ g = min
38
+ b = max
39
+ when 300..360
40
+ r = max
41
+ g = min
42
+ b = ((360 - @h).quo(60).to_f) * (max - min) + min
43
+ end
44
+
45
+ [r.round, g.round, b.round]
46
+ end
47
+ end
48
+ end
@@ -31,6 +31,10 @@ module ColorCode
31
31
  ColorCode::HSV.new(h: h, s:hsv_s, v:v)
32
32
  end
33
33
 
34
+ def distance(rgb)
35
+ Math.sqrt((@r - rgb.r)**2 + (@g - rgb.g)**2 + (@b - rgb.b)**2)
36
+ end
37
+
34
38
  private
35
39
  def parse_code(code)
36
40
  raise ArgumentError.new('invalid value') unless md = code.match(/^#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})$/)
@@ -1,3 +1,3 @@
1
1
  module ColorCode
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/color_code.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'color_code/version'
2
+ require 'color_code/hue_saturation'
2
3
  require 'color_code/rgb'
3
4
  require 'color_code/hsl'
4
5
  require 'color_code/hsv'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: color_code
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - shiro16
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-26 00:00:00.000000000 Z
11
+ date: 2016-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,6 +71,7 @@ files:
71
71
  - lib/color_code.rb
72
72
  - lib/color_code/hsl.rb
73
73
  - lib/color_code/hsv.rb
74
+ - lib/color_code/hue_saturation.rb
74
75
  - lib/color_code/rgb.rb
75
76
  - lib/color_code/version.rb
76
77
  homepage: ''
@@ -92,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
93
  version: '0'
93
94
  requirements: []
94
95
  rubyforge_project:
95
- rubygems_version: 2.4.5
96
+ rubygems_version: 2.4.5.1
96
97
  signing_key:
97
98
  specification_version: 4
98
99
  summary: classes to handle the color code