redgreenblue 0.10.0 → 0.11.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.rb +15 -22
- data/lib/redgreenblue/24bit.rb +11 -0
- data/lib/redgreenblue/gpl.rb +30 -0
- data/lib/redgreenblue/hsb.rb +22 -0
- data/lib/redgreenblue/hsv.rb +0 -22
- data/lib/redgreenblue/inspect.rb +9 -1
- data/lib/redgreenblue/misc.rb +6 -0
- data/lib/redgreenblue/mix.rb +14 -6
- data/lib/redgreenblue/opt/philipshue.rb +15 -5
- data/lib/redgreenblue/ostwald.rb +45 -0
- data/lib/redgreenblue/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc4e866d75637857fb5a4b27b77829e20f488e321fa287b73a5d6d9c21d194a9
|
4
|
+
data.tar.gz: e3f9e875b0a8893bd21a6cd503702881833f54873557f84c4cf4776338294668
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94b96125e65443d7ceb66cfabf9ba6b0681bfc51046b8d308e86d0904a58a7f745f6ea159a040ee79dcb9713ace9adbc416a5705c4b2ebb85333bf09f0dd9a43
|
7
|
+
data.tar.gz: f3e0bd4c0cdcd432dd50fc518f1d2062c66ac907f55b3235405589b2d797791c48b3d62284ae665a8fc4d60c4209ce0b3d46d07a459e15d2fa261d9b1aa8df1d
|
data/lib/redgreenblue.rb
CHANGED
@@ -1,33 +1,26 @@
|
|
1
1
|
class RGB
|
2
2
|
end
|
3
3
|
|
4
|
-
|
4
|
+
%w(
|
5
5
|
|
6
|
-
|
6
|
+
version
|
7
7
|
|
8
|
-
|
9
|
-
require 'redgreenblue/48bit'
|
10
|
-
require 'redgreenblue/hex'
|
11
|
-
require 'redgreenblue/int'
|
8
|
+
base
|
12
9
|
|
13
|
-
|
14
|
-
require 'redgreenblue/hsv'
|
15
|
-
require 'redgreenblue/hsb'
|
10
|
+
24bit 48bit hex int
|
16
11
|
|
17
|
-
|
18
|
-
require 'redgreenblue/cie'
|
12
|
+
hsl hsv hsb
|
19
13
|
|
20
|
-
|
21
|
-
require 'redgreenblue/lazy'
|
14
|
+
ostwald
|
22
15
|
|
23
|
-
|
24
|
-
require 'redgreenblue/bgr24bit'
|
25
|
-
require 'redgreenblue/gif'
|
26
|
-
require 'redgreenblue/terminal'
|
27
|
-
require 'redgreenblue/web'
|
16
|
+
gamma cie
|
28
17
|
|
29
|
-
|
30
|
-
require 'redgreenblue/misc'
|
31
|
-
require 'redgreenblue/random'
|
18
|
+
inspect lazy
|
32
19
|
|
33
|
-
|
20
|
+
rgb565 bgr24bit gif terminal web gpl
|
21
|
+
|
22
|
+
mix misc random
|
23
|
+
|
24
|
+
os
|
25
|
+
|
26
|
+
).each { |m| require "redgreenblue/#{m}" }
|
data/lib/redgreenblue/24bit.rb
CHANGED
@@ -51,6 +51,17 @@ class RGB
|
|
51
51
|
c
|
52
52
|
end
|
53
53
|
|
54
|
+
# Sets the red, green, and blue values to those of the nearest 24-bit color.
|
55
|
+
def snap!
|
56
|
+
self.rgb = rgb
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
# Creates a new RGB object containing the nearest 24-bit color.
|
61
|
+
def snap
|
62
|
+
RGB.rgb rgb
|
63
|
+
end
|
64
|
+
|
54
65
|
# Calls the given block for each 24-bit RGB color (from black to white), passing the color as an RGB object.
|
55
66
|
#
|
56
67
|
# Returns the number of iterations.
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class RGB
|
2
|
+
|
3
|
+
########################################################################
|
4
|
+
# Class methods #
|
5
|
+
########################################################################
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
# Reverse-engineered from:
|
12
|
+
# - https://github.com/GNOME/gimp/blob/5d79fba8238a27b8691556489898d33b3fa0dda0/app/core/gimppalette-load.c
|
13
|
+
def gpl_header(name, columns=nil)
|
14
|
+
"GIMP Palette\n" +
|
15
|
+
"Name: #{name}\n" +
|
16
|
+
( columns ? "Columns: #{columns}\n" : '' )
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
########################################################################
|
22
|
+
# Instance methods #
|
23
|
+
########################################################################
|
24
|
+
|
25
|
+
# Returns the color in the format used in .gpl files (Gimp color palettes). A name for the color is optional.
|
26
|
+
def gpl(name=nil)
|
27
|
+
( "%3d %3d %3d" % rgb ) + ( name ? "\t#{name}" : '' )
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/lib/redgreenblue/hsb.rb
CHANGED
@@ -8,6 +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
12
|
alias hsb hsv
|
12
13
|
|
13
14
|
end
|
@@ -16,24 +17,45 @@ class RGB
|
|
16
17
|
# Instance methods #
|
17
18
|
########################################################################
|
18
19
|
|
20
|
+
# Returns color as HSB:
|
21
|
+
# hue (0..360), saturation (0..1), brightness (0..1).
|
22
|
+
# When saturation is 0, hue is nil.
|
19
23
|
alias hsb hsv
|
20
24
|
|
25
|
+
# Returns the object's HSB-hue (0..360).
|
21
26
|
alias hsb_h hsv_h
|
22
27
|
|
28
|
+
# Returns the object's HSB-saturation (0..1).
|
23
29
|
alias hsb_s hsv_s
|
24
30
|
|
31
|
+
# Returns the object's HSB-brightness (0..1).
|
25
32
|
alias hsb_b hsv_v
|
26
33
|
|
34
|
+
# Sets red, green, and blue using HSB values: hue (0..360), saturation (0..1), and brightness (0..1).
|
27
35
|
alias hsb= hsv=
|
28
36
|
|
37
|
+
# Sets HSB-hue to a number of degrees (0..360) or nil.
|
38
|
+
#
|
39
|
+
# Adjusts red, green, and blue, leaving HSB-saturation and -brightness unchanged.
|
40
|
+
# When hue is nil, saturation will be 0.
|
29
41
|
alias hsb_h= hsv_h=
|
30
42
|
|
43
|
+
# Sets HSB-saturation to a number between 0 and 1.
|
44
|
+
#
|
45
|
+
# Adjusts red, green, and blue, leaving HSB-hue and -brightness unchanged.
|
46
|
+
# When saturation is 0, hue will be nil.
|
31
47
|
alias hsb_s= hsv_s=
|
32
48
|
|
49
|
+
# Sets HSB-brightness to a number between 0 and 1.
|
50
|
+
#
|
51
|
+
# Adjusts red, green, and blue, leaving HSB-hue and -saturation unchanged.
|
52
|
+
# When brightness is 0, hue will be nil, and saturation will be 0.
|
33
53
|
alias hsb_b= hsv_v=
|
34
54
|
|
55
|
+
# Sets red, green, and blue by rotating the object's HSB-hue a number of degrees.
|
35
56
|
alias hsb_rotate! hsv_rotate!
|
36
57
|
|
58
|
+
# Creates one or more new RGB objects by rotating this object's HSB-hue a number of degrees.
|
37
59
|
alias hsb_rotate hsv_rotate
|
38
60
|
|
39
61
|
end
|
data/lib/redgreenblue/hsv.rb
CHANGED
@@ -10,8 +10,6 @@ class RGB
|
|
10
10
|
class << self
|
11
11
|
|
12
12
|
# Creates a new RGB object from HSV values: hue (0..360), saturation (0..1), and value (0..1).
|
13
|
-
#
|
14
|
-
# ::hsb is an alias for ::hsv.
|
15
13
|
def hsv(*a)
|
16
14
|
new hsv_to_values(*a)
|
17
15
|
end
|
@@ -32,36 +30,26 @@ class RGB
|
|
32
30
|
# Returns color as HSV:
|
33
31
|
# hue (0..360), saturation (0..1), value (0..1).
|
34
32
|
# When saturation is 0, hue is nil.
|
35
|
-
#
|
36
|
-
# #hsb is an alias for #hsv.
|
37
33
|
def hsv
|
38
34
|
hsl_hsv_c[1]
|
39
35
|
end
|
40
36
|
|
41
37
|
# Returns the object's HSV-hue (0..360).
|
42
|
-
#
|
43
|
-
# #hsb_h is an alias for #hsv_h.
|
44
38
|
def hsv_h
|
45
39
|
hsv[0]
|
46
40
|
end
|
47
41
|
|
48
42
|
# Returns the object's HSV-saturation (0..1).
|
49
|
-
#
|
50
|
-
# #hsb_s is an alias for #hsv_s.
|
51
43
|
def hsv_s
|
52
44
|
hsv[1]
|
53
45
|
end
|
54
46
|
|
55
47
|
# Returns the object's HSV-value (0..1).
|
56
|
-
#
|
57
|
-
# #hsb_b is an alias for #hsv_v.
|
58
48
|
def hsv_v
|
59
49
|
hsv[2]
|
60
50
|
end
|
61
51
|
|
62
52
|
# Sets red, green, and blue using HSV values: hue (0..360), saturation (0..1), and value (0..1).
|
63
|
-
#
|
64
|
-
# #hsb= is an alias for #hsv=.
|
65
53
|
def hsv=(*a)
|
66
54
|
self.values = RGB.hsv_to_values(*a)
|
67
55
|
end
|
@@ -70,8 +58,6 @@ class RGB
|
|
70
58
|
#
|
71
59
|
# Adjusts red, green, and blue, leaving HSV-saturation and -value unchanged.
|
72
60
|
# When hue is nil, saturation will be 0.
|
73
|
-
#
|
74
|
-
# #hsb_h= is an alias for #hsv_h=.
|
75
61
|
def hsv_h=(degrees)
|
76
62
|
self.hsv = hsv.fill(degrees,0,1)
|
77
63
|
end
|
@@ -80,8 +66,6 @@ class RGB
|
|
80
66
|
#
|
81
67
|
# Adjusts red, green, and blue, leaving HSV-hue and -value unchanged.
|
82
68
|
# When saturation is 0, hue will be nil.
|
83
|
-
#
|
84
|
-
# #hsb_s= is an alias for #hsv_s=.
|
85
69
|
def hsv_s=(value)
|
86
70
|
self.hsv = hsv.fill(value ,1,1)
|
87
71
|
end
|
@@ -90,23 +74,17 @@ class RGB
|
|
90
74
|
#
|
91
75
|
# Adjusts red, green, and blue, leaving HSV-hue and -saturation unchanged.
|
92
76
|
# When value is 0, hue will be nil, and saturation will be 0.
|
93
|
-
#
|
94
|
-
# #hsb_b= is an alias for #hsv_v=.
|
95
77
|
def hsv_v=(value)
|
96
78
|
self.hsv = hsv.fill(value ,2,1)
|
97
79
|
end
|
98
80
|
|
99
81
|
# Sets red, green, and blue by rotating the object's HSV-hue a number of degrees.
|
100
|
-
#
|
101
|
-
# #hsb_rotate! is an alias for #hsv_rotate!.
|
102
82
|
def hsv_rotate!(degrees)
|
103
83
|
self.hsv = zip_add(hsv, [degrees, 0, 0])
|
104
84
|
self
|
105
85
|
end
|
106
86
|
|
107
87
|
# Creates one or more new RGB objects by rotating this object's HSV-hue a number of degrees.
|
108
|
-
#
|
109
|
-
# #hsb_rotate is an alias for #hsv_rotate.
|
110
88
|
def hsv_rotate(a_degrees, *b_degrees)
|
111
89
|
if a_degrees.class != Array and b_degrees.none?
|
112
90
|
RGB.hsv zip_add(hsv, [a_degrees, 0, 0])
|
data/lib/redgreenblue/inspect.rb
CHANGED
@@ -38,7 +38,15 @@ class RGB
|
|
38
38
|
|
39
39
|
# Returns the base inspect style, dependent on the COLORTERM environment variable.
|
40
40
|
def self.base_style
|
41
|
-
ENV['
|
41
|
+
if styles.include? ENV['REDGREENBLUE_STYLE']
|
42
|
+
ENV['REDGREENBLUE_STYLE']
|
43
|
+
else
|
44
|
+
if ENV['COLORTERM'] == 'truecolor'
|
45
|
+
'simple'
|
46
|
+
else
|
47
|
+
'default'
|
48
|
+
end
|
49
|
+
end
|
42
50
|
end
|
43
51
|
|
44
52
|
# Returns the current inspect style.
|
data/lib/redgreenblue/misc.rb
CHANGED
@@ -11,6 +11,12 @@ class RGB
|
|
11
11
|
dup.invert!
|
12
12
|
end
|
13
13
|
|
14
|
+
# Returns true when this is an achromatic color: red, green, and blue have equal values.
|
15
|
+
# Otherwise false.
|
16
|
+
def achromatic?
|
17
|
+
values.min == values.max
|
18
|
+
end
|
19
|
+
|
14
20
|
# Returns an array of RGB objects for all possible ways in which the red, green, and blue values of this object can be exchanged.
|
15
21
|
#
|
16
22
|
# Example: RGB.red.permutation returns [ RGB.red, RGB.green, RGB.blue ].
|
data/lib/redgreenblue/mix.rb
CHANGED
@@ -16,9 +16,13 @@ class RGB
|
|
16
16
|
mix!(RGB.white, portion)
|
17
17
|
end
|
18
18
|
|
19
|
-
# Creates
|
20
|
-
def whiten(portion=0.5)
|
21
|
-
|
19
|
+
# Creates one or more new RGB objects by mixing this object's color with a portion of white.
|
20
|
+
def whiten(portion=0.5, *portions)
|
21
|
+
if (portion.class != Array) and portions.none?
|
22
|
+
mix(RGB.white, portion)
|
23
|
+
else
|
24
|
+
( [portion].flatten + portions ).map { |p| mix(RGB.white, p) }
|
25
|
+
end
|
22
26
|
end
|
23
27
|
|
24
28
|
# Changes the object's color by mixing it with a portion of black.
|
@@ -26,9 +30,13 @@ class RGB
|
|
26
30
|
mix!(RGB.black, portion)
|
27
31
|
end
|
28
32
|
|
29
|
-
# Creates
|
30
|
-
def blacken(portion=0.5)
|
31
|
-
|
33
|
+
# Creates one or more new RGB objects by mixing this object's color with a portion of black.
|
34
|
+
def blacken(portion=0.5, *portions)
|
35
|
+
if (portion.class != Array) and portions.none?
|
36
|
+
mix(RGB.black, portion)
|
37
|
+
else
|
38
|
+
( [portion].flatten + portions ).map { |p| mix(RGB.black, p) }
|
39
|
+
end
|
32
40
|
end
|
33
41
|
|
34
42
|
# Returns a set of colors between this color and another. That other color is included.
|
@@ -1,18 +1,28 @@
|
|
1
1
|
# Optional support for Philips Hue lights.
|
2
2
|
#
|
3
3
|
# Conforms to Bridge API 1.35 for Philips Hue, published 20-Nov-2019.
|
4
|
-
# See (regrettably requires registration):
|
5
|
-
# - https://developers.meethue.com/develop/hue-api/
|
6
4
|
|
7
5
|
# Automatically load core RGB class before loading options.
|
8
6
|
require 'redgreenblue'
|
9
7
|
|
10
8
|
class RGB
|
11
9
|
|
12
|
-
#
|
10
|
+
# Only available when optional support for Philips Hue lights is loaded.
|
13
11
|
#
|
14
|
-
#
|
15
|
-
#
|
12
|
+
# Returns a hash with the arguments required by the Philips Hue API,
|
13
|
+
# to set a light to this RGB object's hue, saturation and brightness.
|
14
|
+
#
|
15
|
+
# Formatted as JSON, this hash can be sent to a bridge to set a light's state.
|
16
|
+
#
|
17
|
+
# See (regrettably requires registration):
|
18
|
+
# - https://developers.meethue.com/develop/hue-api/
|
19
|
+
#
|
20
|
+
# @example Load
|
21
|
+
# require 'redgreenblue/opt/philipshue'
|
22
|
+
# @example Use
|
23
|
+
# RGB.magenta.to_philips_hue_api_hsb_arguments
|
24
|
+
# => {"on"=>true, "bri"=>254, "hue"=>54613, "sat"=>254}
|
25
|
+
# @return [Hash] API arguments
|
16
26
|
def to_philips_hue_api_hsb_arguments(black_off=true)
|
17
27
|
my_hsb = hsb
|
18
28
|
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class RGB
|
2
|
+
|
3
|
+
# Returns a new RGB object with this color's Ostwald full-color,
|
4
|
+
# or nil for achromatic colors (white, greys, and black).
|
5
|
+
#
|
6
|
+
# The resulting color contains no white or black,
|
7
|
+
# i.e. it has at least one RGB component set to 0, and at least one set to maximum.
|
8
|
+
#
|
9
|
+
# This is identical (barring very small rounding errors)
|
10
|
+
# to setting HSL-saturation to 1, and -lightness to 0.5
|
11
|
+
#
|
12
|
+
# Based on:
|
13
|
+
# - Color for the Sciences, pp. 575–591
|
14
|
+
# - https://lirias.kuleuven.be/retrieve/306124 (PDF)
|
15
|
+
def ostwald_color
|
16
|
+
white_portion = values.min
|
17
|
+
color_portion = values.max - white_portion
|
18
|
+
|
19
|
+
if color_portion == 0
|
20
|
+
nil
|
21
|
+
else
|
22
|
+
RGB.new( values.map { |v| ( ( v - white_portion ) / color_portion ).round(6) } )
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns the portions of Ostwald full-color, white, and black, which constitute this color.
|
27
|
+
#
|
28
|
+
# The sum of these three numbers equals 1.
|
29
|
+
#
|
30
|
+
# #cwk is an alias for #ostwald_cwk.
|
31
|
+
#
|
32
|
+
# Based on:
|
33
|
+
# - Color for the Sciences, pp. 575–591
|
34
|
+
# - https://lirias.kuleuven.be/retrieve/306124 (PDF)
|
35
|
+
def ostwald_cwk
|
36
|
+
[
|
37
|
+
color_portion = values.max - values.min,
|
38
|
+
white_portion = values.min,
|
39
|
+
1 - color_portion - white_portion
|
40
|
+
]
|
41
|
+
end
|
42
|
+
|
43
|
+
alias cwk ostwald_cwk
|
44
|
+
|
45
|
+
end
|
data/lib/redgreenblue/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redgreenblue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lllist.eu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-06-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -24,6 +24,7 @@ files:
|
|
24
24
|
- lib/redgreenblue/cie.rb
|
25
25
|
- lib/redgreenblue/gamma.rb
|
26
26
|
- lib/redgreenblue/gif.rb
|
27
|
+
- lib/redgreenblue/gpl.rb
|
27
28
|
- lib/redgreenblue/hex.rb
|
28
29
|
- lib/redgreenblue/hsb.rb
|
29
30
|
- lib/redgreenblue/hsl.rb
|
@@ -38,6 +39,7 @@ files:
|
|
38
39
|
- lib/redgreenblue/opt/philipshue.rb
|
39
40
|
- lib/redgreenblue/os.rb
|
40
41
|
- lib/redgreenblue/os/mac.rb
|
42
|
+
- lib/redgreenblue/ostwald.rb
|
41
43
|
- lib/redgreenblue/random.rb
|
42
44
|
- lib/redgreenblue/rgb565.rb
|
43
45
|
- lib/redgreenblue/terminal.rb
|
@@ -65,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
67
|
- !ruby/object:Gem::Version
|
66
68
|
version: '0'
|
67
69
|
requirements: []
|
68
|
-
rubygems_version: 3.
|
70
|
+
rubygems_version: 3.1.2
|
69
71
|
signing_key:
|
70
72
|
specification_version: 4
|
71
73
|
summary: A simple Ruby library for handling RGB colors.
|