redgreenblue 0.14.0 → 0.17.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 +4 -4
- data/lib/redgreenblue/24bit.rb +27 -13
- data/lib/redgreenblue/48bit.rb +16 -2
- data/lib/redgreenblue/base.rb +15 -1
- data/lib/redgreenblue/bgr24bit.rb +16 -2
- data/lib/redgreenblue/cie_1931.rb +60 -12
- 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 +45 -35
- data/lib/redgreenblue/hex.rb +36 -24
- data/lib/redgreenblue/hsb.rb +15 -11
- data/lib/redgreenblue/hsl.rb +18 -12
- data/lib/redgreenblue/hsv.rb +18 -12
- data/lib/redgreenblue/hsx_shared.rb +1 -9
- data/lib/redgreenblue/hwb.rb +1 -1
- data/lib/redgreenblue/inspect.rb +33 -24
- data/lib/redgreenblue/int.rb +16 -2
- data/lib/redgreenblue/lazy.rb +81 -13
- 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 +22 -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 +3 -3
- data/lib/redgreenblue/random.rb +18 -4
- data/lib/redgreenblue/rgb565.rb +16 -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 +58 -56
- data/lib/redgreenblue.rb +21 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2359fa39f6387727f76770372b1f798ae60fdf34dce8bd481b06d58e5a128c4b
|
4
|
+
data.tar.gz: 6194f07e51ce46a4c9bdd3fd7d2fd895eebcb8e6f34e76c37b3c078d3ea58fb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e76a1651bfac0b0f197cae93a9fa3139e798a3a2ab0ae00950d08eb5e724834f9ccce7fe06beb98b083988fd4d7b672431aee5c23a66c24cce2782669606b517
|
7
|
+
data.tar.gz: 688367a83417deb0a812e96982f9c44ec369a1ee6984836338c25b07bd11843ced02053341044cebcaf2e4e71ce5ef26316799850831e446b85ece5766d31ddf
|
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
|
47
|
+
# Creates a new RGB::Color 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
|
@@ -57,24 +57,38 @@ class RGB
|
|
57
57
|
self
|
58
58
|
end
|
59
59
|
|
60
|
-
# Creates a new RGB
|
60
|
+
# Creates a new RGB::Color containing the nearest 24-bit color.
|
61
61
|
def snap
|
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 RGB
|
69
|
+
|
70
|
+
class << self
|
71
|
+
|
72
|
+
# Creates a new RGB::Color from red, green, and blue components as integers in the range 0..255 (three 8-bit values).
|
73
|
+
def rgb(*rgb)
|
74
|
+
Color.rgb(*rgb)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Calls the given block for each 24-bit RGB color (from black to white), passing the color as an RGB::Color object.
|
78
|
+
#
|
79
|
+
# Returns the number of iterations.
|
80
|
+
def each_24bit_color
|
81
|
+
range = 0..255
|
82
|
+
range.each do |r|
|
83
|
+
range.each do |g|
|
84
|
+
range.each do |b|
|
85
|
+
yield self.rgb(r,g,b)
|
86
|
+
end
|
74
87
|
end
|
75
88
|
end
|
89
|
+
range.size ** 3
|
76
90
|
end
|
77
|
-
|
91
|
+
|
78
92
|
end
|
79
93
|
|
80
94
|
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
|
47
|
+
# Creates a new RGB::Color 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,17 @@ class RGB
|
|
52
52
|
end
|
53
53
|
|
54
54
|
end
|
55
|
+
|
56
|
+
|
57
|
+
module RGB
|
58
|
+
|
59
|
+
class << self
|
60
|
+
|
61
|
+
# Creates a new RGB::Color from red, green, and blue components as integers in the range 0..65535 (three 16-bit values).
|
62
|
+
def rrggbb(*rrggbb)
|
63
|
+
Color.rrggbb(*rrggbb)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
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 RGB::Color 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
|
13
|
+
# Creates a new RGB::Color 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,17 @@ class RGB
|
|
18
18
|
end
|
19
19
|
|
20
20
|
end
|
21
|
+
|
22
|
+
|
23
|
+
module RGB
|
24
|
+
|
25
|
+
class << self
|
26
|
+
|
27
|
+
# Creates a new RGB::Color from BGR24 data (a 3-byte string).
|
28
|
+
def bgr24(bgr)
|
29
|
+
Color.bgr24(bgr)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -1,6 +1,21 @@
|
|
1
|
-
class RGB
|
1
|
+
class RGB::Color
|
2
2
|
|
3
|
-
|
3
|
+
class << self
|
4
|
+
|
5
|
+
# Creates a new RGB::Color from CIE 1931 XYZ values.
|
6
|
+
#
|
7
|
+
# Assumes the XYZ values are relative to D65 reference white, the same as used in sRGB.
|
8
|
+
def cie_xyz(*a)
|
9
|
+
c = self.new
|
10
|
+
c.cie_xyz = a
|
11
|
+
c
|
12
|
+
end
|
13
|
+
|
14
|
+
alias xyz cie_xyz
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns CIE 1931 XYZ values for the RGB::Color object.
|
4
19
|
#
|
5
20
|
# Based on:
|
6
21
|
# - http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html
|
@@ -11,41 +26,74 @@ class RGB
|
|
11
26
|
r, g, b = linear_values
|
12
27
|
|
13
28
|
[
|
14
|
-
|
15
29
|
r * 0.4124_5643_9090 + g * 0.3575_7607_7644 + b * 0.1804_3748_3266,
|
16
30
|
r * 0.2126_7285_1406 + g * 0.7151_5215_5288 + b * 0.0721_7499_3307,
|
17
31
|
r * 0.0193_3389_5582 + g * 0.1191_9202_5881 + b * 0.9503_0407_8536
|
18
|
-
|
19
32
|
].map { |v| round ? v.round(8) : v }
|
20
33
|
end
|
21
34
|
|
22
35
|
alias xyz cie_xyz
|
23
36
|
|
24
|
-
#
|
37
|
+
# Sets the red, green, and blue values by converting the given CIE 1931 XYZ values to RGB.
|
38
|
+
#
|
39
|
+
# Assumes the XYZ values are relative to D65 reference white, the same as used in sRGB.
|
40
|
+
#
|
41
|
+
# Based on:
|
42
|
+
# - http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html
|
43
|
+
# XYZ to sRGB matrix for D65 reference white calculated with Javascript by Bruce Lindbloom:
|
44
|
+
# - http://www.brucelindbloom.com/ColorCalculator.html
|
45
|
+
def cie_xyz=(*a)
|
46
|
+
x, y, z = a.flatten
|
47
|
+
self.linear_values = [
|
48
|
+
x * 3.2404_5416_2114 + y * -1.5371_3851_2798 + z * -0.498_5314_09556,
|
49
|
+
x * -0.9692_6603_0505 + y * 1.8760_1084_5447 + z * 0.0415_5601_7530,
|
50
|
+
x * 0.0556_4343_0959 + y * -0.2040_2591_3517 + z * 1.0572_2518_8223
|
51
|
+
]
|
52
|
+
end
|
53
|
+
|
54
|
+
alias xyz= cie_xyz=
|
55
|
+
|
56
|
+
# Returns CIE 1931 xyY values for the RGB::Color object.
|
25
57
|
#
|
26
58
|
# Based on:
|
27
59
|
# - https://en.wikipedia.org/wiki/CIE_1931_color_space
|
28
60
|
# - http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_xyY.html
|
29
61
|
# - https://ninedegreesbelow.com/photography/xyz-rgb.html
|
30
|
-
def cie_xyy
|
62
|
+
def cie_xyy(round: true)
|
31
63
|
x, y, z = cie_xyz(round: false)
|
32
64
|
|
33
65
|
[
|
34
|
-
|
35
66
|
x / ( x + y + z ),
|
36
67
|
y / ( x + y + z ),
|
37
68
|
y
|
38
|
-
|
39
|
-
].map { |v| v.round(8) }
|
69
|
+
].map { |v| round ? v.round(8) : v }
|
40
70
|
end
|
41
71
|
|
42
72
|
alias xyy cie_xyy
|
43
73
|
|
44
|
-
# Returns CIE 1931 xy values for the RGB object.
|
45
|
-
def cie_xy
|
46
|
-
cie_xyy[0..1]
|
74
|
+
# Returns CIE 1931 xy values for the RGB::Color object.
|
75
|
+
def cie_xy(round: true)
|
76
|
+
cie_xyy(round: round)[0..1]
|
47
77
|
end
|
48
78
|
|
49
79
|
alias xy cie_xy
|
50
80
|
|
51
81
|
end
|
82
|
+
|
83
|
+
|
84
|
+
module RGB
|
85
|
+
|
86
|
+
class << self
|
87
|
+
|
88
|
+
# Creates a new RGB::Color from CIE 1931 XYZ values.
|
89
|
+
#
|
90
|
+
# Assumes the XYZ values are relative to D65 reference white, the same as used in sRGB.
|
91
|
+
def cie_xyz(*a)
|
92
|
+
RGB::Color.cie_xyz(a)
|
93
|
+
end
|
94
|
+
|
95
|
+
alias xyz cie_xyz
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
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,18 +1,14 @@
|
|
1
|
-
class RGB
|
2
|
-
|
3
|
-
#----------------------------------------------------------------------#
|
4
|
-
# Class Methods #
|
5
|
-
#----------------------------------------------------------------------#
|
1
|
+
class RGB::Color
|
6
2
|
|
7
3
|
class << self
|
8
4
|
|
9
|
-
# Creates a new RGB
|
5
|
+
# Creates a new RGB::Color from a line of gpl (Gimp color palette) input. Returns nil if not successful.
|
10
6
|
#
|
11
7
|
# @example
|
12
|
-
# RGB.gpl "255 153 204\tpink"
|
8
|
+
# RGB::Color.gpl "255 153 204\tpink"
|
13
9
|
def gpl(line)
|
14
10
|
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
|
11
|
+
color = RGB::Color.rgb $~[:r].to_i, $~[:g].to_i, $~[:b].to_i
|
16
12
|
color.name = $~[:name] if $~[:name]
|
17
13
|
color
|
18
14
|
else
|
@@ -20,7 +16,46 @@ class RGB
|
|
20
16
|
end
|
21
17
|
end
|
22
18
|
|
23
|
-
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
# Returns the color in the format used in .gpl files (Gimp color palettes), including its name (if present).
|
23
|
+
#
|
24
|
+
# You can optionally supply a name as argument.
|
25
|
+
def gpl(gpl_name=name)
|
26
|
+
( "%3d %3d %3d" % rgb ) +
|
27
|
+
( gpl_name.to_s.size != 0 ? "\t#{gpl_name}" : '' )
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
module RGB
|
34
|
+
|
35
|
+
class << self
|
36
|
+
|
37
|
+
# Creates a new RGB::Color from a line of gpl (Gimp color palette) input. Returns nil if not successful.
|
38
|
+
#
|
39
|
+
# @example
|
40
|
+
# RGB.gpl "255 153 204\tpink"
|
41
|
+
def gpl(line)
|
42
|
+
Color.gpl(line)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns a header for a .gpl file (Gimp color palette). Includes an optional name and number of columns.
|
46
|
+
#
|
47
|
+
# @example
|
48
|
+
# RGB.gpl_header('Spring')
|
49
|
+
#
|
50
|
+
# Reverse-engineered from:
|
51
|
+
# - https://github.com/GNOME/gimp/blob/5d79fba8238a27b8691556489898d33b3fa0dda0/app/core/gimppalette-load.c
|
52
|
+
def gpl_header(name=nil, columns: nil)
|
53
|
+
"GIMP Palette\n" +
|
54
|
+
( name ? "Name: #{name}\n" : '' ) +
|
55
|
+
( columns ? "Columns: #{columns}\n" : '' )
|
56
|
+
end
|
57
|
+
|
58
|
+
# Loads a gpl (Gimp color palette) source and returns an array of RGB::Color objects.
|
24
59
|
#
|
25
60
|
# Options:
|
26
61
|
# - file: Path to a .gpl file to be loaded.
|
@@ -47,7 +82,7 @@ class RGB
|
|
47
82
|
end
|
48
83
|
|
49
84
|
if source.respond_to? :each_line
|
50
|
-
list = source.each_line.map { |line|
|
85
|
+
list = source.each_line.map { |line| Color.gpl(line) }
|
51
86
|
|
52
87
|
if compact
|
53
88
|
list.compact!
|
@@ -64,31 +99,6 @@ class RGB
|
|
64
99
|
end
|
65
100
|
end
|
66
101
|
|
67
|
-
# Returns a header for a .gpl file (Gimp color palette). Includes an optional name and number of columns.
|
68
|
-
#
|
69
|
-
# @example
|
70
|
-
# RGB.gpl_header('Spring')
|
71
|
-
#
|
72
|
-
# Reverse-engineered from:
|
73
|
-
# - https://github.com/GNOME/gimp/blob/5d79fba8238a27b8691556489898d33b3fa0dda0/app/core/gimppalette-load.c
|
74
|
-
def gpl_header(name=nil, columns: nil)
|
75
|
-
"GIMP Palette\n" +
|
76
|
-
( name ? "Name: #{name}\n" : '' ) +
|
77
|
-
( columns ? "Columns: #{columns}\n" : '' )
|
78
|
-
end
|
79
|
-
|
80
|
-
end
|
81
|
-
|
82
|
-
#----------------------------------------------------------------------#
|
83
|
-
# Instance Methods #
|
84
|
-
#----------------------------------------------------------------------#
|
85
|
-
|
86
|
-
# Returns the color in the format used in .gpl files (Gimp color palettes), including its name (if present).
|
87
|
-
#
|
88
|
-
# You can optionally supply a name as argument.
|
89
|
-
def gpl(gpl_name=name)
|
90
|
-
( "%3d %3d %3d" % rgb ) +
|
91
|
-
( gpl_name.to_s.size != 0 ? "\t#{gpl_name}" : '' )
|
92
102
|
end
|
93
103
|
|
94
104
|
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,14 @@ 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
|
|
28
31
|
|
29
|
-
# Creates a new
|
32
|
+
# Creates a new RGB::Color from a 6- or 3-digit hexadecimal string representing red, green, and blue.
|
30
33
|
#
|
31
34
|
# The string may include a '#' prefix.
|
32
35
|
def self.hex(hex_string)
|
@@ -37,31 +40,40 @@ class RGB
|
|
37
40
|
end
|
38
41
|
end
|
39
42
|
|
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
|
43
|
+
end
|
46
44
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
else
|
57
|
-
nil
|
45
|
+
module RGB
|
46
|
+
|
47
|
+
class << self
|
48
|
+
|
49
|
+
# Creates a new RGB::Color from a 6- or 3-digit hexadecimal string representing red, green, and blue.
|
50
|
+
#
|
51
|
+
# The string may include a '#' prefix.
|
52
|
+
def hex(hex_string)
|
53
|
+
Color.hex(hex_string)
|
58
54
|
end
|
59
|
-
end
|
60
55
|
|
61
|
-
|
56
|
+
# Returns a 3-digit shorthand version of a 6-digit hexadecimal string.
|
57
|
+
#
|
58
|
+
# If a shorthand version is not possible, returns the original string.
|
59
|
+
def hex_shorthand(hex_string)
|
60
|
+
hex_string.sub( /^(#?)(\h)\2(\h)\3(\h)\4$/, '\1\2\3\4' )
|
61
|
+
end
|
62
|
+
|
63
|
+
# Parses a 6- or 3-digit hexadecimal string.
|
64
|
+
# Returns three integers in the range 0..255, or nil if not successful.
|
65
|
+
#
|
66
|
+
# The string may include a '#' prefix.
|
67
|
+
def hex_to_rgb(hex_string)
|
68
|
+
if hex_string =~ /^(#?)(\h\h)(\h\h)(\h\h)$/
|
69
|
+
[$2, $3, $4].map { |h| h.to_i(16) }
|
70
|
+
elsif hex_string =~ /^(#?)(\h)(\h)(\h)$/
|
71
|
+
[$2, $3, $4].map { |h| (h*2).to_i(16) }
|
72
|
+
else
|
73
|
+
nil
|
74
|
+
end
|
75
|
+
end
|
62
76
|
|
63
|
-
def hex6
|
64
|
-
'%02x%02x%02x' % [ r, g, b ]
|
65
77
|
end
|
66
78
|
|
67
79
|
end
|
data/lib/redgreenblue/hsb.rb
CHANGED
@@ -1,22 +1,14 @@
|
|
1
1
|
require 'redgreenblue/hsv'
|
2
2
|
|
3
|
-
class RGB
|
4
|
-
|
5
|
-
#----------------------------------------------------------------------#
|
6
|
-
# Class Methods #
|
7
|
-
#----------------------------------------------------------------------#
|
3
|
+
class RGB::Color
|
8
4
|
|
9
5
|
class << self
|
10
6
|
|
11
|
-
# Creates a new RGB
|
7
|
+
# Creates a new RGB::Color from HSB values: hue (0..360), saturation (0..1), and brightness (0..1).
|
12
8
|
alias hsb hsv
|
13
9
|
|
14
10
|
end
|
15
11
|
|
16
|
-
#----------------------------------------------------------------------#
|
17
|
-
# Instance Methods #
|
18
|
-
#----------------------------------------------------------------------#
|
19
|
-
|
20
12
|
# Returns color as HSB:
|
21
13
|
# hue (0..360), saturation (0..1), brightness (0..1).
|
22
14
|
# When saturation is 0, hue is nil.
|
@@ -55,7 +47,19 @@ class RGB
|
|
55
47
|
# Sets red, green, and blue by rotating the object's HSB-hue a number of degrees.
|
56
48
|
alias hsb_rotate! hsv_rotate!
|
57
49
|
|
58
|
-
# Creates one or more new RGB objects by rotating this object's HSB-hue a number of degrees.
|
50
|
+
# Creates one or more new RGB::Color objects by rotating this object's HSB-hue a number of degrees.
|
59
51
|
alias hsb_rotate hsv_rotate
|
60
52
|
|
61
53
|
end
|
54
|
+
|
55
|
+
|
56
|
+
module RGB
|
57
|
+
|
58
|
+
class << self
|
59
|
+
|
60
|
+
# Creates a new RGB::Color from HSB values: hue (0..360), saturation (0..1), and brightness (0..1).
|
61
|
+
alias hsb hsv
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|