color_math 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 +15 -0
- data/LICENSE +21 -0
- data/README.md +2 -0
- data/README.rdoc +24 -0
- data/lib/color_math.rb +140 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZDAxMjg2YjdmNWNiYjIwYjE5ZGJlNWZlOGMxODQ0MTJmN2QwMzRhYQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MTVhNjlmYWEyNzBhMDNkZTI0MjVjMGVlZTk1Mzc2ZmZiMWNkOWJlNQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YzUxNzU4OWVhYTc2ZDVlYTFiZDViMDE1ZmEwZjBhMmVmNTBmOGE3MTk3MTQw
|
10
|
+
OGI3YjE0NzVjMWRkNWFkYWM1MWI5NGIxMmYxODA3NDI2Y2ZlM2ZhZTU1MDY0
|
11
|
+
ZTMyZmNlMjY0YWQyYjk3MTVlMzBkMTYxMmE1NzM2NzYxMWRkZGM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MTk3MzM0OGU3YjMxODUzYWU1ZjM4YTAwOTMzM2E5NDc1MzE5YWQxZDcyZjk1
|
14
|
+
ZjE4NTA1MGM2Yjc0MWYzNTlmMjU1ZmUyYTFmOWVlZTRiYTRjMGNhZGNmY2Q3
|
15
|
+
MzkyNWQ3ZGQ0NzMzZjQ4NDk3MjZjMzJjZGE4MTYwZGMxNzg0NTg=
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Anil Yanduri
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
= Translator
|
2
|
+
|
3
|
+
== Description
|
4
|
+
Gem to convert between HSL color to RGB and vice versa, RGB color to hex color code and vice versa.
|
5
|
+
|
6
|
+
== Installation
|
7
|
+
|
8
|
+
gem install color_math
|
9
|
+
|
10
|
+
== Usage
|
11
|
+
|
12
|
+
require 'rubygems'
|
13
|
+
require 'color_math'
|
14
|
+
|
15
|
+
cl = ColorMath.new 255, 255, 255
|
16
|
+
|
17
|
+
cl.to_hex # returns hex notation of r g b (255, 255, 255)
|
18
|
+
cl.to_hsl # returns hsl notation of r g b (255, 255, 255)
|
19
|
+
|
20
|
+
also can be initialized from hsl or hex with
|
21
|
+
|
22
|
+
cl = ColorMath.from_hsl(360, 100, 100) #where 360 is hue, 100 is saturation and 100 is luminous or brightness
|
23
|
+
cl = ColorMath.from_hex("#FFFFFF")
|
24
|
+
|
data/lib/color_math.rb
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
class ColorMath
|
2
|
+
|
3
|
+
# r The red color value (int)
|
4
|
+
# g The green color value (int)
|
5
|
+
# b The blue color value (int)
|
6
|
+
def initialize(r, g, b)
|
7
|
+
@r = r
|
8
|
+
@g = g
|
9
|
+
@b = b
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_hex
|
13
|
+
self.class.rgb_to_hex(@r, @g, @b)
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_rgb
|
17
|
+
return @r, @g, @b
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_hsl
|
21
|
+
self.class.rgb_to_hsl(@r, @g, @b)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Class methods
|
25
|
+
|
26
|
+
# creates a color object from hex code
|
27
|
+
def self.from_hex(hex)
|
28
|
+
r, g, b = hex_to_rgb(hex)
|
29
|
+
new(r, g, b)
|
30
|
+
end
|
31
|
+
|
32
|
+
# h The hue (int) 0 - 360
|
33
|
+
# s The saturation (int) 0 - 100
|
34
|
+
# l The lightness (int) 0 - 100
|
35
|
+
def self.from_hsl(h, s, l)
|
36
|
+
r, g, b = hsl_to_rgb(h, s, l)
|
37
|
+
new(r, g, b)
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.rgb_to_hex(r, g, b)
|
41
|
+
rbg = [r,g,b]
|
42
|
+
hex = "#"
|
43
|
+
rbg.each do |component|
|
44
|
+
_hex = component.to_s(16)
|
45
|
+
if component < 16
|
46
|
+
hex << "0#{_hex}"
|
47
|
+
else
|
48
|
+
hex << _hex
|
49
|
+
end
|
50
|
+
end
|
51
|
+
hex.upcase
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.hex_to_rgb(hex)
|
55
|
+
hex.gsub!("#", "")
|
56
|
+
components = hex.scan(/.{2}/)
|
57
|
+
components.collect { |component| component.to_i(16) }
|
58
|
+
end
|
59
|
+
|
60
|
+
# Converts an RGB color value to HSL. Conversion formula
|
61
|
+
# adapted from http://en.wikipedia.org/wiki/HSL_color_space. and
|
62
|
+
# # http://stackoverflow.com/a/9493060/520008
|
63
|
+
# Assumes r, g, and b are contained in the set [0, 255] and
|
64
|
+
|
65
|
+
# r The red color value (int)
|
66
|
+
# g The green color value (int)
|
67
|
+
# b The blue color value (int)
|
68
|
+
# The HSL representation [hue, saturation, luminosity]
|
69
|
+
|
70
|
+
def self.rgb_to_hsl(r, g, b)
|
71
|
+
r /= 255.0
|
72
|
+
g /= 255.0
|
73
|
+
b /= 255.0
|
74
|
+
max = [r, g, b].max
|
75
|
+
min = [r, g, b].min
|
76
|
+
h = (max + min) / 2.0
|
77
|
+
s = (max + min) / 2.0
|
78
|
+
l = (max + min) / 2.0
|
79
|
+
|
80
|
+
if(max == min)
|
81
|
+
h = 0
|
82
|
+
s = 0 # achromatic
|
83
|
+
else
|
84
|
+
d = max - min;
|
85
|
+
s = l >= 0.5 ? d / (2.0 - max - min) : d / (max + min)
|
86
|
+
case max
|
87
|
+
when r
|
88
|
+
h = (g - b) / d + (g < b ? 6.0 : 0)
|
89
|
+
when g
|
90
|
+
h = (b - r) / d + 2.0
|
91
|
+
when b
|
92
|
+
h = (r - g) / d + 4.0
|
93
|
+
end
|
94
|
+
h /= 6.0
|
95
|
+
end
|
96
|
+
return [(h*360).round, (s*100).round, (l*100).round]
|
97
|
+
end
|
98
|
+
|
99
|
+
# Converts an HSL color value to RGB. Conversion formula
|
100
|
+
# adapted from http://en.wikipedia.org/wiki/HSL_color_space. and
|
101
|
+
# # http://stackoverflow.com/a/9493060/520008
|
102
|
+
# returns r, g, and b in the set [0, 255].
|
103
|
+
# h The hue (int) 0 - 360
|
104
|
+
# s The saturation (int) 0 - 100
|
105
|
+
# l The lightness (int) 0 - 100
|
106
|
+
# returns The RGB representation [r, g, b]
|
107
|
+
def self.hsl_to_rgb(h, s, l)
|
108
|
+
h = h/360.0
|
109
|
+
s = s/100.0
|
110
|
+
l = l/100.0
|
111
|
+
|
112
|
+
r = 0.0
|
113
|
+
g = 0.0
|
114
|
+
b = 0.0
|
115
|
+
|
116
|
+
if(s == 0.0)
|
117
|
+
r = l.to_f
|
118
|
+
g = l.to_f
|
119
|
+
b = l.to_f #achromatic
|
120
|
+
else
|
121
|
+
q = l < 0.5 ? l * (1 + s) : l + s - l * s
|
122
|
+
p = 2 * l - q
|
123
|
+
r = hue_to_rgb(p, q, h + 1/3.0)
|
124
|
+
g = hue_to_rgb(p, q, h)
|
125
|
+
b = hue_to_rgb(p, q, h - 1/3.0)
|
126
|
+
end
|
127
|
+
|
128
|
+
return [(r * 255).round, (g * 255).round, (b * 255).round]
|
129
|
+
end
|
130
|
+
|
131
|
+
def self.hue_to_rgb(p, q, t)
|
132
|
+
t += 1 if(t < 0)
|
133
|
+
t -= 1 if(t > 1)
|
134
|
+
return (p + (q - p) * 6 * t) if(t < 1/6.0)
|
135
|
+
return q if(t < 1/2.0)
|
136
|
+
return (p + (q - p) * (2/3.0 - t) * 6) if(t < 2/3.0)
|
137
|
+
return p
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: color_math
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Anil Yanduri
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Gem to convert between HSL color to RGB and vice versa, RGB color to
|
14
|
+
hex color code and vice versa.
|
15
|
+
email: anilkumaryln@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- LICENSE
|
21
|
+
- README.md
|
22
|
+
- README.rdoc
|
23
|
+
- lib/color_math.rb
|
24
|
+
homepage: https://github.com/anilyanduri/color_math
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ! '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.4.8
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: RGB to HSL and RGB to HSV color conversion library in Ruby
|
48
|
+
test_files: []
|