redgreenblue 0.7.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,6 +6,11 @@ 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.
10
+ def shuffle
11
+ RGB.new values.shuffle
12
+ end
13
+
9
14
  # Assigns random values to red, green, and blue.
10
15
  def randomize!
11
16
  self.values = Kernel::rand, Kernel::rand, Kernel::rand
@@ -15,11 +15,6 @@ class RGB
15
15
  self.b = ( ( v & 0x001f ) ) << 3
16
16
  end
17
17
 
18
- # Returns the color in 16-bit RGB565 format as a string of 0's and 1's
19
- def rgb565_binary
20
- rgb565.bytes.reverse.map { |b| "%08b" % b }.join
21
- end
22
-
23
18
  # Creates a new RGB color from 16-bit RGB565 data.
24
19
  def self.rgb565(rgb565_string)
25
20
  c = self.new
@@ -0,0 +1,19 @@
1
+ class RGB
2
+
3
+ # With help from:
4
+ # - https://gist.github.com/XVilka/8346728
5
+ # - https://unix.stackexchange.com/questions/404414
6
+
7
+ # Returns the ANSI escape sequence required to set the foreground color on a terminal to this color.
8
+ # Only works on terminals that support 24-bit colors, so-called "true color".
9
+ def terminal_foreground
10
+ "\e[38;2;%d;%d;%dm" % rgb
11
+ end
12
+
13
+ # Returns the ANSI escape sequence required to set the background color on a terminal to this color.
14
+ # Only works on terminals that support 24-bit colors, so-called "true color".
15
+ def terminal_background
16
+ "\e[48;2;%d;%d;%dm" % rgb
17
+ end
18
+
19
+ end
@@ -1,3 +1,11 @@
1
1
  class RGB
2
- VERSION = '0.7.0'
2
+
3
+ # redgreenblue version.
4
+ VERSION = '0.12.0'
5
+
6
+ # Returns RGB::VERSION.
7
+ def self.version
8
+ VERSION
9
+ end
10
+
3
11
  end
@@ -0,0 +1,34 @@
1
+ class RGB
2
+
3
+ # Prints details for the RGB object, using multiple lines.
4
+ def view
5
+ puts _inspect_view
6
+ end
7
+
8
+ alias v view
9
+
10
+ private
11
+
12
+ def _inspect_view
13
+ o = []
14
+ o << 3.times.map { terminal_background + " \e[0m "}
15
+ o << [ "#{_inspect_hex} ", '%-7.7s ' % color_space, ' ' ]
16
+
17
+ o << components.map { |c| c.terminal_background + " \e[0m " }
18
+ o << %w(R: G: B:)
19
+ o << values.map { |v| '%1.5f ' % v }
20
+ o << rgb.map { |v| '%3d ' % v }
21
+
22
+ o << %w(H: S: L:)
23
+ o << hsl.map { |v| v ? ('%7.3f ' % v) : ' - ' }
24
+ o << %w(H: S: B:)
25
+ o << hsb.map { |v| v ? ('%7.3f ' % v) : ' - ' }
26
+
27
+ o << 3.times.map { (ostwald_color ? ostwald_color.terminal_background + " \e[0m " : ' ') }
28
+ o << %w(C: W: K:)
29
+ o << ostwald_cwk.map { |v| '%1.3f ' % v }
30
+
31
+ o.transpose.map(&:join).join("\n")
32
+ end
33
+
34
+ end
@@ -0,0 +1,65 @@
1
+ class RGB
2
+
3
+ #----------------------------------------------------------------------#
4
+ # Class Methods #
5
+ #----------------------------------------------------------------------#
6
+
7
+ class << self
8
+
9
+ # Returns CSS named colors, as per CSS Color Module Level 4.
10
+ #
11
+ # Optional selector argument can be:
12
+ # - String or Regexp (to select by name)
13
+ # - RGB color (to select by color)
14
+ # - Integer (to select by index).
15
+ # Selection by name (string or regular expression) is case-insensitive by default.
16
+ #
17
+ # @example No Selector
18
+ # # All colors
19
+ # RGB.css
20
+ #
21
+ # # Pastels
22
+ # RGB.css.select { |c| c.ostwald_cwk[1] > 0.6 }
23
+ # @example Select by Name
24
+ # # Exact name
25
+ # RGB.css 'pink'
26
+ #
27
+ # # Regular expression
28
+ # RGB.css /pink|rose/
29
+ #
30
+ # @example Select by RGB color
31
+ # RGB.css RGB.hex('0ff')
32
+ def css(selector=nil)
33
+ @@css ||= load_gpl file: ( File.join File.dirname(__FILE__), 'palettes', 'css.gpl' ), freeze: true
34
+ case selector
35
+ when NilClass
36
+ @@css
37
+ when String
38
+ n = selector.downcase
39
+ @@css.select { |c| c.name == n }.first
40
+ when Regexp
41
+ r = Regexp.new selector.source, Regexp::IGNORECASE
42
+ @@css.select { |c| c.name =~ r }
43
+ when Integer
44
+ @@css[selector]
45
+ when self
46
+ @@css.select { |c| c == selector }
47
+ else
48
+ raise ArgumentError, 'Unsupported selector'
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+ #----------------------------------------------------------------------#
55
+ # Instance Methods #
56
+ #----------------------------------------------------------------------#
57
+
58
+ # Returns the object's RGB value in hexadecimal notation as used in CSS.
59
+ #
60
+ # Shortens to 3 digits when possible.
61
+ def css_hex
62
+ "##{hex true}"
63
+ end
64
+
65
+ end
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.7.0
4
+ version: 0.12.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-11 00:00:00.000000000 Z
11
+ date: 2020-09-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -21,22 +21,41 @@ files:
21
21
  - lib/redgreenblue/48bit.rb
22
22
  - lib/redgreenblue/base.rb
23
23
  - lib/redgreenblue/bgr24bit.rb
24
+ - lib/redgreenblue/cie_1931.rb
25
+ - lib/redgreenblue/cie_1976.rb
26
+ - lib/redgreenblue/gamma.rb
24
27
  - lib/redgreenblue/gif.rb
28
+ - lib/redgreenblue/gpl.rb
25
29
  - lib/redgreenblue/hex.rb
26
- - lib/redgreenblue/hsl_hsv.rb
30
+ - lib/redgreenblue/hsb.rb
31
+ - lib/redgreenblue/hsl.rb
32
+ - lib/redgreenblue/hsv.rb
33
+ - lib/redgreenblue/hsx_shared.rb
34
+ - lib/redgreenblue/inspect.rb
35
+ - lib/redgreenblue/int.rb
27
36
  - lib/redgreenblue/lazy.rb
37
+ - lib/redgreenblue/math.rb
28
38
  - lib/redgreenblue/misc.rb
29
- - lib/redgreenblue/nice.rb
39
+ - lib/redgreenblue/mix.rb
40
+ - lib/redgreenblue/name.rb
30
41
  - lib/redgreenblue/opt/philipshue.rb
31
42
  - lib/redgreenblue/os.rb
32
43
  - lib/redgreenblue/os/mac.rb
44
+ - lib/redgreenblue/ostwald.rb
45
+ - lib/redgreenblue/palettes/css.gpl
33
46
  - lib/redgreenblue/random.rb
34
47
  - lib/redgreenblue/rgb565.rb
48
+ - lib/redgreenblue/terminal.rb
35
49
  - lib/redgreenblue/version.rb
50
+ - lib/redgreenblue/view.rb
51
+ - lib/redgreenblue/web.rb
36
52
  homepage: https://github.com/lllisteu/redgreenblue
37
53
  licenses:
38
54
  - MIT
39
- metadata: {}
55
+ metadata:
56
+ homepage_uri: https://github.com/lllisteu/redgreenblue
57
+ changelog_uri: https://github.com/lllisteu/redgreenblue/blob/master/History.md
58
+ documentation_uri: https://www.rubydoc.info/gems/redgreenblue/RGB
40
59
  post_install_message:
41
60
  rdoc_options: []
42
61
  require_paths:
@@ -52,8 +71,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
71
  - !ruby/object:Gem::Version
53
72
  version: '0'
54
73
  requirements: []
55
- rubygems_version: 3.0.3
74
+ rubygems_version: 3.1.2
56
75
  signing_key:
57
76
  specification_version: 4
58
- summary: A simple Ruby library for handling RGB colors
77
+ summary: A simple Ruby library for handling RGB colors.
59
78
  test_files: []
@@ -1,59 +0,0 @@
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
@@ -1,9 +0,0 @@
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
- alias to_s inspect
8
-
9
- end