Class: RGB
- Inherits:
-
Object
- Object
- RGB
- Defined in:
- lib/redgreenblue.rb,
lib/redgreenblue/gif.rb,
lib/redgreenblue/hex.rb,
lib/redgreenblue/base.rb,
lib/redgreenblue/lazy.rb,
lib/redgreenblue/misc.rb,
lib/redgreenblue/nice.rb,
lib/redgreenblue/24bit.rb,
lib/redgreenblue/48bit.rb,
lib/redgreenblue/os/mac.rb,
lib/redgreenblue/random.rb,
lib/redgreenblue/rgb565.rb,
lib/redgreenblue/hsl_hsv.rb,
lib/redgreenblue/version.rb,
lib/redgreenblue/bgr24bit.rb,
lib/redgreenblue/opt/philipshue.rb
Constant Summary collapse
- VERSION =
'0.7.0'
Instance Attribute Summary collapse
-
#blue ⇒ Object
Returns the value of attribute blue.
-
#green ⇒ Object
Returns the value of attribute green.
-
#red ⇒ Object
Returns the value of attribute red.
Class Method Summary collapse
-
.bgr24(bgr) ⇒ Object
factory method.
-
.black ⇒ Object
Creates a black RGB object.
-
.blue ⇒ Object
Creates a pure blue RGB object.
-
.green ⇒ Object
Creates a pure green RGB object.
-
.grey(lightness = 0.5) ⇒ Object
Creates a grey RGB object.
-
.hex(s) ⇒ Object
factory method.
- .hex_shorthand(h) ⇒ Object
-
.pick(default_color = RGB.new) ⇒ Object
Shows the Mac OS color picker and creates an RGB object with the chosen color.
-
.rand ⇒ Object
Creates a new RGB object with random red, green, and blue values.
-
.red ⇒ Object
Creates a pure red RGB object.
-
.rgb(*rgb) ⇒ Object
Creates a new object from red, green, and blue components as integers in the range 0..255 (three 8-bit values).
-
.rgb565(rgb565_string) ⇒ Object
Creates a new RGB color from 16-bit RGB565 data.
-
.rrggbb(*rrggbb) ⇒ Object
Creates a new object from red, green, and blue components as integers in the range 0..65535 (three 16-bit values).
-
.white ⇒ Object
Creates a white RGB object.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Returns true if this object and an other object represent exactly the same color.
-
#b ⇒ Object
Returns the blue component as an integer in the range 0..255 (an 8-bit value).
-
#b=(n) ⇒ Object
Sets the blue component using an integer in the range 0..255 (an 8-bit value).
-
#bb ⇒ Object
Returns the blue component as an integer in the range 0..65535 (a 16-bit value).
-
#bb=(n) ⇒ Object
Sets the blue component using an integer in the range 0..65535 (a 16-bit value).
-
#bgr24 ⇒ Object
bgr 24-bit methods (as used by BMP bitmaps).
- #bgr24=(s) ⇒ Object
- #blacken(p) ⇒ Object
-
#blacken!(p) ⇒ Object
Mix with black.
-
#g ⇒ Object
Returns the green component as an integer in the range 0..255 (an 8-bit value).
-
#g=(n) ⇒ Object
Sets the green component using an integer in the range 0..255 (an 8-bit value).
-
#gg ⇒ Object
Returns the green component as an integer in the range 0..65535 (a 16-bit value).
-
#gg=(n) ⇒ Object
Sets the green component using an integer in the range 0..65535 (a 16-bit value).
-
#gif_pixel ⇒ Object
Returns a 1-pixel GIF image set to the color.
-
#gif_pixel_write(file_path) ⇒ Object
Writes a 1-pixel GIF image to a file.
- #hex(shorthand = false) ⇒ Object
- #hex=(s) ⇒ Object
-
#hsl ⇒ Object
Returns color as HSL: hue (0..360), saturation (0..1), lightness (0..1).
-
#hsv ⇒ Object
(also: #hsb)
Returns color as HSV: hue (0..360), saturation (0..1), value (0..1).
-
#initialize(*a) ⇒ RGB
constructor
A new instance of RGB.
- #inspect ⇒ Object (also: #to_s)
- #invert ⇒ Object
-
#invert! ⇒ Object
Invert.
- #mix(color, p = 0.5) ⇒ Object
-
#mix!(color, p = 0.5) ⇒ Object
Mix with second RGB color.
-
#pick ⇒ Object
Shows the Mac OS color picker to choose a color for the RGB object.
-
#r ⇒ Object
Returns the red component as an integer in the range 0..255 (an 8-bit value).
-
#r=(n) ⇒ Object
Sets the red component using an integer in the range 0..255 (an 8-bit value).
-
#randomize! ⇒ Object
Assigns random values to red, green, and blue.
-
#rgb ⇒ Object
Returns the red, green, and blue components as integers in the range 0..255 (three 8-bit values).
-
#rgb565 ⇒ Object
Returns the color in 16-bit RGB565 format.
-
#rgb565=(rgb565_string) ⇒ Object
Sets the color from 16-bit RGB565 data.
-
#rgb565_binary ⇒ Object
Returns the color in 16-bit RGB565 format as a string of 0’s and 1’s.
-
#rgb=(*rgb) ⇒ Object
Sets the red, green, and blue components using three integers in the range 0..255 (three 8-bit values).
-
#rr ⇒ Object
Returns the red component as an integer in the range 0..65535 (a 16-bit value).
-
#rr=(n) ⇒ Object
Sets the red component using an integer in the range 0..65535 (a 16-bit value).
-
#rrggbb ⇒ Object
Returns the red, green, and blue components as integers in the range 0..65535 (three 16-bit values).
-
#rrggbb=(*rrggbb) ⇒ Object
Sets the red, green, and blue components using three integers in the range 0..65535 (three 16-bit values).
-
#shuffle! ⇒ Object
Shuffles the object’s red, green, and blue values.
-
#to_h ⇒ Object
Returns a sorted hash of 3 key/value pairs for red, green and blue, sorted in order of decreasing value.
-
#to_philips_hue_api_hsb_arguments(black_off = true) ⇒ Object
Returns the arguments required by the Philips Hue API to set a light to this RGB object’s hue, saturation and brightness (HSB).
- #values ⇒ Object (also: #to_a)
- #values=(*a) ⇒ Object
- #whiten(p) ⇒ Object
-
#whiten!(p) ⇒ Object
Mix with white.
Constructor Details
#initialize(*a) ⇒ RGB
Returns a new instance of RGB.
5 6 7 |
# File 'lib/redgreenblue/base.rb', line 5 def initialize(*a) self.values = a.any? ? a : [ 0.5, 0.5, 0.5 ] end |
Instance Attribute Details
#blue ⇒ Object
Returns the value of attribute blue.
3 4 5 |
# File 'lib/redgreenblue/base.rb', line 3 def blue @blue end |
#green ⇒ Object
Returns the value of attribute green.
3 4 5 |
# File 'lib/redgreenblue/base.rb', line 3 def green @green end |
#red ⇒ Object
Returns the value of attribute red.
3 4 5 |
# File 'lib/redgreenblue/base.rb', line 3 def red @red end |
Class Method Details
.bgr24(bgr) ⇒ Object
factory method
14 15 16 17 18 |
# File 'lib/redgreenblue/bgr24bit.rb', line 14 def self.bgr24(bgr) c = self.new c.bgr24 = bgr c end |
.black ⇒ Object
Creates a black RGB object.
9 10 11 |
# File 'lib/redgreenblue/lazy.rb', line 9 def self.black new(0,0,0) end |
.blue ⇒ Object
Creates a pure blue RGB object.
34 35 36 |
# File 'lib/redgreenblue/lazy.rb', line 34 def self.blue new(0,0,1) end |
.green ⇒ Object
Creates a pure green RGB object.
29 30 31 |
# File 'lib/redgreenblue/lazy.rb', line 29 def self.green new(0,1,0) end |
.grey(lightness = 0.5) ⇒ Object
Creates a grey RGB object. Defaults to lightness 0.5, a middle grey. Black equals grey(0), white equals grey(1).
::gray is an alias for ::grey.
16 17 18 |
# File 'lib/redgreenblue/lazy.rb', line 16 def self.grey(lightness=0.5) new(lightness, lightness, lightness) end |
.hex(s) ⇒ Object
factory method
20 21 22 23 24 |
# File 'lib/redgreenblue/hex.rb', line 20 def self.hex(s) c = self.new c.hex = s c end |
.hex_shorthand(h) ⇒ Object
26 27 28 |
# File 'lib/redgreenblue/hex.rb', line 26 def self.hex_shorthand(h) h.sub( /^(#?)(\h)\2(\h)\3(\h)\4$/, '\1\2\3\4' ) end |
.pick(default_color = RGB.new) ⇒ Object
Shows the Mac OS color picker and creates an RGB object with the chosen color. If no default color is specified, the picker defaults to a middle grey.
13 14 15 16 17 18 19 20 |
# File 'lib/redgreenblue/os/mac.rb', line 13 def self.pick(default_color=RGB.new) result = RGB.mac_choose(default_color.rrggbb) if result RGB.rrggbb result else nil end end |
.rand ⇒ Object
Creates a new RGB object with random red, green, and blue values.
16 17 18 |
# File 'lib/redgreenblue/random.rb', line 16 def self.rand new(Kernel::rand, Kernel::rand, Kernel::rand) end |
.red ⇒ Object
Creates a pure red RGB object.
24 25 26 |
# File 'lib/redgreenblue/lazy.rb', line 24 def self.red new(1,0,0) end |
.rgb(*rgb) ⇒ Object
Creates a new object from red, green, and blue components as integers in the range 0..255 (three 8-bit values).
48 49 50 51 52 |
# File 'lib/redgreenblue/24bit.rb', line 48 def self.rgb(*rgb) c = self.new c.rgb = rgb c end |
.rgb565(rgb565_string) ⇒ Object
Creates a new RGB color from 16-bit RGB565 data.
24 25 26 27 28 |
# File 'lib/redgreenblue/rgb565.rb', line 24 def self.rgb565(rgb565_string) c = self.new c.rgb565 = rgb565_string c end |
.rrggbb(*rrggbb) ⇒ Object
Creates a new object from red, green, and blue components as integers in the range 0..65535 (three 16-bit values).
48 49 50 51 52 |
# File 'lib/redgreenblue/48bit.rb', line 48 def self.rrggbb(*rrggbb) c = self.new c.rrggbb = rrggbb c end |
.white ⇒ Object
Creates a white RGB object.
4 5 6 |
# File 'lib/redgreenblue/lazy.rb', line 4 def self.white new(1,1,1) end |
Instance Method Details
#==(other) ⇒ Object
Returns true if this object and an other object represent exactly the same color. Otherwise returns false.
32 33 34 |
# File 'lib/redgreenblue/base.rb', line 32 def ==(other) ( self.class == other.class ) && ( self.values == other.values ) end |
#b ⇒ Object
Returns the blue component as an integer in the range 0..255 (an 8-bit value).
16 17 18 |
# File 'lib/redgreenblue/24bit.rb', line 16 def b (blue * 255).round end |
#b=(n) ⇒ Object
Sets the blue component using an integer in the range 0..255 (an 8-bit value).
31 32 33 |
# File 'lib/redgreenblue/24bit.rb', line 31 def b=(n) self.blue = n / 255.0 end |
#bb ⇒ Object
Returns the blue component as an integer in the range 0..65535 (a 16-bit value).
16 17 18 |
# File 'lib/redgreenblue/48bit.rb', line 16 def bb (blue * 65535).round end |
#bb=(n) ⇒ Object
Sets the blue component using an integer in the range 0..65535 (a 16-bit value).
31 32 33 |
# File 'lib/redgreenblue/48bit.rb', line 31 def bb=(n) self.blue = n / 65535.0 end |
#bgr24 ⇒ Object
bgr 24-bit methods (as used by BMP bitmaps)
5 6 7 |
# File 'lib/redgreenblue/bgr24bit.rb', line 5 def bgr24 [b, g, r].pack('C3') end |
#bgr24=(s) ⇒ Object
9 10 11 |
# File 'lib/redgreenblue/bgr24bit.rb', line 9 def bgr24=(s) self.b, self.g, self.r = s.unpack('C3') end |
#blacken(p) ⇒ Object
46 47 48 |
# File 'lib/redgreenblue/misc.rb', line 46 def blacken(p) mix(RGB.black, p) end |
#blacken!(p) ⇒ Object
Mix with black
42 43 44 |
# File 'lib/redgreenblue/misc.rb', line 42 def blacken!(p) mix!(RGB.black, p) end |
#g ⇒ Object
Returns the green component as an integer in the range 0..255 (an 8-bit value).
11 12 13 |
# File 'lib/redgreenblue/24bit.rb', line 11 def g (green * 255).round end |
#g=(n) ⇒ Object
Sets the green component using an integer in the range 0..255 (an 8-bit value).
26 27 28 |
# File 'lib/redgreenblue/24bit.rb', line 26 def g=(n) self.green = n / 255.0 end |
#gg ⇒ Object
Returns the green component as an integer in the range 0..65535 (a 16-bit value).
11 12 13 |
# File 'lib/redgreenblue/48bit.rb', line 11 def gg (green * 65535).round end |
#gg=(n) ⇒ Object
Sets the green component using an integer in the range 0..65535 (a 16-bit value).
26 27 28 |
# File 'lib/redgreenblue/48bit.rb', line 26 def gg=(n) self.green = n / 65535.0 end |
#gif_pixel ⇒ Object
7 8 9 10 11 |
# File 'lib/redgreenblue/gif.rb', line 7 def gif_pixel "GIF89a\1\0\1\0\x90\0\0".b + rgb.pack('C3') + "\0\0\0,\0\0\0\0\1\0\1\0\0\x02\x02\x04\1\0;".b end |
#gif_pixel_write(file_path) ⇒ Object
Writes a 1-pixel GIF image to a file.
14 15 16 |
# File 'lib/redgreenblue/gif.rb', line 14 def gif_pixel_write(file_path) File.binwrite(file_path, gif_pixel) end |
#hex(shorthand = false) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/redgreenblue/hex.rb', line 3 def hex(shorthand=false) if shorthand RGB.hex_shorthand hexadecimal else hexadecimal end end |
#hex=(s) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/redgreenblue/hex.rb', line 11 def hex=(s) if ( s =~ /^(#?)(\h\h)(\h\h)(\h\h)$/ ) # 6-digit hex self.rgb = [ $2.to_i(16), $3.to_i(16), $4.to_i(16) ] elsif ( s =~ /^(#?)(\h)(\h)(\h)$/ ) # 3-digit hex self.rgb = [ ($2*2).to_i(16), ($3*2).to_i(16), ($4*2).to_i(16) ] end end |
#hsl ⇒ Object
Returns color as HSL: hue (0..360), saturation (0..1), lightness (0..1). When saturation is 0, hue is nil.
6 7 8 |
# File 'lib/redgreenblue/hsl_hsv.rb', line 6 def hsl hsl_hsv_c[0] end |
#hsv ⇒ Object Also known as: hsb
Returns color as HSV: hue (0..360), saturation (0..1), value (0..1). When saturation is 0, hue is nil.
#hsb is an alias for #hsv.
15 16 17 |
# File 'lib/redgreenblue/hsl_hsv.rb', line 15 def hsv hsl_hsv_c[1] end |
#inspect ⇒ Object Also known as: to_s
3 4 5 |
# File 'lib/redgreenblue/nice.rb', line 3 def inspect "RGB ##{hex} (red=%1.5f green=%1.5f blue=%1.5f)" % [red, green, blue] end |
#invert ⇒ Object
26 27 28 |
# File 'lib/redgreenblue/misc.rb', line 26 def invert dup.invert! end |
#invert! ⇒ Object
Invert
21 22 23 24 |
# File 'lib/redgreenblue/misc.rb', line 21 def invert! self.values = values.map { |v| 1-v } self end |
#mix(color, p = 0.5) ⇒ Object
15 16 17 |
# File 'lib/redgreenblue/misc.rb', line 15 def mix(color,p=0.5) RGB.new mix_values(color.values, p) end |
#mix!(color, p = 0.5) ⇒ Object
Mix with second RGB color. p denotes portion of the mixed-in color to be used. p=0 delivers pure original color (no change), p=1 delivers pure mixed-in color.
10 11 12 13 |
# File 'lib/redgreenblue/misc.rb', line 10 def mix!(color,p=0.5) self.values = mix_values(color.values, p) self end |
#pick ⇒ Object
Shows the Mac OS color picker to choose a color for the RGB object.
4 5 6 7 8 9 |
# File 'lib/redgreenblue/os/mac.rb', line 4 def pick result = RGB.mac_choose(rrggbb) if result self.rrggbb = result end end |
#r ⇒ Object
Returns the red component as an integer in the range 0..255 (an 8-bit value).
6 7 8 |
# File 'lib/redgreenblue/24bit.rb', line 6 def r (red * 255).round end |
#r=(n) ⇒ Object
Sets the red component using an integer in the range 0..255 (an 8-bit value).
21 22 23 |
# File 'lib/redgreenblue/24bit.rb', line 21 def r=(n) self.red = n / 255.0 end |
#randomize! ⇒ Object
Assigns random values to red, green, and blue.
10 11 12 13 |
# File 'lib/redgreenblue/random.rb', line 10 def randomize! self.values = Kernel::rand, Kernel::rand, Kernel::rand self end |
#rgb ⇒ Object
Returns the red, green, and blue components as integers in the range 0..255 (three 8-bit values).
38 39 40 |
# File 'lib/redgreenblue/24bit.rb', line 38 def rgb [r,g,b] end |
#rgb565 ⇒ Object
Returns the color in 16-bit RGB565 format.
4 5 6 |
# File 'lib/redgreenblue/rgb565.rb', line 4 def rgb565 [((r >> 3) << 11) + ((g >> 2) << 5) + (b >> 3)].pack 'S<' end |
#rgb565=(rgb565_string) ⇒ Object
Sets the color from 16-bit RGB565 data. With help from:
11 12 13 14 15 16 |
# File 'lib/redgreenblue/rgb565.rb', line 11 def rgb565=(rgb565_string) v = ( rgb565_string.unpack "S<" )[0] self.r = ( ( v & 0xf800 ) >> 11 ) << 3 self.g = ( ( v & 0x07e0 ) >> 5 ) << 2 self.b = ( ( v & 0x001f ) ) << 3 end |
#rgb565_binary ⇒ Object
Returns the color in 16-bit RGB565 format as a string of 0’s and 1’s
19 20 21 |
# File 'lib/redgreenblue/rgb565.rb', line 19 def rgb565_binary rgb565.bytes.reverse.map { |b| "%08b" % b }.join end |
#rgb=(*rgb) ⇒ Object
Sets the red, green, and blue components using three integers in the range 0..255 (three 8-bit values).
43 44 45 |
# File 'lib/redgreenblue/24bit.rb', line 43 def rgb=(*rgb) self.r, self.g, self.b = rgb.flatten end |
#rr ⇒ Object
Returns the red component as an integer in the range 0..65535 (a 16-bit value).
6 7 8 |
# File 'lib/redgreenblue/48bit.rb', line 6 def rr (red * 65535).round end |
#rr=(n) ⇒ Object
Sets the red component using an integer in the range 0..65535 (a 16-bit value).
21 22 23 |
# File 'lib/redgreenblue/48bit.rb', line 21 def rr=(n) self.red = n / 65535.0 end |
#rrggbb ⇒ Object
Returns the red, green, and blue components as integers in the range 0..65535 (three 16-bit values).
38 39 40 |
# File 'lib/redgreenblue/48bit.rb', line 38 def rrggbb [rr,gg,bb] end |
#rrggbb=(*rrggbb) ⇒ Object
Sets the red, green, and blue components using three integers in the range 0..65535 (three 16-bit values).
43 44 45 |
# File 'lib/redgreenblue/48bit.rb', line 43 def rrggbb=(*rrggbb) self.rr, self.gg, self.bb = rrggbb.flatten end |
#shuffle! ⇒ Object
Shuffles the object’s red, green, and blue values.
4 5 6 7 |
# File 'lib/redgreenblue/random.rb', line 4 def shuffle! self.values = values.shuffle self end |
#to_h ⇒ Object
Returns a sorted hash of 3 key/value pairs for red, green and blue, sorted in order of decreasing value
39 40 41 42 43 |
# File 'lib/redgreenblue/base.rb', line 39 def to_h ([:red, :green, :blue].zip values).sort_by { |k,v| [-v,[:red, :green, :blue].index(k)] }.to_h end |
#to_philips_hue_api_hsb_arguments(black_off = true) ⇒ Object
Returns the arguments required by the Philips Hue API to set a light to this RGB object’s hue, saturation and brightness (HSB).
When formatted as JSON, this hash can be used directly to set a light’s state.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/redgreenblue/opt/philipshue.rb', line 7 def to_philips_hue_api_hsb_arguments(black_off=true) my_hsb = hsb # Black means 'off' if black_off and ( my_hsb[2] == 0 ) { 'on' => false } else { # On/Off state of the light 'on' => true, # Brightness 1..254 # Note: a brightness of 1 will not switch the light off 'bri' => ( my_hsb[2] * 253 + 1 ).round, # Hue 0..65535 'hue' => (( my_hsb[0] || 0 ) * 65535 / 360 ).round, # Saturation 0..254 'sat' => ( my_hsb[1] * 254 ).round } end end |
#values ⇒ Object Also known as: to_a
21 22 23 |
# File 'lib/redgreenblue/base.rb', line 21 def values [ red, green, blue ] end |
#values=(*a) ⇒ Object
27 28 29 |
# File 'lib/redgreenblue/base.rb', line 27 def values=(*a) self.red, self.green, self.blue = a.flatten end |