color_code 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f4a47ae103030ebc9914dd4b8c9d9726cd8b42c8
4
+ data.tar.gz: f26c1f7ee3642aca7c17322a794894ef22463d1e
5
+ SHA512:
6
+ metadata.gz: 58eebdeb86de24fab4fb9658171d1f29b82c6585d564cab3c7db1cfa5c30e6ea2f8e18638e43bf142ae4c5cb465dffffd767e70356428c0e406d9c123d79e09d
7
+ data.tar.gz: 7d51438470a3df775fffe7515ba6f1a86aebd17fa256e804b2649bb41b03ed46b5a660d818e0372c3fb930b5a1069dd836eed04319bb81ad213ba63da55d6c68
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in color_code.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # ColorCode
2
+
3
+ simple handle the color code
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'color_code'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install color_code
20
+
21
+ ## Usage
22
+
23
+ RGB class
24
+
25
+ ```ruby
26
+ rgb = ColorCode::RGB.new(r: 255, g: 0, b: 0)
27
+ rgb.to_s # => '#ff0000'
28
+ rbg.to_hash # => { r: 255, g: 0, b: 0 }
29
+ ```
30
+
31
+ HSL class
32
+
33
+ ```ruby
34
+ hsl = ColorCode::HSL.new(h: 0, s: 100, l: 50)
35
+ hsl.to_s # => '#ff0000'
36
+ hsl.to_hash # => { h: 0, s: 100, l: 50 }
37
+ ```
38
+
39
+ convert RGB to HSL
40
+
41
+ ``ruby
42
+ rgb = ColorCode::RGB.new(r: 255, g: 0, b: 0)
43
+ rgb.to_s # => '#ff0000'
44
+ hsl = rgb.to_hsl
45
+ hsl.to_s # => '#ff0000'
46
+ ```
47
+
48
+ convert HSL to RGB
49
+
50
+ ```ruby
51
+ hsl = ColorCode::HSL.new(h: 0, s: 100, l: 50)
52
+ hsl.to_s # => '#ff0000'
53
+ rgb = hsl.to_rgb
54
+ rgb.to_s # => '#ff0000'
55
+ ```
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it ( https://github.com/[my-github-username]/color_code/fork )
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "color_code"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'color_code/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "color_code"
8
+ spec.version = ColorCode::VERSION
9
+ spec.authors = ["shiro16"]
10
+ spec.email = ["nyanyanyawan24@gmail.com"]
11
+
12
+ spec.summary = %q{classes to handle the color code}
13
+ spec.description = %q{classes to handle the color code}
14
+ spec.homepage = ""
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.8"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,76 @@
1
+ module ColorCode
2
+ class HSL
3
+ attr_accessor :h, :s, :l
4
+
5
+ def initialize(code=nil, h: 0, s: 0, l: 0)
6
+ if code
7
+ hsl = ColorCode::RGB.new(code).to_hsl
8
+ h, s, l = hsl.to_a
9
+ end
10
+
11
+ @h = h
12
+ @s = s
13
+ @l = l
14
+ raise if @h > 360 || @s > 100 || @l > 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, @l]
26
+ end
27
+
28
+ def to_hash
29
+ { h: @h, s: @s, l: @l }
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
+ 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)
42
+ 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)
45
+ end
46
+
47
+ case @h
48
+ when 0...60
49
+ r = max
50
+ g = (@h / 60) * (max - min) + min
51
+ b = min
52
+ when 60...120
53
+ r = ((120 - @h) / 60) * (max - min) + min
54
+ g = max
55
+ b = min
56
+ when 120...180
57
+ r = min
58
+ g = max
59
+ b = ((@h - 120) / 60) * (max - min) + min
60
+ when 180...240
61
+ r = min
62
+ g = ((240 - @h) / 60) * (max - min) + min
63
+ b = max
64
+ when 240...300
65
+ r = ((@h - 240) / 60) * (max - min) + min
66
+ g = min
67
+ b = max
68
+ when 300..360
69
+ r = max
70
+ g = min
71
+ b = ((360 - @h) / 60) * (max - min) + min
72
+ end
73
+ [r.round, g.round, b.round]
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,75 @@
1
+ module ColorCode
2
+ class RGB
3
+ attr_accessor :r, :g, :b
4
+
5
+ def initialize(code=nil, r: 0, g: 0, b: 0)
6
+ r, g, b = parse_code(code) if code
7
+ @r = r
8
+ @g = g
9
+ @b = b
10
+ raise ArgumentError.new('invalid value') if @r > 255 || @g > 255 || @b > 255
11
+ end
12
+
13
+ def to_s
14
+ rgb = to_a.map { |hue| '%02x' % hue }.join
15
+ "##{rgb}"
16
+ end
17
+
18
+ def to_a
19
+ [@r, @g, @b]
20
+ end
21
+
22
+ def to_hash
23
+ { r: @r, g: @g, b: @b }
24
+ end
25
+
26
+ def to_hsl
27
+ ColorCode::HSL.new(h: h, s:s, l:l)
28
+ end
29
+
30
+ private
31
+ def parse_code(code)
32
+ raise ArgumentError.new('invalid value') unless md = code.match(/^#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})$/)
33
+ md[1..3].map(&:hex)
34
+ end
35
+
36
+ def h
37
+ return 0 if @r == @g && @g == @b
38
+ hue = case max
39
+ when @r
40
+ 60 * ((@g - @b) / (max - min))
41
+ when @g
42
+ 60 * ((@b - @r) / (max - min)) + 120
43
+ when @b
44
+ 60 * ((@r - @g) / (max - min)) + 240
45
+ end
46
+ hue + 360 if hue < 0
47
+ hue.round
48
+ end
49
+
50
+ def s
51
+ converge = (max + min) / 2
52
+
53
+ saturation = if converge < 128
54
+ (max - min).quo(max + min).to_f
55
+ else
56
+ (max - min).quo(510 - max + min).to_f
57
+ end
58
+ (saturation * 100).round
59
+ end
60
+
61
+ def l
62
+ luminance = (max + min).quo(2).to_f
63
+ luminance = luminance.quo(255).to_f * 100
64
+ luminance.round
65
+ end
66
+
67
+ def max
68
+ to_a.max
69
+ end
70
+
71
+ def min
72
+ to_a.min
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module ColorCode
2
+ VERSION = "0.1.0"
3
+ end
data/lib/color_code.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'color_code/version'
2
+ require 'color_code/rgb'
3
+ require 'color_code/hsl'
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: color_code
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - shiro16
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: classes to handle the color code
56
+ email:
57
+ - nyanyanyawan24@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - .travis.yml
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - color_code.gemspec
71
+ - lib/color_code.rb
72
+ - lib/color_code/hsl.rb
73
+ - lib/color_code/rgb.rb
74
+ - lib/color_code/version.rb
75
+ homepage: ''
76
+ licenses: []
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.0.14
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: classes to handle the color code
98
+ test_files: []