color-generator 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +12 -1
- data/lib/color-generator.rb +66 -41
- data/lib/color-generator/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -10,7 +10,18 @@ If you are using these colors as background colors, consistent lightness lets yo
|
|
10
10
|
## Usage
|
11
11
|
|
12
12
|
require 'color-generator'
|
13
|
-
|
13
|
+
|
14
|
+
Generate colors using the HSL color representation:
|
15
|
+
|
16
|
+
generator = ColorGenerator.new saturation: 0.3, lightness: 0.75
|
17
|
+
color1 = generator.create
|
18
|
+
# => "cfd2ac"
|
19
|
+
color2 = generator.create
|
20
|
+
# => "cbacd2"
|
21
|
+
|
22
|
+
Generate colors using the HSV color representation:
|
23
|
+
|
24
|
+
generator = ColorGenerator.new saturation: 0.3, value: 1.0
|
14
25
|
color1 = generator.create
|
15
26
|
# => "f7b3ff"
|
16
27
|
color2 = generator.create
|
data/lib/color-generator.rb
CHANGED
@@ -1,25 +1,61 @@
|
|
1
1
|
class ColorGenerator
|
2
2
|
GOLDEN_RATIO_CONJUGATE = 0.618033988749895
|
3
3
|
|
4
|
-
attr_reader :hue, :saturation, :value
|
4
|
+
attr_reader :mode, :hue, :saturation, :lightness, :value
|
5
5
|
|
6
6
|
# Initializes a color generator.
|
7
7
|
#
|
8
|
-
# @param [
|
9
|
-
# @
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
# @param [Hash] opts optional arguments
|
9
|
+
# @option opts [Float,Integer] :saturation saturation in the interval [0, 1]
|
10
|
+
# @option opts [Float,Integer] :lightness lightness in the interval [0, 1],
|
11
|
+
# sets the color representation to HSL
|
12
|
+
# @option opts [Float,Integer] :value value in the interval [0, 1], sets the
|
13
|
+
# color representation to HSV
|
14
|
+
def initialize(opts = {})
|
15
|
+
@hue = rand
|
16
|
+
@saturation = opts[:saturation].to_f
|
17
|
+
if opts.has_key? :lightness
|
18
|
+
@mode = :HSL
|
19
|
+
@lightness = opts[:lightness].to_f
|
20
|
+
else
|
21
|
+
@mode = :HSV
|
22
|
+
@value = opts[:value].to_f
|
23
|
+
end
|
14
24
|
end
|
15
25
|
|
16
26
|
# Generates a random color.
|
17
27
|
#
|
18
28
|
# @return [String] an RGB hex triplet
|
19
29
|
def create
|
20
|
-
@hue
|
21
|
-
|
22
|
-
|
30
|
+
@hue = (hue + GOLDEN_RATIO_CONJUGATE) % 1
|
31
|
+
color = if hsl?
|
32
|
+
self.class.rgb_from_hsl hue, saturation, lightness
|
33
|
+
else
|
34
|
+
self.class.rgb_from_hsv hue, saturation, value
|
35
|
+
end
|
36
|
+
'%02x%02x%02x' % color
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [Boolean] whether the color representation is HSL
|
40
|
+
def hsl?
|
41
|
+
mode == :HSL
|
42
|
+
end
|
43
|
+
|
44
|
+
# @return [Boolean] whether the color representation is HSV
|
45
|
+
def hsv?
|
46
|
+
mode == :HSV
|
47
|
+
end
|
48
|
+
|
49
|
+
# Converts a color from HSL to RGB.
|
50
|
+
#
|
51
|
+
# @param [Float] h hue in the interval [0, 1]
|
52
|
+
# @param [Float] s saturation in the interval [0, 1]
|
53
|
+
# @param [Float] l lightness in the interval [0, 1]
|
54
|
+
# @return [Array] an RGB decimal triplet
|
55
|
+
def self.rgb_from_hsl(h, s, l)
|
56
|
+
c = (1 - (2 * l - 1).abs) * s
|
57
|
+
m = l - 0.5 * c
|
58
|
+
rgb_from_hsl_or_hsv h, c, m
|
23
59
|
end
|
24
60
|
|
25
61
|
# Converts a color from HSV to RGB.
|
@@ -28,48 +64,37 @@ class ColorGenerator
|
|
28
64
|
# @param [Float] s saturation in the interval [0, 1]
|
29
65
|
# @param [Float] v value in the interval [0, 1]
|
30
66
|
# @return [Array] an RGB decimal triplet
|
31
|
-
#
|
32
|
-
# @see http://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB
|
33
67
|
def self.rgb_from_hsv(h, s, v)
|
34
68
|
c = v * s
|
35
69
|
m = v - c
|
70
|
+
rgb_from_hsl_or_hsv h, c, m
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
# @param [Float] h hue in the interval [0, 1]
|
76
|
+
# @param [Float] c chroma
|
77
|
+
# @param [Float] m
|
78
|
+
# @return [Array] an RGB decimal triplet
|
79
|
+
#
|
80
|
+
# @see http://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB
|
81
|
+
def self.rgb_from_hsl_or_hsv(h, c, m)
|
36
82
|
h_prime = h * 6
|
37
|
-
|
83
|
+
x = c * (1 - (h_prime % 2 - 1).abs)
|
38
84
|
|
39
|
-
# Unlike the Wikipedia article, we add m right away. This reduces the
|
40
|
-
# number of operations to perform. For example, for H' < 1:
|
41
|
-
#
|
42
|
-
# R = R1 + m
|
43
|
-
# R = C + m
|
44
|
-
# R = C + V - C
|
45
|
-
# R = V
|
46
|
-
#
|
47
|
-
# G = G1 + m
|
48
|
-
# G = X + m
|
49
|
-
# G = C (1 - |H' mod 2 - 1|) + m
|
50
|
-
# G = C (1 - |H' mod 2 - 1|) + V - C
|
51
|
-
# G = C - C |H' mod 2 - 1| + V - C
|
52
|
-
# G = V - C |H' mod 2 - 1|
|
53
|
-
#
|
54
|
-
# B = B1 + m
|
55
|
-
# B = 0 + m
|
56
|
-
# B = m
|
57
|
-
#
|
58
|
-
# The blog post above calculates two X values, for when H' mod 2 is
|
59
|
-
# greater or less than 1.
|
60
85
|
case h_prime.to_i
|
61
86
|
when 0 # 0 <= H' < 1
|
62
|
-
[
|
87
|
+
[c, x, 0]
|
63
88
|
when 1 # 1 <= H' < 2
|
64
|
-
[
|
89
|
+
[x, c, 0]
|
65
90
|
when 2 # 2 <= H' < 3
|
66
|
-
[
|
91
|
+
[0, c, x]
|
67
92
|
when 3 # 3 <= H' < 4
|
68
|
-
[
|
93
|
+
[0, x, c]
|
69
94
|
when 4 # 4 <= H' < 5
|
70
|
-
[
|
95
|
+
[x, 0, c]
|
71
96
|
else # 5 <= H' < 6
|
72
|
-
[
|
73
|
-
end.map{|value| (value * 255).round}
|
97
|
+
[c, 0, x]
|
98
|
+
end.map{|value| ((value + m) * 255).round}
|
74
99
|
end
|
75
100
|
end
|