redgreenblue 0.13.0 → 0.16.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/redgreenblue/24bit.rb +29 -12
- data/lib/redgreenblue/48bit.rb +19 -2
- data/lib/redgreenblue/base.rb +15 -1
- data/lib/redgreenblue/bgr24bit.rb +19 -2
- data/lib/redgreenblue/cie_1931.rb +4 -4
- data/lib/redgreenblue/cie_1976.rb +7 -7
- data/lib/redgreenblue/cie_1994.rb +55 -0
- data/lib/redgreenblue/gamma.rb +1 -1
- data/lib/redgreenblue/gif.rb +1 -1
- data/lib/redgreenblue/gpl.rb +48 -28
- data/lib/redgreenblue/hex.rb +39 -24
- data/lib/redgreenblue/hsb.rb +18 -3
- data/lib/redgreenblue/hsl.rb +21 -4
- data/lib/redgreenblue/hsv.rb +21 -4
- data/lib/redgreenblue/hsx_shared.rb +1 -1
- data/lib/redgreenblue/hwb.rb +14 -0
- data/lib/redgreenblue/inspect.rb +36 -24
- data/lib/redgreenblue/int.rb +19 -2
- data/lib/redgreenblue/lazy.rb +84 -10
- data/lib/redgreenblue/mac.rb +1 -1
- data/lib/redgreenblue/match.rb +29 -1
- data/lib/redgreenblue/math.rb +1 -1
- data/lib/redgreenblue/misc.rb +25 -8
- data/lib/redgreenblue/mix.rb +6 -6
- data/lib/redgreenblue/name.rb +1 -1
- data/lib/redgreenblue/opt/philipshue.rb +5 -3
- data/lib/redgreenblue/os/mac.rb +23 -6
- data/lib/redgreenblue/ostwald.rb +5 -5
- data/lib/redgreenblue/random.rb +21 -4
- data/lib/redgreenblue/rgb565.rb +19 -2
- data/lib/redgreenblue/terminal.rb +1 -1
- data/lib/redgreenblue/version.rb +2 -2
- data/lib/redgreenblue/view.rb +11 -5
- data/lib/redgreenblue/web.rb +67 -14
- data/lib/redgreenblue.rb +22 -3
- metadata +10 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f836ad38758f98dc7f9d677e9bf97dcfd878a09e994622b16b4854d1bff01277
|
4
|
+
data.tar.gz: 938a0a5efefa5c4f181bccdb996be922ad51d8dfd7cfe623209d4582aa19d4e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c40b54efa01653b785065707a57841fd156ba6238a4eccbc09454f969b1cfd08439bbb70431de985874606fe88115e7688dc2e8819c83335bbfd17a0c339a557
|
7
|
+
data.tar.gz: 4a7cdf6351af6e718ac857aaf87c052f1fc85110aca42eb049a22e18f63c25188a5e0189713ff46fa9ee7573e5a4d7729979fdf892a6c1a1f992fd127045618c
|
data/lib/redgreenblue/24bit.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
class RGB
|
1
|
+
class RGB::Color
|
2
2
|
|
3
3
|
# r, g, b methods
|
4
4
|
|
@@ -44,7 +44,7 @@ class RGB
|
|
44
44
|
self.r, self.g, self.b = rgb.flatten
|
45
45
|
end
|
46
46
|
|
47
|
-
# Creates a new object from red, green, and blue components as integers in the range 0..255 (three 8-bit values).
|
47
|
+
# Creates a new Color object from red, green, and blue components as integers in the range 0..255 (three 8-bit values).
|
48
48
|
def self.rgb(*rgb)
|
49
49
|
c = self.new
|
50
50
|
c.rgb = rgb
|
@@ -62,19 +62,36 @@ class RGB
|
|
62
62
|
RGB.rgb rgb
|
63
63
|
end
|
64
64
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
65
|
+
end
|
66
|
+
|
67
|
+
#----------------------------------------------------------------------#
|
68
|
+
# Module Methods #
|
69
|
+
#----------------------------------------------------------------------#
|
70
|
+
|
71
|
+
module RGB
|
72
|
+
|
73
|
+
class << self
|
74
|
+
|
75
|
+
# Creates a new Color object from red, green, and blue components as integers in the range 0..255 (three 8-bit values).
|
76
|
+
def rgb(*rgb)
|
77
|
+
Color.rgb(*rgb)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Calls the given block for each 24-bit RGB color (from black to white), passing the color as an RGB::Color object.
|
81
|
+
#
|
82
|
+
# Returns the number of iterations.
|
83
|
+
def each_24bit_color
|
84
|
+
range = 0..255
|
85
|
+
range.each do |r|
|
86
|
+
range.each do |g|
|
87
|
+
range.each do |b|
|
88
|
+
yield self.rgb(r,g,b)
|
89
|
+
end
|
74
90
|
end
|
75
91
|
end
|
92
|
+
range.size ** 3
|
76
93
|
end
|
77
|
-
|
94
|
+
|
78
95
|
end
|
79
96
|
|
80
97
|
end
|
data/lib/redgreenblue/48bit.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
class RGB
|
1
|
+
class RGB::Color
|
2
2
|
|
3
3
|
# rr, gg, bb methods
|
4
4
|
|
@@ -44,7 +44,7 @@ class RGB
|
|
44
44
|
self.rr, self.gg, self.bb = rrggbb.flatten
|
45
45
|
end
|
46
46
|
|
47
|
-
# Creates a new object from red, green, and blue components as integers in the range 0..65535 (three 16-bit values).
|
47
|
+
# Creates a new Color object from red, green, and blue components as integers in the range 0..65535 (three 16-bit values).
|
48
48
|
def self.rrggbb(*rrggbb)
|
49
49
|
c = self.new
|
50
50
|
c.rrggbb = rrggbb
|
@@ -52,3 +52,20 @@ class RGB
|
|
52
52
|
end
|
53
53
|
|
54
54
|
end
|
55
|
+
|
56
|
+
#----------------------------------------------------------------------#
|
57
|
+
# Module Methods #
|
58
|
+
#----------------------------------------------------------------------#
|
59
|
+
|
60
|
+
module RGB
|
61
|
+
|
62
|
+
class << self
|
63
|
+
|
64
|
+
# Creates a new Color object from red, green, and blue components as integers in the range 0..65535 (three 16-bit values).
|
65
|
+
def rrggbb(*rrggbb)
|
66
|
+
Color.rrggbb(*rrggbb)
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
data/lib/redgreenblue/base.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
class RGB
|
1
|
+
class RGB::Color
|
2
2
|
|
3
3
|
def initialize(*a)
|
4
4
|
self.values = a.any? ? a : [ 0.5, 0.5, 0.5 ]
|
@@ -82,3 +82,17 @@ class RGB
|
|
82
82
|
end
|
83
83
|
|
84
84
|
end
|
85
|
+
|
86
|
+
# The main namespace for redgreenblue.
|
87
|
+
module RGB
|
88
|
+
|
89
|
+
class << self
|
90
|
+
|
91
|
+
# Creates a new Color object from red, green, and blue components as three values between 0 and 1.
|
92
|
+
def new(*a)
|
93
|
+
Color.new(*a)
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
class RGB
|
1
|
+
class RGB::Color
|
2
2
|
|
3
3
|
# Returns a 3-byte string containing the object's color in BGR24 format.
|
4
4
|
def bgr24
|
@@ -10,7 +10,7 @@ class RGB
|
|
10
10
|
self.b, self.g, self.r = bgr_string.unpack('C3')
|
11
11
|
end
|
12
12
|
|
13
|
-
# Creates a new RGB object from BGR24 data (a 3-byte string).
|
13
|
+
# Creates a new RGB::Color object from BGR24 data (a 3-byte string).
|
14
14
|
def self.bgr24(bgr)
|
15
15
|
c = self.new
|
16
16
|
c.bgr24 = bgr
|
@@ -18,3 +18,20 @@ class RGB
|
|
18
18
|
end
|
19
19
|
|
20
20
|
end
|
21
|
+
|
22
|
+
#----------------------------------------------------------------------#
|
23
|
+
# Module Methods #
|
24
|
+
#----------------------------------------------------------------------#
|
25
|
+
|
26
|
+
module RGB
|
27
|
+
|
28
|
+
class << self
|
29
|
+
|
30
|
+
# Creates a new RGB::Color object from BGR24 data (a 3-byte string).
|
31
|
+
def bgr24(bgr)
|
32
|
+
Color.bgr24(bgr)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
class RGB
|
1
|
+
class RGB::Color
|
2
2
|
|
3
|
-
# Returns CIE 1931 XYZ values for the RGB object.
|
3
|
+
# Returns CIE 1931 XYZ values for the RGB::Color object.
|
4
4
|
#
|
5
5
|
# Based on:
|
6
6
|
# - http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html
|
@@ -21,7 +21,7 @@ class RGB
|
|
21
21
|
|
22
22
|
alias xyz cie_xyz
|
23
23
|
|
24
|
-
# Returns CIE 1931 xyY values for the RGB object.
|
24
|
+
# Returns CIE 1931 xyY values for the RGB::Color object.
|
25
25
|
#
|
26
26
|
# Based on:
|
27
27
|
# - https://en.wikipedia.org/wiki/CIE_1931_color_space
|
@@ -41,7 +41,7 @@ class RGB
|
|
41
41
|
|
42
42
|
alias xyy cie_xyy
|
43
43
|
|
44
|
-
# Returns CIE 1931 xy values for the RGB object.
|
44
|
+
# Returns CIE 1931 xy values for the RGB::Color object.
|
45
45
|
def cie_xy
|
46
46
|
cie_xyy[0..1]
|
47
47
|
end
|
@@ -1,27 +1,27 @@
|
|
1
|
-
class RGB
|
1
|
+
class RGB::Color
|
2
2
|
|
3
|
-
# Returns CIE 1976 L*a*b* (CIELAB) values for the RGB object.
|
3
|
+
# Returns CIE 1976 L*a*b* (CIELAB) values for the RGB::Color object.
|
4
4
|
def cie_lab(round: true)
|
5
5
|
cie_lab_luv(round: round, type: :lab)
|
6
6
|
end
|
7
7
|
|
8
8
|
alias lab cie_lab
|
9
9
|
|
10
|
-
# Returns CIE 1976 LCHab values for the RGB object, derived from L*a*b* (CIELAB).
|
10
|
+
# Returns CIE 1976 LCHab values for the RGB::Color object, derived from L*a*b* (CIELAB).
|
11
11
|
#
|
12
12
|
# When C is 0, H is nil.
|
13
13
|
def cie_lch_ab
|
14
14
|
cie_lch_ab_uv(type: :lab)
|
15
15
|
end
|
16
16
|
|
17
|
-
# Returns CIE 1976 L*u*v* (CIELUV) values for the RGB object.
|
17
|
+
# Returns CIE 1976 L*u*v* (CIELUV) values for the RGB::Color object.
|
18
18
|
def cie_luv(round: true)
|
19
19
|
cie_lab_luv(round: round, type: :luv)
|
20
20
|
end
|
21
21
|
|
22
22
|
alias luv cie_luv
|
23
23
|
|
24
|
-
# Returns CIE 1976 LCHuv values for the RGB object, derived from L*u*v* (CIELUV).
|
24
|
+
# Returns CIE 1976 LCHuv values for the RGB::Color object, derived from L*u*v* (CIELUV).
|
25
25
|
#
|
26
26
|
# When C is 0, H is nil.
|
27
27
|
def cie_lch_uv
|
@@ -44,7 +44,7 @@ class RGB
|
|
44
44
|
|
45
45
|
private
|
46
46
|
|
47
|
-
# Returns either CIE 1976 L*a*b* (CIELAB) or CIE 1976 L*u*v* (CIELUV) values for the RGB object.
|
47
|
+
# Returns either CIE 1976 L*a*b* (CIELAB) or CIE 1976 L*u*v* (CIELUV) values for the RGB::Color object.
|
48
48
|
#
|
49
49
|
# L*a*b* formula based on:
|
50
50
|
# - http://www.brucelindbloom.com/Eqn_XYZ_to_Lab.html
|
@@ -82,7 +82,7 @@ class RGB
|
|
82
82
|
end.map { |v| v.nan? ? 0.0 : ( round ? v.round(8) : v ) }
|
83
83
|
end
|
84
84
|
|
85
|
-
# Returns either CIE 1976 LCHab or CIE 1976 LCHuv values for the RGB object.
|
85
|
+
# Returns either CIE 1976 LCHab or CIE 1976 LCHuv values for the RGB::Color object.
|
86
86
|
#
|
87
87
|
# Based on:
|
88
88
|
# - http://www.brucelindbloom.com/Eqn_Lab_to_LCH.html
|
@@ -0,0 +1,55 @@
|
|
1
|
+
class RGB::Color
|
2
|
+
|
3
|
+
# Returns the difference between this (reference) color and another color, according to the CIE 1994 delta E formula.
|
4
|
+
#
|
5
|
+
# By default uses parameters for use in graphic arts, and reference conditions.
|
6
|
+
# Parameters (k1, k2, kl, kc, kh) can be individually overriden for different applications and for variations in conditions.
|
7
|
+
#
|
8
|
+
# Based on:
|
9
|
+
# - http://www.brucelindbloom.com/Eqn_DeltaE_CIE94.html
|
10
|
+
# - https://archive.org/details/gov.law.cie.15.2004
|
11
|
+
# - https://en.wikipedia.org/wiki/Color_difference
|
12
|
+
def delta_e_cie_1994(another, k1: 0.045, k2: 0.015, kl: 1, kc: 1, kh: 1)
|
13
|
+
|
14
|
+
l , a , b = cie_lab(round: false)
|
15
|
+
l2, a2, b2 = another.cie_lab(round: false)
|
16
|
+
|
17
|
+
c = Math.hypot(a , b )
|
18
|
+
c2 = Math.hypot(a2, b2)
|
19
|
+
|
20
|
+
da = a - a2
|
21
|
+
db = b - b2
|
22
|
+
dc = c - c2
|
23
|
+
|
24
|
+
dh2 = (da ** 2) + (db ** 2) - (dc ** 2)
|
25
|
+
dl = l - l2
|
26
|
+
|
27
|
+
sl = 1
|
28
|
+
sc = 1 + k1 * c
|
29
|
+
sh = 1 + k2 * c
|
30
|
+
|
31
|
+
Math.sqrt(
|
32
|
+
( (dl / ( kl*sl)) ** 2 ) +
|
33
|
+
( (dc / ( kc*sc)) ** 2 ) +
|
34
|
+
( dh2 / ((kh*sh) ** 2) )
|
35
|
+
).round(6)
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
alias de94 delta_e_cie_1994
|
40
|
+
|
41
|
+
# Returns the difference between this (reference) color and another color, according to the CIE 1994 delta E formula.
|
42
|
+
#
|
43
|
+
# For use in graphic arts, under reference conditions.
|
44
|
+
def de94g(another)
|
45
|
+
delta_e_cie_1994(another)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Returns the difference between this (reference) color and another color, according to the CIE 1994 delta E formula.
|
49
|
+
#
|
50
|
+
# For use with textiles, under reference conditions.
|
51
|
+
def de94t(another)
|
52
|
+
delta_e_cie_1994(another, k1: 0.048, k2: 0.014, kl: 2)
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
data/lib/redgreenblue/gamma.rb
CHANGED
data/lib/redgreenblue/gif.rb
CHANGED
data/lib/redgreenblue/gpl.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
class RGB
|
1
|
+
class RGB::Color
|
2
2
|
|
3
3
|
#----------------------------------------------------------------------#
|
4
4
|
# Class Methods #
|
@@ -6,13 +6,13 @@ class RGB
|
|
6
6
|
|
7
7
|
class << self
|
8
8
|
|
9
|
-
# Creates a new RGB object from a line of gpl (Gimp color palette) input. Returns nil if not successful.
|
9
|
+
# Creates a new RGB::Color object from a line of gpl (Gimp color palette) input. Returns nil if not successful.
|
10
10
|
#
|
11
11
|
# @example
|
12
|
-
# RGB.gpl "255 153 204\tpink"
|
12
|
+
# RGB::Color.gpl "255 153 204\tpink"
|
13
13
|
def gpl(line)
|
14
14
|
if line.chomp.match( /^\s*(?<r>\d{1,3})\s+(?<g>\d{1,3})\s+(?<b>\d{1,3})(\s+(?<name>.*))?/ )
|
15
|
-
color = RGB.rgb $~[:r].to_i, $~[:g].to_i, $~[:b].to_i
|
15
|
+
color = RGB::Color.rgb $~[:r].to_i, $~[:g].to_i, $~[:b].to_i
|
16
16
|
color.name = $~[:name] if $~[:name]
|
17
17
|
color
|
18
18
|
else
|
@@ -20,7 +20,49 @@ class RGB
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
|
23
|
+
end
|
24
|
+
|
25
|
+
#----------------------------------------------------------------------#
|
26
|
+
# Instance Methods #
|
27
|
+
#----------------------------------------------------------------------#
|
28
|
+
|
29
|
+
# Returns the color in the format used in .gpl files (Gimp color palettes), including its name (if present).
|
30
|
+
#
|
31
|
+
# You can optionally supply a name as argument.
|
32
|
+
def gpl(gpl_name=name)
|
33
|
+
( "%3d %3d %3d" % rgb ) +
|
34
|
+
( gpl_name.to_s.size != 0 ? "\t#{gpl_name}" : '' )
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
module RGB
|
41
|
+
|
42
|
+
class << self
|
43
|
+
|
44
|
+
# Creates a new RGB::Color object from a line of gpl (Gimp color palette) input. Returns nil if not successful.
|
45
|
+
#
|
46
|
+
# @example
|
47
|
+
# RGB.gpl "255 153 204\tpink"
|
48
|
+
def gpl(line)
|
49
|
+
Color.gpl(line)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Returns a header for a .gpl file (Gimp color palette). Includes an optional name and number of columns.
|
53
|
+
#
|
54
|
+
# @example
|
55
|
+
# RGB.gpl_header('Spring')
|
56
|
+
#
|
57
|
+
# Reverse-engineered from:
|
58
|
+
# - https://github.com/GNOME/gimp/blob/5d79fba8238a27b8691556489898d33b3fa0dda0/app/core/gimppalette-load.c
|
59
|
+
def gpl_header(name=nil, columns: nil)
|
60
|
+
"GIMP Palette\n" +
|
61
|
+
( name ? "Name: #{name}\n" : '' ) +
|
62
|
+
( columns ? "Columns: #{columns}\n" : '' )
|
63
|
+
end
|
64
|
+
|
65
|
+
# Loads a gpl (Gimp color palette) source and returns an array of RGB::Color objects.
|
24
66
|
#
|
25
67
|
# Options:
|
26
68
|
# - file: Path to a .gpl file to be loaded.
|
@@ -47,7 +89,7 @@ class RGB
|
|
47
89
|
end
|
48
90
|
|
49
91
|
if source.respond_to? :each_line
|
50
|
-
list = source.each_line.map { |line|
|
92
|
+
list = source.each_line.map { |line| Color.gpl(line) }
|
51
93
|
|
52
94
|
if compact
|
53
95
|
list.compact!
|
@@ -64,28 +106,6 @@ class RGB
|
|
64
106
|
end
|
65
107
|
end
|
66
108
|
|
67
|
-
private
|
68
|
-
|
69
|
-
# Reverse-engineered from:
|
70
|
-
# - https://github.com/GNOME/gimp/blob/5d79fba8238a27b8691556489898d33b3fa0dda0/app/core/gimppalette-load.c
|
71
|
-
def gpl_header(name, columns=nil)
|
72
|
-
"GIMP Palette\n" +
|
73
|
-
"Name: #{name}\n" +
|
74
|
-
( columns ? "Columns: #{columns}\n" : '' )
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
#----------------------------------------------------------------------#
|
80
|
-
# Instance Methods #
|
81
|
-
#----------------------------------------------------------------------#
|
82
|
-
|
83
|
-
# Returns the color in the format used in .gpl files (Gimp color palettes), including its name (if present).
|
84
|
-
#
|
85
|
-
# You can optionally supply a name as argument.
|
86
|
-
def gpl(gpl_name=name)
|
87
|
-
( "%3d %3d %3d" % rgb ) +
|
88
|
-
( gpl_name.to_s.size != 0 ? "\t#{gpl_name}" : '' )
|
89
109
|
end
|
90
110
|
|
91
111
|
end
|
data/lib/redgreenblue/hex.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
class RGB
|
1
|
+
class RGB::Color
|
2
2
|
|
3
3
|
# Returns a 6-digit hexadecimal string representing the object's red, green, and blue components as 8-bit values.
|
4
4
|
#
|
@@ -22,11 +22,17 @@ class RGB
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
private
|
25
26
|
|
26
|
-
|
27
|
+
def hex6
|
28
|
+
'%02x%02x%02x' % [ r, g, b ]
|
29
|
+
end
|
27
30
|
|
31
|
+
#----------------------------------------------------------------------#
|
32
|
+
# Class Methods #
|
33
|
+
#----------------------------------------------------------------------#
|
28
34
|
|
29
|
-
# Creates a new object from a 6- or 3-digit hexadecimal string representing red, green, and blue.
|
35
|
+
# Creates a new Color object from a 6- or 3-digit hexadecimal string representing red, green, and blue.
|
30
36
|
#
|
31
37
|
# The string may include a '#' prefix.
|
32
38
|
def self.hex(hex_string)
|
@@ -37,31 +43,40 @@ class RGB
|
|
37
43
|
end
|
38
44
|
end
|
39
45
|
|
40
|
-
|
41
|
-
#
|
42
|
-
# If a shorthand version is not possible, returns the original string.
|
43
|
-
def self.hex_shorthand(hex_string)
|
44
|
-
hex_string.sub( /^(#?)(\h)\2(\h)\3(\h)\4$/, '\1\2\3\4' )
|
45
|
-
end
|
46
|
+
end
|
46
47
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
else
|
57
|
-
nil
|
48
|
+
module RGB
|
49
|
+
|
50
|
+
class << self
|
51
|
+
|
52
|
+
# Creates a new Color object from a 6- or 3-digit hexadecimal string representing red, green, and blue.
|
53
|
+
#
|
54
|
+
# The string may include a '#' prefix.
|
55
|
+
def hex(hex_string)
|
56
|
+
Color.hex(hex_string)
|
58
57
|
end
|
59
|
-
end
|
60
58
|
|
61
|
-
|
59
|
+
# Returns a 3-digit shorthand version of a 6-digit hexadecimal string.
|
60
|
+
#
|
61
|
+
# If a shorthand version is not possible, returns the original string.
|
62
|
+
def hex_shorthand(hex_string)
|
63
|
+
hex_string.sub( /^(#?)(\h)\2(\h)\3(\h)\4$/, '\1\2\3\4' )
|
64
|
+
end
|
65
|
+
|
66
|
+
# Parses a 6- or 3-digit hexadecimal string.
|
67
|
+
# Returns three integers in the range 0..255, or nil if not successful.
|
68
|
+
#
|
69
|
+
# The string may include a '#' prefix.
|
70
|
+
def hex_to_rgb(hex_string)
|
71
|
+
if hex_string =~ /^(#?)(\h\h)(\h\h)(\h\h)$/
|
72
|
+
[$2, $3, $4].map { |h| h.to_i(16) }
|
73
|
+
elsif hex_string =~ /^(#?)(\h)(\h)(\h)$/
|
74
|
+
[$2, $3, $4].map { |h| (h*2).to_i(16) }
|
75
|
+
else
|
76
|
+
nil
|
77
|
+
end
|
78
|
+
end
|
62
79
|
|
63
|
-
def hex6
|
64
|
-
'%02x%02x%02x' % [ r, g, b ]
|
65
80
|
end
|
66
81
|
|
67
82
|
end
|
data/lib/redgreenblue/hsb.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'redgreenblue/hsv'
|
2
2
|
|
3
|
-
class RGB
|
3
|
+
class RGB::Color
|
4
4
|
|
5
5
|
#----------------------------------------------------------------------#
|
6
6
|
# Class Methods #
|
@@ -8,7 +8,7 @@ class RGB
|
|
8
8
|
|
9
9
|
class << self
|
10
10
|
|
11
|
-
# Creates a new RGB object from HSB values: hue (0..360), saturation (0..1), and brightness (0..1).
|
11
|
+
# Creates a new RGB::Color object from HSB values: hue (0..360), saturation (0..1), and brightness (0..1).
|
12
12
|
alias hsb hsv
|
13
13
|
|
14
14
|
end
|
@@ -55,7 +55,22 @@ class RGB
|
|
55
55
|
# Sets red, green, and blue by rotating the object's HSB-hue a number of degrees.
|
56
56
|
alias hsb_rotate! hsv_rotate!
|
57
57
|
|
58
|
-
# Creates one or more new RGB objects by rotating this object's HSB-hue a number of degrees.
|
58
|
+
# Creates one or more new RGB::Color objects by rotating this object's HSB-hue a number of degrees.
|
59
59
|
alias hsb_rotate hsv_rotate
|
60
60
|
|
61
61
|
end
|
62
|
+
|
63
|
+
#----------------------------------------------------------------------#
|
64
|
+
# Module Methods #
|
65
|
+
#----------------------------------------------------------------------#
|
66
|
+
|
67
|
+
module RGB
|
68
|
+
|
69
|
+
class << self
|
70
|
+
|
71
|
+
# Creates a new RGB::Color object from HSB values: hue (0..360), saturation (0..1), and brightness (0..1).
|
72
|
+
alias hsb hsv
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
data/lib/redgreenblue/hsl.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'redgreenblue/hsx_shared'
|
2
2
|
require 'redgreenblue/math'
|
3
3
|
|
4
|
-
class RGB
|
4
|
+
class RGB::Color
|
5
5
|
|
6
6
|
#----------------------------------------------------------------------#
|
7
7
|
# Class Methods #
|
@@ -9,7 +9,7 @@ class RGB
|
|
9
9
|
|
10
10
|
class << self
|
11
11
|
|
12
|
-
# Creates a new RGB object from HSL values: hue (0..360), saturation (0..1), and lightness (0..1).
|
12
|
+
# Creates a new RGB::Color object from HSL values: hue (0..360), saturation (0..1), and lightness (0..1).
|
13
13
|
def hsl(*a)
|
14
14
|
new hsl_to_values(*a)
|
15
15
|
end
|
@@ -51,7 +51,7 @@ class RGB
|
|
51
51
|
|
52
52
|
# Sets red, green, and blue using HSL values: hue (0..360), saturation (0..1), and lightness (0..1).
|
53
53
|
def hsl=(*a)
|
54
|
-
self.values = RGB.hsl_to_values(*a)
|
54
|
+
self.values = RGB::Color.hsl_to_values(*a)
|
55
55
|
end
|
56
56
|
|
57
57
|
# Sets HSL-hue to a number of degrees (0..360) or nil.
|
@@ -84,7 +84,7 @@ class RGB
|
|
84
84
|
self
|
85
85
|
end
|
86
86
|
|
87
|
-
# Creates one or more new RGB objects by rotating this object's HSL-hue a number of degrees.
|
87
|
+
# Creates one or more new RGB::Color objects by rotating this object's HSL-hue a number of degrees.
|
88
88
|
def hsl_rotate(a_degrees, *b_degrees)
|
89
89
|
if a_degrees.class != Array and b_degrees.none?
|
90
90
|
RGB.hsl zip_add(hsl, [a_degrees, 0, 0])
|
@@ -94,3 +94,20 @@ class RGB
|
|
94
94
|
end
|
95
95
|
|
96
96
|
end
|
97
|
+
|
98
|
+
#----------------------------------------------------------------------#
|
99
|
+
# Module Methods #
|
100
|
+
#----------------------------------------------------------------------#
|
101
|
+
|
102
|
+
module RGB
|
103
|
+
|
104
|
+
class << self
|
105
|
+
|
106
|
+
# Creates a new RGB::Color object from HSL values: hue (0..360), saturation (0..1), and lightness (0..1).
|
107
|
+
def hsl(*a)
|
108
|
+
Color.hsl(*a)
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
data/lib/redgreenblue/hsv.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'redgreenblue/hsx_shared'
|
2
2
|
require 'redgreenblue/math'
|
3
3
|
|
4
|
-
class RGB
|
4
|
+
class RGB::Color
|
5
5
|
|
6
6
|
#----------------------------------------------------------------------#
|
7
7
|
# Class Methods #
|
@@ -9,7 +9,7 @@ class RGB
|
|
9
9
|
|
10
10
|
class << self
|
11
11
|
|
12
|
-
# Creates a new RGB object from HSV values: hue (0..360), saturation (0..1), and value (0..1).
|
12
|
+
# Creates a new RGB::Color object from HSV values: hue (0..360), saturation (0..1), and value (0..1).
|
13
13
|
def hsv(*a)
|
14
14
|
new hsv_to_values(*a)
|
15
15
|
end
|
@@ -51,7 +51,7 @@ class RGB
|
|
51
51
|
|
52
52
|
# Sets red, green, and blue using HSV values: hue (0..360), saturation (0..1), and value (0..1).
|
53
53
|
def hsv=(*a)
|
54
|
-
self.values = RGB.hsv_to_values(*a)
|
54
|
+
self.values = RGB::Color.hsv_to_values(*a)
|
55
55
|
end
|
56
56
|
|
57
57
|
# Sets HSV-hue to a number of degrees (0..360) or nil.
|
@@ -84,7 +84,7 @@ class RGB
|
|
84
84
|
self
|
85
85
|
end
|
86
86
|
|
87
|
-
# Creates one or more new RGB objects by rotating this object's HSV-hue a number of degrees.
|
87
|
+
# Creates one or more new RGB::Color objects by rotating this object's HSV-hue a number of degrees.
|
88
88
|
def hsv_rotate(a_degrees, *b_degrees)
|
89
89
|
if a_degrees.class != Array and b_degrees.none?
|
90
90
|
RGB.hsv zip_add(hsv, [a_degrees, 0, 0])
|
@@ -94,3 +94,20 @@ class RGB
|
|
94
94
|
end
|
95
95
|
|
96
96
|
end
|
97
|
+
|
98
|
+
#----------------------------------------------------------------------#
|
99
|
+
# Module Methods #
|
100
|
+
#----------------------------------------------------------------------#
|
101
|
+
|
102
|
+
module RGB
|
103
|
+
|
104
|
+
class << self
|
105
|
+
|
106
|
+
# Creates a new RGB::Color object from HSV values: hue (0..360), saturation (0..1), and value (0..1).
|
107
|
+
def hsv(*a)
|
108
|
+
Color.hsv(*a)
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class RGB::Color
|
2
|
+
|
3
|
+
# Returns the color's hue (0..360), whiteness (0..1), and blackness (0..1), as defined by the HWB color model.
|
4
|
+
#
|
5
|
+
# For achromatic colors, hue is nil.
|
6
|
+
#
|
7
|
+
# Based on:
|
8
|
+
# - http://alvyray.com/Papers/CG/HWB_JGTv208.pdf (PDF)
|
9
|
+
# - https://en.wikipedia.org/wiki/HWB_color_model
|
10
|
+
def hwb
|
11
|
+
[ hsv_h, cwk[1,2] ].flatten
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|