redgreenblue 0.6.0 → 0.7.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 +2 -0
- data/lib/redgreenblue/24bit.rb +12 -5
- data/lib/redgreenblue/48bit.rb +12 -5
- data/lib/redgreenblue/base.rb +18 -4
- data/lib/redgreenblue/gif.rb +3 -2
- data/lib/redgreenblue/hsl_hsv.rb +59 -0
- data/lib/redgreenblue/lazy.rb +29 -2
- data/lib/redgreenblue/nice.rb +2 -0
- data/lib/redgreenblue/opt/philipshue.rb +35 -0
- data/lib/redgreenblue/os/mac.rb +13 -9
- data/lib/redgreenblue/random.rb +6 -3
- data/lib/redgreenblue/rgb565.rb +10 -6
- 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: 9dea62bd74822100097e661284f2d44164a1be23ee0f81acdc778c182f0939ec
|
4
|
+
data.tar.gz: baa4c5ab0b3a367841ee18d94131ad4077661d515edb29939ed661175b479ee1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81808e69a0aadb6ea54cb291fd682c1949e2178df1c31954256167552f12b990ef825561689a958cb533144c25d31852785ebcebe9d07ddb5684b3b0dfcd26ba
|
7
|
+
data.tar.gz: 62370184d5b291081bd8e7a1f698647d961fd4c98525677821950f59e58e5536473d5f4c0e87da53109866e875caf94fc578fdf14d4de602d50fd0085d4fe6d9
|
data/lib/redgreenblue.rb
CHANGED
data/lib/redgreenblue/24bit.rb
CHANGED
@@ -2,43 +2,50 @@ class RGB
|
|
2
2
|
|
3
3
|
# r, g, b methods
|
4
4
|
|
5
|
+
# Returns the red component as an integer in the range 0..255 (an 8-bit value).
|
5
6
|
def r
|
6
7
|
(red * 255).round
|
7
8
|
end
|
8
9
|
|
10
|
+
# Returns the green component as an integer in the range 0..255 (an 8-bit value).
|
9
11
|
def g
|
10
12
|
(green * 255).round
|
11
13
|
end
|
12
14
|
|
15
|
+
# Returns the blue component as an integer in the range 0..255 (an 8-bit value).
|
13
16
|
def b
|
14
17
|
(blue * 255).round
|
15
18
|
end
|
16
19
|
|
20
|
+
# Sets the red component using an integer in the range 0..255 (an 8-bit value).
|
17
21
|
def r=(n)
|
18
22
|
self.red = n / 255.0
|
19
23
|
end
|
20
24
|
|
25
|
+
# Sets the green component using an integer in the range 0..255 (an 8-bit value).
|
21
26
|
def g=(n)
|
22
27
|
self.green = n / 255.0
|
23
28
|
end
|
24
29
|
|
30
|
+
# Sets the blue component using an integer in the range 0..255 (an 8-bit value).
|
25
31
|
def b=(n)
|
26
32
|
self.blue = n / 255.0
|
27
33
|
end
|
28
34
|
|
29
35
|
# rgb methods
|
30
36
|
|
37
|
+
# Returns the red, green, and blue components as integers in the range 0..255 (three 8-bit values).
|
31
38
|
def rgb
|
32
39
|
[r,g,b]
|
33
40
|
end
|
34
41
|
|
35
|
-
|
36
|
-
|
42
|
+
# Sets the red, green, and blue components using three integers in the range 0..255 (three 8-bit values).
|
43
|
+
def rgb=(*rgb)
|
44
|
+
self.r, self.g, self.b = rgb.flatten
|
37
45
|
end
|
38
46
|
|
39
|
-
#
|
40
|
-
|
41
|
-
def self.rgb(rgb)
|
47
|
+
# Creates a new object from red, green, and blue components as integers in the range 0..255 (three 8-bit values).
|
48
|
+
def self.rgb(*rgb)
|
42
49
|
c = self.new
|
43
50
|
c.rgb = rgb
|
44
51
|
c
|
data/lib/redgreenblue/48bit.rb
CHANGED
@@ -2,43 +2,50 @@ class RGB
|
|
2
2
|
|
3
3
|
# rr, gg, bb methods
|
4
4
|
|
5
|
+
# Returns the red component as an integer in the range 0..65535 (a 16-bit value).
|
5
6
|
def rr
|
6
7
|
(red * 65535).round
|
7
8
|
end
|
8
9
|
|
10
|
+
# Returns the green component as an integer in the range 0..65535 (a 16-bit value).
|
9
11
|
def gg
|
10
12
|
(green * 65535).round
|
11
13
|
end
|
12
14
|
|
15
|
+
# Returns the blue component as an integer in the range 0..65535 (a 16-bit value).
|
13
16
|
def bb
|
14
17
|
(blue * 65535).round
|
15
18
|
end
|
16
19
|
|
20
|
+
# Sets the red component using an integer in the range 0..65535 (a 16-bit value).
|
17
21
|
def rr=(n)
|
18
22
|
self.red = n / 65535.0
|
19
23
|
end
|
20
24
|
|
25
|
+
# Sets the green component using an integer in the range 0..65535 (a 16-bit value).
|
21
26
|
def gg=(n)
|
22
27
|
self.green = n / 65535.0
|
23
28
|
end
|
24
29
|
|
30
|
+
# Sets the blue component using an integer in the range 0..65535 (a 16-bit value).
|
25
31
|
def bb=(n)
|
26
32
|
self.blue = n / 65535.0
|
27
33
|
end
|
28
34
|
|
29
35
|
# rrggbb methods
|
30
36
|
|
37
|
+
# Returns the red, green, and blue components as integers in the range 0..65535 (three 16-bit values).
|
31
38
|
def rrggbb
|
32
39
|
[rr,gg,bb]
|
33
40
|
end
|
34
41
|
|
35
|
-
|
36
|
-
|
42
|
+
# Sets the red, green, and blue components using three integers in the range 0..65535 (three 16-bit values).
|
43
|
+
def rrggbb=(*rrggbb)
|
44
|
+
self.rr, self.gg, self.bb = rrggbb.flatten
|
37
45
|
end
|
38
46
|
|
39
|
-
#
|
40
|
-
|
41
|
-
def self.rrggbb(rrggbb)
|
47
|
+
# Creates a new object from red, green, and blue components as integers in the range 0..65535 (three 16-bit values).
|
48
|
+
def self.rrggbb(*rrggbb)
|
42
49
|
c = self.new
|
43
50
|
c.rrggbb = rrggbb
|
44
51
|
c
|
data/lib/redgreenblue/base.rb
CHANGED
@@ -2,8 +2,8 @@ class RGB
|
|
2
2
|
|
3
3
|
attr_reader :red, :green, :blue
|
4
4
|
|
5
|
-
def initialize(a
|
6
|
-
self.values = a
|
5
|
+
def initialize(*a)
|
6
|
+
self.values = a.any? ? a : [ 0.5, 0.5, 0.5 ]
|
7
7
|
end
|
8
8
|
|
9
9
|
def red=(n)
|
@@ -24,8 +24,22 @@ class RGB
|
|
24
24
|
|
25
25
|
alias to_a values
|
26
26
|
|
27
|
-
def values=(a)
|
28
|
-
self.red, self.green, self.blue = a
|
27
|
+
def values=(*a)
|
28
|
+
self.red, self.green, self.blue = a.flatten
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns true if this object and an other object represent exactly the same color. Otherwise returns false.
|
32
|
+
def ==(other)
|
33
|
+
( self.class == other.class ) && ( self.values == other.values )
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns a sorted hash of 3 key/value pairs
|
37
|
+
# for red, green and blue,
|
38
|
+
# sorted in order of decreasing value
|
39
|
+
def to_h
|
40
|
+
([:red, :green, :blue].zip values).sort_by {
|
41
|
+
|k,v| [-v,[:red, :green, :blue].index(k)]
|
42
|
+
}.to_h
|
29
43
|
end
|
30
44
|
|
31
45
|
private
|
data/lib/redgreenblue/gif.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
class RGB
|
2
2
|
|
3
|
-
#
|
3
|
+
# Returns a 1-pixel GIF image set to the color.
|
4
4
|
#
|
5
5
|
# With help from:
|
6
|
-
# -
|
6
|
+
# - https://www.perlmonks.org/?node_id=7974
|
7
7
|
def gif_pixel
|
8
8
|
"GIF89a\1\0\1\0\x90\0\0".b +
|
9
9
|
rgb.pack('C3') +
|
10
10
|
"\0\0\0,\0\0\0\0\1\0\1\0\0\x02\x02\x04\1\0;".b
|
11
11
|
end
|
12
12
|
|
13
|
+
# Writes a 1-pixel GIF image to a file.
|
13
14
|
def gif_pixel_write(file_path)
|
14
15
|
File.binwrite(file_path, gif_pixel)
|
15
16
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
class RGB
|
2
|
+
|
3
|
+
# Returns color as HSL:
|
4
|
+
# hue (0..360), saturation (0..1), lightness (0..1).
|
5
|
+
# When saturation is 0, hue is nil.
|
6
|
+
def hsl
|
7
|
+
hsl_hsv_c[0]
|
8
|
+
end
|
9
|
+
|
10
|
+
# Returns color as HSV:
|
11
|
+
# hue (0..360), saturation (0..1), value (0..1).
|
12
|
+
# When saturation is 0, hue is nil.
|
13
|
+
#
|
14
|
+
# #hsb is an alias for #hsv.
|
15
|
+
def hsv
|
16
|
+
hsl_hsv_c[1]
|
17
|
+
end
|
18
|
+
|
19
|
+
alias hsb hsv
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
# Compute HSL, HSV, and chroma.
|
24
|
+
# With help from:
|
25
|
+
# - https://en.wikipedia.org/wiki/HSL_and_HSV
|
26
|
+
def hsl_hsv_c
|
27
|
+
sorted_hash = to_h
|
28
|
+
min, max = sorted_hash.values.values_at(2,0)
|
29
|
+
|
30
|
+
chroma = max - min
|
31
|
+
|
32
|
+
hue =
|
33
|
+
if chroma == 0
|
34
|
+
nil
|
35
|
+
else
|
36
|
+
case sorted_hash.keys.first
|
37
|
+
when :red
|
38
|
+
60 * ( ( ( green - blue ) / chroma ).modulo 6 )
|
39
|
+
when :green
|
40
|
+
60 * ( ( blue - red ) / chroma + 2 )
|
41
|
+
when :blue
|
42
|
+
60 * ( ( red - green ) / chroma + 4 )
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
lightness = ( min + max ) / 2.0
|
47
|
+
|
48
|
+
saturation_hsl =
|
49
|
+
chroma == 0 ? 0.0 : ( chroma / ( 1 - (2 * lightness - 1).abs ) ).round(9)
|
50
|
+
|
51
|
+
value = max
|
52
|
+
|
53
|
+
saturation_hsv =
|
54
|
+
value == 0 ? 0.0 : chroma / value
|
55
|
+
|
56
|
+
[ [hue, saturation_hsl, lightness], [hue, saturation_hsv, value], chroma ]
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/lib/redgreenblue/lazy.rb
CHANGED
@@ -1,11 +1,38 @@
|
|
1
1
|
class RGB
|
2
2
|
|
3
|
+
# Creates a white RGB object.
|
3
4
|
def self.white
|
4
|
-
new(
|
5
|
+
new(1,1,1)
|
5
6
|
end
|
6
7
|
|
8
|
+
# Creates a black RGB object.
|
7
9
|
def self.black
|
8
|
-
new(
|
10
|
+
new(0,0,0)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Creates a grey RGB object. Defaults to lightness 0.5, a middle grey. Black equals grey(0), white equals grey(1).
|
14
|
+
#
|
15
|
+
# ::gray is an alias for ::grey.
|
16
|
+
def self.grey(lightness=0.5)
|
17
|
+
new(lightness, lightness, lightness)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Alias gray for grey.
|
21
|
+
self.singleton_class.send(:alias_method, :gray, :grey)
|
22
|
+
|
23
|
+
# Creates a pure red RGB object.
|
24
|
+
def self.red
|
25
|
+
new(1,0,0)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Creates a pure green RGB object.
|
29
|
+
def self.green
|
30
|
+
new(0,1,0)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Creates a pure blue RGB object.
|
34
|
+
def self.blue
|
35
|
+
new(0,0,1)
|
9
36
|
end
|
10
37
|
|
11
38
|
end
|
data/lib/redgreenblue/nice.rb
CHANGED
@@ -0,0 +1,35 @@
|
|
1
|
+
class RGB
|
2
|
+
|
3
|
+
# Returns the arguments required by the Philips Hue API to set a light to this RGB object's hue, saturation and brightness (HSB).
|
4
|
+
#
|
5
|
+
# When formatted as JSON, this hash can be used
|
6
|
+
# directly to set a light's state.
|
7
|
+
def to_philips_hue_api_hsb_arguments(black_off=true)
|
8
|
+
my_hsb = hsb
|
9
|
+
|
10
|
+
# Black means 'off'
|
11
|
+
if black_off and ( my_hsb[2] == 0 )
|
12
|
+
{ 'on' => false }
|
13
|
+
|
14
|
+
else
|
15
|
+
{
|
16
|
+
|
17
|
+
# On/Off state of the light
|
18
|
+
'on' => true,
|
19
|
+
|
20
|
+
# Brightness 1..254
|
21
|
+
# Note: a brightness of 1 will not switch the light off
|
22
|
+
'bri' => ( my_hsb[2] * 253 + 1 ).round,
|
23
|
+
|
24
|
+
# Hue 0..65535
|
25
|
+
'hue' => (( my_hsb[0] || 0 ) * 65535 / 360 ).round,
|
26
|
+
|
27
|
+
# Saturation 0..254
|
28
|
+
'sat' => ( my_hsb[1] * 254 ).round
|
29
|
+
|
30
|
+
}
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
data/lib/redgreenblue/os/mac.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
class RGB
|
2
2
|
|
3
|
+
# Shows the Mac OS color picker to choose a color for the RGB object.
|
3
4
|
def pick
|
4
5
|
result = RGB.mac_choose(rrggbb)
|
5
6
|
if result
|
@@ -7,10 +8,10 @@ class RGB
|
|
7
8
|
end
|
8
9
|
end
|
9
10
|
|
10
|
-
#
|
11
|
-
|
12
|
-
def self.pick
|
13
|
-
result = RGB.mac_choose(
|
11
|
+
# Shows the Mac OS color picker and creates an RGB object with the chosen color.
|
12
|
+
# If no default color is specified, the picker defaults to a middle grey.
|
13
|
+
def self.pick(default_color=RGB.new)
|
14
|
+
result = RGB.mac_choose(default_color.rrggbb)
|
14
15
|
if result
|
15
16
|
RGB.rrggbb result
|
16
17
|
else
|
@@ -20,11 +21,14 @@ class RGB
|
|
20
21
|
|
21
22
|
private
|
22
23
|
|
23
|
-
#
|
24
|
-
# - requires a 48-bit RGB triplet [rr, gg, bb] for default choice.
|
25
|
-
#
|
26
|
-
# - Returns nil when <cancel> is clicked or <esc> key hit.
|
27
|
-
# - Otherwise returns 48-bit RGB triplet [rr, gg, bb].
|
24
|
+
# Uses Applescript to call the color picker on Mac OS.
|
25
|
+
# - requires a 48-bit RGB triplet [rr, gg, bb] for default choice.
|
26
|
+
#
|
27
|
+
# - Returns nil when <cancel> is clicked or <esc> key hit.
|
28
|
+
# - Otherwise returns 48-bit RGB triplet [rr, gg, bb].
|
29
|
+
#
|
30
|
+
# Applescript command documented here:
|
31
|
+
# Standard Additions -> User Interaction -> choose color
|
28
32
|
def self.mac_choose(color)
|
29
33
|
|
30
34
|
script = <<~ENDSCRIPT
|
data/lib/redgreenblue/random.rb
CHANGED
@@ -1,17 +1,20 @@
|
|
1
1
|
class RGB
|
2
2
|
|
3
|
+
# Shuffles the object's red, green, and blue values.
|
3
4
|
def shuffle!
|
4
|
-
self.
|
5
|
+
self.values = values.shuffle
|
5
6
|
self
|
6
7
|
end
|
7
8
|
|
9
|
+
# Assigns random values to red, green, and blue.
|
8
10
|
def randomize!
|
9
|
-
self.
|
11
|
+
self.values = Kernel::rand, Kernel::rand, Kernel::rand
|
10
12
|
self
|
11
13
|
end
|
12
14
|
|
15
|
+
# Creates a new RGB object with random red, green, and blue values.
|
13
16
|
def self.rand
|
14
|
-
new(
|
17
|
+
new(Kernel::rand, Kernel::rand, Kernel::rand)
|
15
18
|
end
|
16
19
|
|
17
20
|
end
|
data/lib/redgreenblue/rgb565.rb
CHANGED
@@ -1,25 +1,29 @@
|
|
1
1
|
class RGB
|
2
2
|
|
3
|
+
# Returns the color in 16-bit RGB565 format.
|
3
4
|
def rgb565
|
4
5
|
[((r >> 3) << 11) + ((g >> 2) << 5) + (b >> 3)].pack 'S<'
|
5
6
|
end
|
6
7
|
|
7
|
-
#
|
8
|
-
|
9
|
-
|
8
|
+
# Sets the color from 16-bit RGB565 data.
|
9
|
+
# With help from:
|
10
|
+
# - https://stackoverflow.com/questions/2442576/
|
11
|
+
def rgb565=(rgb565_string)
|
12
|
+
v = ( rgb565_string.unpack "S<" )[0]
|
10
13
|
self.r = ( ( v & 0xf800 ) >> 11 ) << 3
|
11
14
|
self.g = ( ( v & 0x07e0 ) >> 5 ) << 2
|
12
15
|
self.b = ( ( v & 0x001f ) ) << 3
|
13
16
|
end
|
14
17
|
|
18
|
+
# Returns the color in 16-bit RGB565 format as a string of 0's and 1's
|
15
19
|
def rgb565_binary
|
16
20
|
rgb565.bytes.reverse.map { |b| "%08b" % b }.join
|
17
21
|
end
|
18
22
|
|
19
|
-
#
|
20
|
-
def self.rgb565(
|
23
|
+
# Creates a new RGB color from 16-bit RGB565 data.
|
24
|
+
def self.rgb565(rgb565_string)
|
21
25
|
c = self.new
|
22
|
-
c.rgb565 =
|
26
|
+
c.rgb565 = rgb565_string
|
23
27
|
c
|
24
28
|
end
|
25
29
|
|
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.7.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: 2019-
|
11
|
+
date: 2019-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -23,9 +23,11 @@ files:
|
|
23
23
|
- lib/redgreenblue/bgr24bit.rb
|
24
24
|
- lib/redgreenblue/gif.rb
|
25
25
|
- lib/redgreenblue/hex.rb
|
26
|
+
- lib/redgreenblue/hsl_hsv.rb
|
26
27
|
- lib/redgreenblue/lazy.rb
|
27
28
|
- lib/redgreenblue/misc.rb
|
28
29
|
- lib/redgreenblue/nice.rb
|
30
|
+
- lib/redgreenblue/opt/philipshue.rb
|
29
31
|
- lib/redgreenblue/os.rb
|
30
32
|
- lib/redgreenblue/os/mac.rb
|
31
33
|
- lib/redgreenblue/random.rb
|
@@ -53,5 +55,5 @@ requirements: []
|
|
53
55
|
rubygems_version: 3.0.3
|
54
56
|
signing_key:
|
55
57
|
specification_version: 4
|
56
|
-
summary:
|
58
|
+
summary: A simple Ruby library for handling RGB colors
|
57
59
|
test_files: []
|