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.
@@ -1,15 +1,11 @@
1
1
  require 'redgreenblue/hsx_shared'
2
2
  require 'redgreenblue/math'
3
3
 
4
- class RGB
5
-
6
- #----------------------------------------------------------------------#
7
- # Class Methods #
8
- #----------------------------------------------------------------------#
4
+ class RGB::Color
9
5
 
10
6
  class << self
11
7
 
12
- # Creates a new RGB object from HSL values: hue (0..360), saturation (0..1), and lightness (0..1).
8
+ # Creates a new RGB::Color from HSL values: hue (0..360), saturation (0..1), and lightness (0..1).
13
9
  def hsl(*a)
14
10
  new hsl_to_values(*a)
15
11
  end
@@ -23,10 +19,6 @@ class RGB
23
19
 
24
20
  end
25
21
 
26
- #----------------------------------------------------------------------#
27
- # Instance Methods #
28
- #----------------------------------------------------------------------#
29
-
30
22
  # Returns color as HSL:
31
23
  # hue (0..360), saturation (0..1), lightness (0..1).
32
24
  # When saturation is 0, hue is nil.
@@ -51,7 +43,7 @@ class RGB
51
43
 
52
44
  # Sets red, green, and blue using HSL values: hue (0..360), saturation (0..1), and lightness (0..1).
53
45
  def hsl=(*a)
54
- self.values = RGB.hsl_to_values(*a)
46
+ self.values = RGB::Color.hsl_to_values(*a)
55
47
  end
56
48
 
57
49
  # Sets HSL-hue to a number of degrees (0..360) or nil.
@@ -84,7 +76,7 @@ class RGB
84
76
  self
85
77
  end
86
78
 
87
- # Creates one or more new RGB objects by rotating this object's HSL-hue a number of degrees.
79
+ # Creates one or more new RGB::Color objects by rotating this object's HSL-hue a number of degrees.
88
80
  def hsl_rotate(a_degrees, *b_degrees)
89
81
  if a_degrees.class != Array and b_degrees.none?
90
82
  RGB.hsl zip_add(hsl, [a_degrees, 0, 0])
@@ -94,3 +86,17 @@ class RGB
94
86
  end
95
87
 
96
88
  end
89
+
90
+
91
+ module RGB
92
+
93
+ class << self
94
+
95
+ # Creates a new RGB::Color from HSL values: hue (0..360), saturation (0..1), and lightness (0..1).
96
+ def hsl(*a)
97
+ Color.hsl(*a)
98
+ end
99
+
100
+ end
101
+
102
+ end
@@ -1,15 +1,11 @@
1
1
  require 'redgreenblue/hsx_shared'
2
2
  require 'redgreenblue/math'
3
3
 
4
- class RGB
5
-
6
- #----------------------------------------------------------------------#
7
- # Class Methods #
8
- #----------------------------------------------------------------------#
4
+ class RGB::Color
9
5
 
10
6
  class << self
11
7
 
12
- # Creates a new RGB object from HSV values: hue (0..360), saturation (0..1), and value (0..1).
8
+ # Creates a new RGB::Color from HSV values: hue (0..360), saturation (0..1), and value (0..1).
13
9
  def hsv(*a)
14
10
  new hsv_to_values(*a)
15
11
  end
@@ -23,10 +19,6 @@ class RGB
23
19
 
24
20
  end
25
21
 
26
- #----------------------------------------------------------------------#
27
- # Instance Methods #
28
- #----------------------------------------------------------------------#
29
-
30
22
  # Returns color as HSV:
31
23
  # hue (0..360), saturation (0..1), value (0..1).
32
24
  # When saturation is 0, hue is nil.
@@ -51,7 +43,7 @@ class RGB
51
43
 
52
44
  # Sets red, green, and blue using HSV values: hue (0..360), saturation (0..1), and value (0..1).
53
45
  def hsv=(*a)
54
- self.values = RGB.hsv_to_values(*a)
46
+ self.values = RGB::Color.hsv_to_values(*a)
55
47
  end
56
48
 
57
49
  # Sets HSV-hue to a number of degrees (0..360) or nil.
@@ -84,7 +76,7 @@ class RGB
84
76
  self
85
77
  end
86
78
 
87
- # Creates one or more new RGB objects by rotating this object's HSV-hue a number of degrees.
79
+ # Creates one or more new RGB::Color objects by rotating this object's HSV-hue a number of degrees.
88
80
  def hsv_rotate(a_degrees, *b_degrees)
89
81
  if a_degrees.class != Array and b_degrees.none?
90
82
  RGB.hsv zip_add(hsv, [a_degrees, 0, 0])
@@ -94,3 +86,17 @@ class RGB
94
86
  end
95
87
 
96
88
  end
89
+
90
+
91
+ module RGB
92
+
93
+ class << self
94
+
95
+ # Creates a new RGB::Color from HSV values: hue (0..360), saturation (0..1), and value (0..1).
96
+ def hsv(*a)
97
+ Color.hsv(*a)
98
+ end
99
+
100
+ end
101
+
102
+ end
@@ -1,8 +1,4 @@
1
- class RGB
2
-
3
- #----------------------------------------------------------------------#
4
- # Class Methods #
5
- #----------------------------------------------------------------------#
1
+ class RGB::Color
6
2
 
7
3
  class << self
8
4
 
@@ -48,10 +44,6 @@ class RGB
48
44
 
49
45
  end
50
46
 
51
- #----------------------------------------------------------------------#
52
- # Instance Methods #
53
- #----------------------------------------------------------------------#
54
-
55
47
  private
56
48
 
57
49
  # Compute HSL, HSV, and chroma.
@@ -1,4 +1,4 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
3
  # Returns the color's hue (0..360), whiteness (0..1), and blackness (0..1), as defined by the HWB color model.
4
4
  #
@@ -1,4 +1,4 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
3
  private
4
4
 
@@ -32,7 +32,7 @@ class RGB
32
32
  #
33
33
  # You can choose among several inspect styles. See the styles, style, and style= class methods.
34
34
  def inspect
35
- send "_inspect_#{self.class.style}"
35
+ send "_inspect_#{RGB.style}"
36
36
  end
37
37
 
38
38
  # Returns a string representation of the object.
@@ -40,34 +40,43 @@ class RGB
40
40
  _inspect_default
41
41
  end
42
42
 
43
- # Returns the base inspect style, dependent on the COLORTERM environment variable.
44
- def self.base_style
45
- if styles.include? ENV['REDGREENBLUE_STYLE']
46
- ENV['REDGREENBLUE_STYLE']
47
- else
48
- if ENV['COLORTERM'] == 'truecolor'
49
- 'simple'
43
+ end
44
+
45
+
46
+ module RGB
47
+
48
+ class << self
49
+
50
+ # Returns the base inspect style, dependent on the COLORTERM environment variable.
51
+ def base_style
52
+ if styles.include? ENV['REDGREENBLUE_STYLE']
53
+ ENV['REDGREENBLUE_STYLE']
50
54
  else
51
- 'default'
55
+ if ENV['COLORTERM'] == 'truecolor'
56
+ 'simple'
57
+ else
58
+ 'default'
59
+ end
52
60
  end
53
61
  end
54
- end
55
62
 
56
- # Returns the current inspect style.
57
- def self.style
58
- @@style ||= base_style
59
- end
63
+ # Returns the current inspect style.
64
+ def style
65
+ @@style ||= base_style
66
+ end
60
67
 
61
- # Returns a list of all available inspect styles.
62
- def self.styles
63
- ( self.instance_methods + self.private_instance_methods ).grep( /^_inspect_(.*)/ ) { $1 }.sort
64
- end
68
+ # Returns a list of all available inspect styles.
69
+ def styles
70
+ ( Color.instance_methods + Color.private_instance_methods ).grep( /^_inspect_(.*)/ ) { $1 }.sort
71
+ end
72
+
73
+ # Selects an inspect style.
74
+ #
75
+ # Only the first few characters of your preferred style are required.
76
+ def style=(s)
77
+ @@style = styles.grep( /^#{s.to_s.downcase}/ ).first || style
78
+ end
65
79
 
66
- # Selects an inspect style.
67
- #
68
- # Only the first few characters of your preferred style are required.
69
- def self.style=(s)
70
- @@style = styles.grep( /^#{s.to_s.downcase}/ ).first || style
71
80
  end
72
81
 
73
82
  end
@@ -1,11 +1,11 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
3
  # Returns the color as a 24-bit integer in the range 0..16777215.
4
4
  def to_i
5
5
  ( r << 16 ) + ( g << 8 ) + b
6
6
  end
7
7
 
8
- # Creates a new RGB object from a 24-bit integer in the range 0..16777215.
8
+ # Creates a new RGB::Color from a 24-bit integer in the range 0..16777215.
9
9
  def self.at(number)
10
10
  n = number.to_i
11
11
  if (0..16777215) === n
@@ -20,3 +20,17 @@ class RGB
20
20
  end
21
21
 
22
22
  end
23
+
24
+
25
+ module RGB
26
+
27
+ class << self
28
+
29
+ # Creates a new RGB::Color from a 24-bit integer in the range 0..16777215.
30
+ def at(number)
31
+ Color.at(number)
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -1,22 +1,19 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
- #----------------------------------------------------------------------#
4
- # Class Methods #
5
- #----------------------------------------------------------------------#
6
3
 
7
4
  class << self
8
5
 
9
- # Creates a white RGB object.
6
+ # Creates a white RGB::Color.
10
7
  def white
11
8
  new(1,1,1)
12
9
  end
13
10
 
14
- # Creates a black RGB object.
11
+ # Creates a black RGB::Color.
15
12
  def black
16
13
  new(0,0,0)
17
14
  end
18
15
 
19
- # Creates a grey RGB object. Defaults to lightness 0.5, a middle grey. Black equals grey(0), white equals grey(1).
16
+ # Creates a grey RGB::Color. Defaults to lightness 0.5, a middle grey. Black equals grey(0), white equals grey(1).
20
17
  #
21
18
  # ::gray is an alias for ::grey.
22
19
  def grey(lightness=0.5)
@@ -26,32 +23,32 @@ class RGB
26
23
  # Alias gray for grey.
27
24
  alias gray grey
28
25
 
29
- # Creates a pure red RGB object.
26
+ # Creates a pure red RGB::Color.
30
27
  def red
31
28
  new(1,0,0)
32
29
  end
33
30
 
34
- # Creates a pure green RGB object.
31
+ # Creates a pure green RGB::Color.
35
32
  def green
36
33
  new(0,1,0)
37
34
  end
38
35
 
39
- # Creates a pure blue RGB object.
36
+ # Creates a pure blue RGB::Color.
40
37
  def blue
41
38
  new(0,0,1)
42
39
  end
43
40
 
44
- # Creates a yellow RGB object.
41
+ # Creates a yellow RGB::Color.
45
42
  def yellow
46
43
  new(1,1,0)
47
44
  end
48
45
 
49
- # Creates a cyan RGB object.
46
+ # Creates a cyan RGB::Color.
50
47
  def cyan
51
48
  new(0,1,1)
52
49
  end
53
50
 
54
- # Creates a magenta RGB object.
51
+ # Creates a magenta RGB::Color.
55
52
  def magenta
56
53
  new(1,0,1)
57
54
  end
@@ -71,3 +68,74 @@ class RGB
71
68
  end
72
69
 
73
70
  end
71
+
72
+
73
+ module RGB
74
+
75
+ class << self
76
+
77
+ # Creates a white RGB::Color.
78
+ def white
79
+ Color.white
80
+ end
81
+
82
+ # Creates a black RGB::Color.
83
+ def black
84
+ Color.black
85
+ end
86
+
87
+ # Creates a grey RGB::Color. Defaults to lightness 0.5, a middle grey. Black equals grey(0), white equals grey(1).
88
+ #
89
+ # ::gray is an alias for ::grey.
90
+ def grey(lightness=0.5)
91
+ Color.grey(lightness)
92
+ end
93
+
94
+ # Alias gray for grey.
95
+ alias gray grey
96
+
97
+ # Creates a pure red RGB::Color.
98
+ def red
99
+ Color.red
100
+ end
101
+
102
+ # Creates a pure green RGB::Color.
103
+ def green
104
+ Color.green
105
+ end
106
+
107
+ # Creates a pure blue RGB::Color.
108
+ def blue
109
+ Color.blue
110
+ end
111
+
112
+ # Creates a yellow RGB::Color.
113
+ def yellow
114
+ Color.yellow
115
+ end
116
+
117
+ # Creates a cyan RGB::Color.
118
+ def cyan
119
+ Color.cyan
120
+ end
121
+
122
+ # Creates a magenta RGB::Color.
123
+ def magenta
124
+ Color.magenta
125
+ end
126
+
127
+ # Returns the 8 corners of the RGB cube.
128
+ def corners
129
+ [ black, red, yellow, green, cyan, blue, magenta, white ]
130
+ end
131
+
132
+ # Returns the centre of the RGB cube.
133
+ def centre
134
+ grey
135
+ end
136
+
137
+ alias center centre
138
+
139
+ end
140
+
141
+ end
@@ -1,4 +1,4 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
3
  # Returns the color in the format used by AppleScript (a 48-bit RGB triplet).
4
4
  def applescript
@@ -1,4 +1,4 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
3
  # Matches this color to a set of colors by calculating their euclidean distance.
4
4
  #
@@ -18,4 +18,32 @@ class RGB
18
18
  others.map { |c| [ c, de76(c) ] }.sort_by { |a| a[1] }
19
19
  end
20
20
 
21
+ # Matches this (reference) color to a set of colors using the CIE 1994 delta E formula.
22
+ #
23
+ # Returns the given set of colors with their difference from this color, sorted by difference (nearest color first).
24
+ #
25
+ # By default uses parameters for use in graphic arts, and reference conditions.
26
+ # Parameters (k1, k2, kl, kc, kh) can be individually overriden for different applications and for variations in conditions.
27
+ def match_de94(others, k1: 0.045, k2: 0.015, kl: 1, kc: 1, kh: 1)
28
+ others.map { |c| [ c, de94(c, k1: k1, k2: k2, kl: kl, kc: kc, kh: kh) ] }.sort_by { |a| a[1] }
29
+ end
30
+
31
+ # Matches this (reference) color to a set of colors using the CIE 1994 delta E formula.
32
+ #
33
+ # For use in graphic arts, under reference conditions.
34
+ #
35
+ # Returns the given set of colors with their difference from this color, sorted by difference (nearest color first).
36
+ def match_de94g(others)
37
+ others.map { |c| [ c, de94g(c) ] }.sort_by { |a| a[1] }
38
+ end
39
+
40
+ # Matches this (reference) color to a set of colors using the CIE 1994 delta E formula.
41
+ #
42
+ # For use with textiles, under reference conditions.
43
+ #
44
+ # Returns the given set of colors with their difference from this color, sorted by difference (nearest color first).
45
+ def match_de94t(others)
46
+ others.map { |c| [ c, de94t(c) ] }.sort_by { |a| a[1] }
47
+ end
48
+
21
49
  end
@@ -1,4 +1,4 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
3
  private
4
4
 
@@ -1,4 +1,4 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
3
  # Inverts the object's color.
4
4
  def invert!
@@ -6,7 +6,7 @@ class RGB
6
6
  self
7
7
  end
8
8
 
9
- # Creates a new RGB object with the inverted color of this RGB object.
9
+ # Creates a new RGB::Color with the inverted color of this RGB object.
10
10
  def invert
11
11
  dup.invert!
12
12
  end
@@ -17,23 +17,23 @@ class RGB
17
17
  values.min == values.max
18
18
  end
19
19
 
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.
20
+ # Returns an array of RGB::Colors for all possible ways in which the red, green, and blue values of this object can be exchanged.
21
21
  #
22
22
  # Example: RGB.red.permutation returns [ RGB.red, RGB.green, RGB.blue ].
23
23
  # See also: shuffle.
24
24
  def permutation
25
- values.permutation.to_a.uniq.map { |v| RGB.new v }
25
+ values.permutation.to_a.uniq.map { |v| RGB::Color.new v }
26
26
  end
27
27
 
28
- # Returns an array of three RGB objects, for the red, green, and blue components of this object.
28
+ # Returns an array of three RGB::Colors, for the red, green, and blue components of this object.
29
29
  def components
30
- [ RGB.new(red,0,0), RGB.new(0,green,0), RGB.new(0,0,blue) ]
30
+ [ RGB::Color.new(red,0,0), RGB::Color.new(0,green,0), RGB::Color.new(0,0,blue) ]
31
31
  end
32
32
 
33
- # Creates a new RGB object from three RGB objects representing the red, green, and blue components.
33
+ # Creates a new RGB::Color from three RGB::Colors representing the red, green, and blue components.
34
34
  def self.assemble(*a)
35
35
  v = a.flatten
36
- RGB.new(v[0].red, v[1].green, v[2].blue)
36
+ RGB::Color.new(v[0].red, v[1].green, v[2].blue)
37
37
  end
38
38
 
39
39
  # Returns the euclidean distance between this color and another color.
@@ -50,3 +50,17 @@ class RGB
50
50
  end
51
51
 
52
52
  end
53
+
54
+
55
+ module RGB
56
+
57
+ class << self
58
+
59
+ # Creates a new RGB::Color from three RGB::Colors representing the red, green, and blue components.
60
+ def assemble(*a)
61
+ Color.assemble(*a)
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -1,14 +1,14 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
- # Changes the object's color by mixing it with a portion of another RGB color.
3
+ # Changes the object's color by mixing it with a portion of another RGB::Color.
4
4
  def mix!(another,portion=0.5)
5
5
  self.values = mix_values(another.values, portion)
6
6
  self
7
7
  end
8
8
 
9
- # Creates a new RGB object by mixing this object's color with a portion of another RGB color.
9
+ # Creates a new RGB::Color by mixing this object's color with a portion of another RGB::Color.
10
10
  def mix(another,portion=0.5)
11
- RGB.new mix_values(another.values, portion)
11
+ RGB::Color.new mix_values(another.values, portion)
12
12
  end
13
13
 
14
14
  # Changes the object's color by mixing it with a portion of white.
@@ -16,7 +16,7 @@ class RGB
16
16
  mix!(RGB.white, portion)
17
17
  end
18
18
 
19
- # Creates one or more new RGB objects by mixing this object's color with a portion of white.
19
+ # Creates one or more new RGB::Colors by mixing this object's color with a portion of white.
20
20
  def whiten(portion=0.5, *portions)
21
21
  if (portion.class != Array) and portions.none?
22
22
  mix(RGB.white, portion)
@@ -30,7 +30,7 @@ class RGB
30
30
  mix!(RGB.black, portion)
31
31
  end
32
32
 
33
- # Creates one or more new RGB objects by mixing this object's color with a portion of black.
33
+ # Creates one or more new RGB::Colors by mixing this object's color with a portion of black.
34
34
  def blacken(portion=0.5, *portions)
35
35
  if (portion.class != Array) and portions.none?
36
36
  mix(RGB.black, portion)
@@ -1,4 +1,4 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
3
  # Returns the name.
4
4
  def name
@@ -2,15 +2,15 @@
2
2
  #
3
3
  # Conforms to Bridge API 1.35 for Philips Hue, published 20-Nov-2019.
4
4
 
5
- # Automatically load core RGB class before loading options.
5
+ # Automatically load core RGB module before loading options.
6
6
  require 'redgreenblue'
7
7
 
8
- class RGB
8
+ class RGB::Color
9
9
 
10
10
  # Only available when optional support for Philips Hue lights is loaded.
11
11
  #
12
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.
13
+ # to set a light to this RGB::Color's hue, saturation and brightness.
14
14
  #
15
15
  # Formatted as JSON, this hash can be sent to a bridge to set a light's state.
16
16
  #
@@ -22,6 +22,8 @@ class RGB
22
22
  # @example Use
23
23
  # RGB.magenta.to_philips_hue_api_hsb_arguments
24
24
  # => {"on"=>true, "bri"=>254, "hue"=>54613, "sat"=>254}
25
+ # RGB.black.to_philips_hue_api_hsb_arguments
26
+ # => {"on"=>false}
25
27
  # @return [Hash] API arguments
26
28
  def to_philips_hue_api_hsb_arguments(black_off=true)
27
29
  my_hsb = hsb
@@ -1,20 +1,20 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
- # On Mac OS, shows the color picker to choose a color for the RGB object.
3
+ # On Mac OS, shows the color picker to choose a color for this object.
4
4
  # Not available on other platforms.
5
5
  def pick
6
- result = RGB.mac_choose(self)
6
+ result = RGB::Color.mac_choose(self)
7
7
  if result
8
8
  self.rrggbb = result
9
9
  end
10
10
  end
11
11
 
12
- # On Mac OS, shows the color picker and creates an RGB object with the chosen color.
12
+ # On Mac OS, shows the color picker and creates an RGB::Color with the chosen color.
13
13
  # Not available on other platforms.
14
14
  #
15
15
  # If no default color is specified, the picker defaults to a middle grey.
16
- def self.pick(default_color=RGB.new)
17
- result = RGB.mac_choose(default_color)
16
+ def self.pick(default_color=RGB::Color.new)
17
+ result = RGB::Color.mac_choose(default_color)
18
18
  if result
19
19
  RGB.rrggbb result
20
20
  else
@@ -62,3 +62,20 @@ class RGB
62
62
  end
63
63
 
64
64
  end
65
+
66
+
67
+ module RGB
68
+
69
+ class << self
70
+
71
+ # On Mac OS, shows the color picker and creates an RGB::Color with the chosen color.
72
+ # Not available on other platforms.
73
+ #
74
+ # If no default color is specified, the picker defaults to a middle grey.
75
+ def pick(default_color=new)
76
+ Color.pick(default_color)
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -1,6 +1,6 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
- # Returns a new RGB object with this color's Ostwald full-color,
3
+ # Returns a new RGB::Color with this color's Ostwald full-color,
4
4
  # or nil for achromatic colors (white, greys, and black).
5
5
  #
6
6
  # The resulting color contains no white or black,
@@ -19,7 +19,7 @@ class RGB
19
19
  if color_portion == 0
20
20
  nil
21
21
  else
22
- RGB.new( values.map { |v| ( ( v - white_portion ) / color_portion ).round(6) } )
22
+ RGB::Color.new( values.map { |v| ( ( v - white_portion ) / color_portion ).round(6) } )
23
23
  end
24
24
  end
25
25