redgreenblue 0.15.0 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,46 @@ 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 Methods #
47
+ #----------------------------------------------------------------------#
48
+
49
+ module RGB
50
+
51
+ class << self
52
+
53
+ # Returns the base inspect style, dependent on the COLORTERM environment variable.
54
+ def base_style
55
+ if styles.include? ENV['REDGREENBLUE_STYLE']
56
+ ENV['REDGREENBLUE_STYLE']
50
57
  else
51
- 'default'
58
+ if ENV['COLORTERM'] == 'truecolor'
59
+ 'simple'
60
+ else
61
+ 'default'
62
+ end
52
63
  end
53
64
  end
54
- end
55
65
 
56
- # Returns the current inspect style.
57
- def self.style
58
- @@style ||= base_style
59
- end
66
+ # Returns the current inspect style.
67
+ def style
68
+ @@style ||= base_style
69
+ end
60
70
 
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
71
+ # Returns a list of all available inspect styles.
72
+ def styles
73
+ ( Color.instance_methods + Color.private_instance_methods ).grep( /^_inspect_(.*)/ ) { $1 }.sort
74
+ end
75
+
76
+ # Selects an inspect style.
77
+ #
78
+ # Only the first few characters of your preferred style are required.
79
+ def style=(s)
80
+ @@style = styles.grep( /^#{s.to_s.downcase}/ ).first || style
81
+ end
65
82
 
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
83
  end
72
84
 
73
85
  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 object 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,20 @@ class RGB
20
20
  end
21
21
 
22
22
  end
23
+
24
+ #----------------------------------------------------------------------#
25
+ # Module Methods #
26
+ #----------------------------------------------------------------------#
27
+
28
+ module RGB
29
+
30
+ class << self
31
+
32
+ # Creates a new RGB::Color object from a 24-bit integer in the range 0..16777215.
33
+ def at(number)
34
+ Color.at(number)
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -1,4 +1,4 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
3
  #----------------------------------------------------------------------#
4
4
  # Class Methods #
@@ -6,17 +6,17 @@ class RGB
6
6
 
7
7
  class << self
8
8
 
9
- # Creates a white RGB object.
9
+ # Creates a white RGB::Color object.
10
10
  def white
11
11
  new(1,1,1)
12
12
  end
13
13
 
14
- # Creates a black RGB object.
14
+ # Creates a black RGB::Color object.
15
15
  def black
16
16
  new(0,0,0)
17
17
  end
18
18
 
19
- # Creates a grey RGB object. Defaults to lightness 0.5, a middle grey. Black equals grey(0), white equals grey(1).
19
+ # Creates a grey RGB::Color object. Defaults to lightness 0.5, a middle grey. Black equals grey(0), white equals grey(1).
20
20
  #
21
21
  # ::gray is an alias for ::grey.
22
22
  def grey(lightness=0.5)
@@ -26,32 +26,32 @@ class RGB
26
26
  # Alias gray for grey.
27
27
  alias gray grey
28
28
 
29
- # Creates a pure red RGB object.
29
+ # Creates a pure red RGB::Color object.
30
30
  def red
31
31
  new(1,0,0)
32
32
  end
33
33
 
34
- # Creates a pure green RGB object.
34
+ # Creates a pure green RGB::Color object.
35
35
  def green
36
36
  new(0,1,0)
37
37
  end
38
38
 
39
- # Creates a pure blue RGB object.
39
+ # Creates a pure blue RGB::Color object.
40
40
  def blue
41
41
  new(0,0,1)
42
42
  end
43
43
 
44
- # Creates a yellow RGB object.
44
+ # Creates a yellow RGB::Color object.
45
45
  def yellow
46
46
  new(1,1,0)
47
47
  end
48
48
 
49
- # Creates a cyan RGB object.
49
+ # Creates a cyan RGB::Color object.
50
50
  def cyan
51
51
  new(0,1,1)
52
52
  end
53
53
 
54
- # Creates a magenta RGB object.
54
+ # Creates a magenta RGB::Color object.
55
55
  def magenta
56
56
  new(1,0,1)
57
57
  end
@@ -71,3 +71,77 @@ class RGB
71
71
  end
72
72
 
73
73
  end
74
+
75
+ #----------------------------------------------------------------------#
76
+ # Module Methods #
77
+ #----------------------------------------------------------------------#
78
+
79
+ module RGB
80
+
81
+ class << self
82
+
83
+ # Creates a white RGB::Color object.
84
+ def white
85
+ Color.white
86
+ end
87
+
88
+ # Creates a black RGB::Color object.
89
+ def black
90
+ Color.black
91
+ end
92
+
93
+ # Creates a grey RGB::Color object. Defaults to lightness 0.5, a middle grey. Black equals grey(0), white equals grey(1).
94
+ #
95
+ # ::gray is an alias for ::grey.
96
+ def grey(lightness=0.5)
97
+ Color.grey(lightness)
98
+ end
99
+
100
+ # Alias gray for grey.
101
+ alias gray grey
102
+
103
+ # Creates a pure red RGB::Color object.
104
+ def red
105
+ Color.red
106
+ end
107
+
108
+ # Creates a pure green RGB::Color object.
109
+ def green
110
+ Color.green
111
+ end
112
+
113
+ # Creates a pure blue RGB::Color object.
114
+ def blue
115
+ Color.blue
116
+ end
117
+
118
+ # Creates a yellow RGB::Color object.
119
+ def yellow
120
+ Color.yellow
121
+ end
122
+
123
+ # Creates a cyan RGB::Color object.
124
+ def cyan
125
+ Color.cyan
126
+ end
127
+
128
+ # Creates a magenta RGB::Color object.
129
+ def magenta
130
+ Color.magenta
131
+ end
132
+
133
+ # Returns the 8 corners of the RGB cube.
134
+ def corners
135
+ [ black, red, yellow, green, cyan, blue, magenta, white ]
136
+ end
137
+
138
+ # Returns the centre of the RGB cube.
139
+ def centre
140
+ grey
141
+ end
142
+
143
+ alias center centre
144
+
145
+ end
146
+
147
+ 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
  #
@@ -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,20 @@ class RGB
50
50
  end
51
51
 
52
52
  end
53
+
54
+ #----------------------------------------------------------------------#
55
+ # Module Methods #
56
+ #----------------------------------------------------------------------#
57
+
58
+ module RGB
59
+
60
+ class << self
61
+
62
+ # Creates a new RGB::Color from three RGB::Colors representing the red, green, and blue components.
63
+ def assemble(*a)
64
+ Color.assemble(*a)
65
+ end
66
+
67
+ end
68
+
69
+ 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
  #
@@ -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 object 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
 
@@ -1,4 +1,4 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
3
  # Shuffles the object's red, green, and blue values.
4
4
  def shuffle!
@@ -6,9 +6,9 @@ class RGB
6
6
  self
7
7
  end
8
8
 
9
- # Creates a new RGB object with this object's red, green, and blue values shuffled.
9
+ # Creates a new RGB::Color with this object's red, green, and blue values shuffled.
10
10
  def shuffle
11
- RGB.new values.shuffle
11
+ RGB::Color.new values.shuffle
12
12
  end
13
13
 
14
14
  # Assigns random values to red, green, and blue.
@@ -17,9 +17,26 @@ class RGB
17
17
  self
18
18
  end
19
19
 
20
- # Creates a new RGB object with random red, green, and blue values.
20
+ # Creates a new RGB::Color with random red, green, and blue values.
21
21
  def self.rand
22
22
  new(Kernel::rand, Kernel::rand, Kernel::rand)
23
23
  end
24
24
 
25
25
  end
26
+
27
+ #----------------------------------------------------------------------#
28
+ # Module Methods #
29
+ #----------------------------------------------------------------------#
30
+
31
+ module RGB
32
+
33
+ class << self
34
+
35
+ # Creates a new RGB::Color with random red, green, and blue values.
36
+ def rand
37
+ Color.rand
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -1,4 +1,4 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
3
  # Returns the color in 16-bit RGB565 format.
4
4
  def rgb565
@@ -15,7 +15,7 @@ class RGB
15
15
  self.b = ( ( v & 0x001f ) ) << 3
16
16
  end
17
17
 
18
- # Creates a new RGB color from 16-bit RGB565 data.
18
+ # Creates a new RGB::Color from 16-bit RGB565 data.
19
19
  def self.rgb565(rgb565_string)
20
20
  c = self.new
21
21
  c.rgb565 = rgb565_string
@@ -23,3 +23,20 @@ class RGB
23
23
  end
24
24
 
25
25
  end
26
+
27
+ #----------------------------------------------------------------------#
28
+ # Module Methods #
29
+ #----------------------------------------------------------------------#
30
+
31
+ module RGB
32
+
33
+ class << self
34
+
35
+ # Creates a new RGB::Color from 16-bit RGB565 data.
36
+ def rgb565(rgb565_string)
37
+ Color.rgb565(rgb565_string)
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -1,4 +1,4 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
3
  # With help from:
4
4
  # - https://gist.github.com/XVilka/8346728
@@ -1,7 +1,7 @@
1
- class RGB
1
+ module RGB
2
2
 
3
3
  # redgreenblue version.
4
- VERSION = '0.15.0'
4
+ VERSION = '0.16.0'
5
5
 
6
6
  # Returns RGB::VERSION.
7
7
  def self.version
@@ -1,6 +1,6 @@
1
- class RGB
1
+ class RGB::Color
2
2
 
3
- # Prints a color swatch and details for the RGB object, using multiple lines.
3
+ # Prints a color swatch and details for the RGB::Color object, using multiple lines.
4
4
  #
5
5
  # You can optionally supply a second color to be shown inside the swatch, for comparison.
6
6
  def view(other=nil)