redgreenblue 0.6.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1cac7c885ca1de3e3fe22ebfaf7207dd17b7bd25e8c1dd6f82cfdab0e0b5cee1
4
+ data.tar.gz: 7ca71212d78ae2a638d8bfb54dcb8a824367eac6e7fe8ca1eaa1e239b22bbaa9
5
+ SHA512:
6
+ metadata.gz: 23316f3526690807fabde09d5a909ae7afe396da7b039baa6352c814ab653d02ee5646aa23ece364748cf090f4b9218a34c0cd19b8a04313086b2c5f9dcadf97
7
+ data.tar.gz: 19f71a26312a83d845f8ff508758e653b693289be129e02281e65cb8da4b13d32ab5b58282883ea0d0dc4c156ddd775c0179354bea5dccc6065f00ad49b7f66c
@@ -0,0 +1,22 @@
1
+ class RGB
2
+ end
3
+
4
+ require 'redgreenblue/version'
5
+
6
+ require 'redgreenblue/base'
7
+
8
+ require 'redgreenblue/24bit'
9
+ require 'redgreenblue/48bit'
10
+ require 'redgreenblue/hex'
11
+
12
+ require 'redgreenblue/lazy'
13
+ require 'redgreenblue/nice'
14
+
15
+ require 'redgreenblue/rgb565'
16
+ require 'redgreenblue/bgr24bit'
17
+ require 'redgreenblue/gif'
18
+
19
+ require 'redgreenblue/misc'
20
+ require 'redgreenblue/random'
21
+
22
+ require 'redgreenblue/os'
@@ -0,0 +1,47 @@
1
+ class RGB
2
+
3
+ # r, g, b methods
4
+
5
+ def r
6
+ (red * 255).round
7
+ end
8
+
9
+ def g
10
+ (green * 255).round
11
+ end
12
+
13
+ def b
14
+ (blue * 255).round
15
+ end
16
+
17
+ def r=(n)
18
+ self.red = n / 255.0
19
+ end
20
+
21
+ def g=(n)
22
+ self.green = n / 255.0
23
+ end
24
+
25
+ def b=(n)
26
+ self.blue = n / 255.0
27
+ end
28
+
29
+ # rgb methods
30
+
31
+ def rgb
32
+ [r,g,b]
33
+ end
34
+
35
+ def rgb=(rgb)
36
+ self.r, self.g, self.b = rgb
37
+ end
38
+
39
+ # factory method
40
+
41
+ def self.rgb(rgb)
42
+ c = self.new
43
+ c.rgb = rgb
44
+ c
45
+ end
46
+
47
+ end
@@ -0,0 +1,47 @@
1
+ class RGB
2
+
3
+ # rr, gg, bb methods
4
+
5
+ def rr
6
+ (red * 65535).round
7
+ end
8
+
9
+ def gg
10
+ (green * 65535).round
11
+ end
12
+
13
+ def bb
14
+ (blue * 65535).round
15
+ end
16
+
17
+ def rr=(n)
18
+ self.red = n / 65535.0
19
+ end
20
+
21
+ def gg=(n)
22
+ self.green = n / 65535.0
23
+ end
24
+
25
+ def bb=(n)
26
+ self.blue = n / 65535.0
27
+ end
28
+
29
+ # rrggbb methods
30
+
31
+ def rrggbb
32
+ [rr,gg,bb]
33
+ end
34
+
35
+ def rrggbb=(rrggbb)
36
+ self.rr, self.gg, self.bb = rrggbb
37
+ end
38
+
39
+ # factory method
40
+
41
+ def self.rrggbb(rrggbb)
42
+ c = self.new
43
+ c.rrggbb = rrggbb
44
+ c
45
+ end
46
+
47
+ end
@@ -0,0 +1,38 @@
1
+ class RGB
2
+
3
+ attr_reader :red, :green, :blue
4
+
5
+ def initialize(a=[0.5, 0.5, 0.5])
6
+ self.values = a
7
+ end
8
+
9
+ def red=(n)
10
+ @red = limit(n)
11
+ end
12
+
13
+ def green=(n)
14
+ @green = limit(n)
15
+ end
16
+
17
+ def blue=(n)
18
+ @blue = limit(n)
19
+ end
20
+
21
+ def values
22
+ [ red, green, blue ]
23
+ end
24
+
25
+ alias to_a values
26
+
27
+ def values=(a)
28
+ self.red, self.green, self.blue = a
29
+ end
30
+
31
+ private
32
+
33
+ # limit to 0..1
34
+ def limit(n)
35
+ n <= 0 ? 0.0 : n >= 1 ? 1.0 : n
36
+ end
37
+
38
+ end
@@ -0,0 +1,20 @@
1
+ class RGB
2
+
3
+ # bgr 24-bit methods (as used by BMP bitmaps)
4
+
5
+ def bgr24
6
+ [b, g, r].pack('C3')
7
+ end
8
+
9
+ def bgr24=(s)
10
+ self.b, self.g, self.r = s.unpack('C3')
11
+ end
12
+
13
+ # factory method
14
+ def self.bgr24(bgr)
15
+ c = self.new
16
+ c.bgr24 = bgr
17
+ c
18
+ end
19
+
20
+ end
@@ -0,0 +1,17 @@
1
+ class RGB
2
+
3
+ # Return a 1-pixel GIF
4
+ #
5
+ # With help from:
6
+ # - http://www.perlmonks.org/?node_id=7974
7
+ def gif_pixel
8
+ "GIF89a\1\0\1\0\x90\0\0".b +
9
+ rgb.pack('C3') +
10
+ "\0\0\0,\0\0\0\0\1\0\1\0\0\x02\x02\x04\1\0;".b
11
+ end
12
+
13
+ def gif_pixel_write(file_path)
14
+ File.binwrite(file_path, gif_pixel)
15
+ end
16
+
17
+ end
@@ -0,0 +1,36 @@
1
+ class RGB
2
+
3
+ def hex(shorthand=false)
4
+ if shorthand
5
+ RGB.hex_shorthand hexadecimal
6
+ else
7
+ hexadecimal
8
+ end
9
+ end
10
+
11
+ def hex=(s)
12
+ if ( s =~ /^(#?)(\h\h)(\h\h)(\h\h)$/ ) # 6-digit hex
13
+ self.rgb = [ $2.to_i(16), $3.to_i(16), $4.to_i(16) ]
14
+ elsif ( s =~ /^(#?)(\h)(\h)(\h)$/ ) # 3-digit hex
15
+ self.rgb = [ ($2*2).to_i(16), ($3*2).to_i(16), ($4*2).to_i(16) ]
16
+ end
17
+ end
18
+
19
+ # factory method
20
+ def self.hex(s)
21
+ c = self.new
22
+ c.hex = s
23
+ c
24
+ end
25
+
26
+ def self.hex_shorthand(h)
27
+ h.sub( /^(#?)(\h)\2(\h)\3(\h)\4$/, '\1\2\3\4' )
28
+ end
29
+
30
+ private
31
+
32
+ def hexadecimal
33
+ '%02x%02x%02x' % [ r, g, b ]
34
+ end
35
+
36
+ end
@@ -0,0 +1,11 @@
1
+ class RGB
2
+
3
+ def self.white
4
+ new([1,1,1])
5
+ end
6
+
7
+ def self.black
8
+ new([0,0,0])
9
+ end
10
+
11
+ end
@@ -0,0 +1,60 @@
1
+ class RGB
2
+
3
+ # Mix
4
+
5
+ # Mix with second RGB color.
6
+ # p denotes portion of the mixed-in color to be used.
7
+ # p=0 delivers pure original color (no change),
8
+ # p=1 delivers pure mixed-in color.
9
+
10
+ def mix!(color,p=0.5)
11
+ self.values = mix_values(color.values, p)
12
+ self
13
+ end
14
+
15
+ def mix(color,p=0.5)
16
+ RGB.new mix_values(color.values, p)
17
+ end
18
+
19
+ # Invert
20
+
21
+ def invert!
22
+ self.values = values.map { |v| 1-v }
23
+ self
24
+ end
25
+
26
+ def invert
27
+ dup.invert!
28
+ end
29
+
30
+ # Mix with white
31
+
32
+ def whiten!(p)
33
+ mix!(RGB.white, p)
34
+ end
35
+
36
+ def whiten(p)
37
+ mix(RGB.white, p)
38
+ end
39
+
40
+ # Mix with black
41
+
42
+ def blacken!(p)
43
+ mix!(RGB.black, p)
44
+ end
45
+
46
+ def blacken(p)
47
+ mix(RGB.black, p)
48
+ end
49
+
50
+ private
51
+
52
+ def mix_values(v, p)
53
+ raise(ArgumentError, "Portion '#{p}' not in range (0..1)") unless (0..1).include? p
54
+ [
55
+ ( red * (1-p) ) + ( v[0] * p ),
56
+ ( green * (1-p) ) + ( v[1] * p ),
57
+ ( blue * (1-p) ) + ( v[2] * p )
58
+ ]
59
+ end
60
+ end
@@ -0,0 +1,7 @@
1
+ class RGB
2
+
3
+ def inspect
4
+ "RGB ##{hex} (red=%1.5f green=%1.5f blue=%1.5f)" % [red, green, blue]
5
+ end
6
+
7
+ end
@@ -0,0 +1,3 @@
1
+ if /darwin/ =~ RUBY_PLATFORM
2
+ require 'redgreenblue/os/mac'
3
+ end
@@ -0,0 +1,50 @@
1
+ class RGB
2
+
3
+ def pick
4
+ result = RGB.mac_choose(rrggbb)
5
+ if result
6
+ self.rrggbb = result
7
+ end
8
+ end
9
+
10
+ # factory method
11
+
12
+ def self.pick
13
+ result = RGB.mac_choose(RGB.new.rrggbb)
14
+ if result
15
+ RGB.rrggbb result
16
+ else
17
+ nil
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ # Use Applescript to call color picker on Mac.
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].
28
+ def self.mac_choose(color)
29
+
30
+ script = <<~ENDSCRIPT
31
+ tell application "Terminal"
32
+ try
33
+ return choose color default color { #{color[0]}, #{color[1]}, #{color[2]} }
34
+ on error
35
+ return ""
36
+ end try
37
+ end tell
38
+ ENDSCRIPT
39
+
40
+ result = `osascript -e '#{script}'`.chomp
41
+
42
+ if result.empty?
43
+ nil
44
+ else
45
+ (result.split( /,\w*/ )).map { |s| s.to_i }
46
+ end
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,17 @@
1
+ class RGB
2
+
3
+ def shuffle!
4
+ self.red, self.green, self.blue = [red, green, blue].shuffle
5
+ self
6
+ end
7
+
8
+ def randomize!
9
+ self.red, self.green, self.blue = Kernel::rand, Kernel::rand, Kernel::rand
10
+ self
11
+ end
12
+
13
+ def self.rand
14
+ new([Kernel::rand, Kernel::rand, Kernel::rand])
15
+ end
16
+
17
+ end
@@ -0,0 +1,26 @@
1
+ class RGB
2
+
3
+ def rgb565
4
+ [((r >> 3) << 11) + ((g >> 2) << 5) + (b >> 3)].pack 'S<'
5
+ end
6
+
7
+ # https://stackoverflow.com/questions/2442576/
8
+ def rgb565=(s)
9
+ v = ( s.unpack "S<" )[0]
10
+ self.r = ( ( v & 0xf800 ) >> 11 ) << 3
11
+ self.g = ( ( v & 0x07e0 ) >> 5 ) << 2
12
+ self.b = ( ( v & 0x001f ) ) << 3
13
+ end
14
+
15
+ def rgb565_binary
16
+ rgb565.bytes.reverse.map { |b| "%08b" % b }.join
17
+ end
18
+
19
+ # factory method
20
+ def self.rgb565(s)
21
+ c = self.new
22
+ c.rgb565 = s
23
+ c
24
+ end
25
+
26
+ end
@@ -0,0 +1,3 @@
1
+ class RGB
2
+ VERSION = '0.6.0'
3
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redgreenblue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ platform: ruby
6
+ authors:
7
+ - lllist.eu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/redgreenblue.rb
20
+ - lib/redgreenblue/24bit.rb
21
+ - lib/redgreenblue/48bit.rb
22
+ - lib/redgreenblue/base.rb
23
+ - lib/redgreenblue/bgr24bit.rb
24
+ - lib/redgreenblue/gif.rb
25
+ - lib/redgreenblue/hex.rb
26
+ - lib/redgreenblue/lazy.rb
27
+ - lib/redgreenblue/misc.rb
28
+ - lib/redgreenblue/nice.rb
29
+ - lib/redgreenblue/os.rb
30
+ - lib/redgreenblue/os/mac.rb
31
+ - lib/redgreenblue/random.rb
32
+ - lib/redgreenblue/rgb565.rb
33
+ - lib/redgreenblue/version.rb
34
+ homepage: https://github.com/lllisteu/redgreenblue
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.2.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.0.3
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: RGB colors for Ruby
57
+ test_files: []