redgreenblue 0.8.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -1,5 +1,60 @@
1
1
  class RGB
2
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
+
3
58
  # Returns the object's RGB value in hexadecimal notation as used in CSS.
4
59
  #
5
60
  # Shortens to 3 digits when possible.
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.8.0
4
+ version: 0.13.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-12-20 00:00:00.000000000 Z
11
+ date: 2020-11-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -21,26 +21,43 @@ files:
21
21
  - lib/redgreenblue/48bit.rb
22
22
  - lib/redgreenblue/base.rb
23
23
  - lib/redgreenblue/bgr24bit.rb
24
- - lib/redgreenblue/cie.rb
24
+ - lib/redgreenblue/cie_1931.rb
25
+ - lib/redgreenblue/cie_1976.rb
26
+ - lib/redgreenblue/gamma.rb
25
27
  - lib/redgreenblue/gif.rb
28
+ - lib/redgreenblue/gpl.rb
26
29
  - lib/redgreenblue/hex.rb
27
- - 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
28
34
  - lib/redgreenblue/inspect.rb
35
+ - lib/redgreenblue/int.rb
29
36
  - lib/redgreenblue/lazy.rb
37
+ - lib/redgreenblue/mac.rb
38
+ - lib/redgreenblue/match.rb
39
+ - lib/redgreenblue/math.rb
30
40
  - lib/redgreenblue/misc.rb
31
41
  - lib/redgreenblue/mix.rb
42
+ - lib/redgreenblue/name.rb
32
43
  - lib/redgreenblue/opt/philipshue.rb
33
44
  - lib/redgreenblue/os.rb
34
45
  - lib/redgreenblue/os/mac.rb
46
+ - lib/redgreenblue/ostwald.rb
47
+ - lib/redgreenblue/palettes/css.gpl
35
48
  - lib/redgreenblue/random.rb
36
49
  - lib/redgreenblue/rgb565.rb
37
50
  - lib/redgreenblue/terminal.rb
38
51
  - lib/redgreenblue/version.rb
52
+ - lib/redgreenblue/view.rb
39
53
  - lib/redgreenblue/web.rb
40
54
  homepage: https://github.com/lllisteu/redgreenblue
41
55
  licenses:
42
56
  - MIT
43
- metadata: {}
57
+ metadata:
58
+ homepage_uri: https://github.com/lllisteu/redgreenblue
59
+ changelog_uri: https://github.com/lllisteu/redgreenblue/blob/master/History.md
60
+ documentation_uri: https://www.rubydoc.info/gems/redgreenblue/RGB
44
61
  post_install_message:
45
62
  rdoc_options: []
46
63
  require_paths:
@@ -56,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
73
  - !ruby/object:Gem::Version
57
74
  version: '0'
58
75
  requirements: []
59
- rubygems_version: 3.0.6
76
+ rubygems_version: 3.1.2
60
77
  signing_key:
61
78
  specification_version: 4
62
79
  summary: A simple Ruby library for handling RGB colors.
@@ -1,62 +0,0 @@
1
- class RGB
2
-
3
- # Returns CIE 1931 XYZ values for the RGB object.
4
- #
5
- # Based on:
6
- # - http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html
7
- # - https://en.wikipedia.org/wiki/CIE_1931_color_space
8
- # sRGB to XYZ matrix for D65 reference white by Bruce Lindbloom:
9
- # - http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
10
- def cie_xyz
11
- r, g, b = expanded_srgb_values
12
- [
13
- r * 0.4124564 + g * 0.3575761 + b * 0.1804375,
14
- r * 0.2126729 + g * 0.7151522 + b * 0.0721750,
15
- r * 0.0193339 + g * 0.1191920 + b * 0.9503041
16
- ].map { |v| v.round(6) }
17
- end
18
-
19
- alias xyz cie_xyz
20
-
21
- # Returns CIE 1931 xyY values for the RGB object.
22
- #
23
- # Based on:
24
- # - https://en.wikipedia.org/wiki/CIE_1931_color_space
25
- # - http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_xyY.html
26
- # - https://ninedegreesbelow.com/photography/xyz-rgb.html
27
- def cie_xyy
28
- x, y, z = xyz
29
- [
30
- x / ( x + y + z ),
31
- y / ( x + y + z ),
32
- y
33
- ].map { |v| v.round(6) }
34
- end
35
-
36
- alias xyy cie_xyy
37
-
38
- # Returns CIE 1931 xy values for the RGB object.
39
- def cie_xy
40
- cie_xyy[0..1]
41
- end
42
-
43
- alias xy cie_xy
44
-
45
- private
46
-
47
- # Returns gamma-expanded (inverse-companded) RGB values for sRGB.
48
- #
49
- # Based on:
50
- # - https://en.wikipedia.org/wiki/SRGB
51
- # - http://www.brucelindbloom.com/Eqn_RGB_to_XYZ.html
52
- def expanded_srgb_values
53
- values.map { |v|
54
- if v <= 0.04045
55
- v / 12.92
56
- else
57
- ( ( v + 0.055 ) / 1.055 ) ** 2.4
58
- end
59
- }
60
- end
61
-
62
- end
@@ -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