redgreenblue 0.12.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 28b5f73bd443540e199b93625df5c7447491b502573abe5aa14089f895fd2700
4
- data.tar.gz: c0a3c77946d5a4d0e6c43d5769385a6c5b72df52c9f7a4cfb52d09bdc50a5153
3
+ metadata.gz: 65191c26a6dcae9a6bb52abe182d376e0975b79672686120185c9a2b3d49e4e9
4
+ data.tar.gz: 3fb9dc542660f5edaec8283a2e74ec0687e369e05475bf6840344ca5cca0304f
5
5
  SHA512:
6
- metadata.gz: 20b4959b4e82fdf67b48dbff3a0c82713a960c56a5bed7e32a78782cab40f1fb9d0f71c2d2fe93cef887a3c97a2590133a85d68b8d57912d7ec0bc4a0817febe
7
- data.tar.gz: bdc13584341d62b90586cf768cd114079e223e64424e0e9e6c24e4398ae74746f2530ed7e2c7540071cf99a4df4afe3a2e7bdadd7c2d0d3dbc6e5d595047d7f7
6
+ metadata.gz: 4c0d6a47aa611822dba62189c14ebf38c0ef6315f08ac9b5eb9efe04b28487d2c95105c0e86536cb52fdc9ce36df445d060623a14e99f2dbab34df5e0d10e515
7
+ data.tar.gz: 756bdaa8c6859c685e51342ee8a6d34e7340ea0580bd79b4487443d97b04629ed1b7c8f1631bf3ac26a5570e6c243aba7f7b931243ea967f07d8437fa223d495
@@ -21,9 +21,9 @@ end
21
21
 
22
22
  inspect view lazy
23
23
 
24
- rgb565 bgr24bit gif terminal web gpl
24
+ rgb565 bgr24bit gif terminal web gpl mac
25
25
 
26
- mix misc random
26
+ match mix misc random
27
27
 
28
28
  os
29
29
 
@@ -28,7 +28,7 @@ class RGB
28
28
  cie_lch_ab_uv(type: :luv)
29
29
  end
30
30
 
31
- # Returns the object's color distance from another RGB object, according to the CIE 1976 delta E formula.
31
+ # Returns the difference between this color and another color, according to the CIE 1976 delta E formula.
32
32
  #
33
33
  # Based on:
34
34
  # - http://www.brucelindbloom.com/Eqn_DeltaE_CIE76.html
@@ -61,7 +61,7 @@ class RGB
61
61
 
62
62
  f = [ x / xr, y / yr, z / zr ].map { |v|
63
63
  if v > ( 216.0 / 24389 )
64
- v ** ( 1.0 / 3 ) # cube root
64
+ Math.cbrt v
65
65
  else
66
66
  ( 24389.0 / 27 * v + 16 ) / 116.0
67
67
  end
@@ -97,7 +97,7 @@ class RGB
97
97
 
98
98
  [
99
99
  l.round(8),
100
- c = ( ( v1 ** 2 + v2 ** 2) ** ( 1.0 / 2 ) ).round(8),
100
+ c = Math.hypot(v1, v2).round(8),
101
101
  c == 0 ? nil : ( Math.atan2(v2, v1) * 180.0 / Math::PI ).modulo(360).round(8)
102
102
  ]
103
103
  end
@@ -1,53 +1,73 @@
1
1
  class RGB
2
2
 
3
- # Creates a white RGB object.
4
- def self.white
5
- new(1,1,1)
6
- end
3
+ #----------------------------------------------------------------------#
4
+ # Class Methods #
5
+ #----------------------------------------------------------------------#
7
6
 
8
- # Creates a black RGB object.
9
- def self.black
10
- new(0,0,0)
11
- end
7
+ class << self
12
8
 
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
9
+ # Creates a white RGB object.
10
+ def white
11
+ new(1,1,1)
12
+ end
19
13
 
20
- # Alias gray for grey.
21
- self.singleton_class.send(:alias_method, :gray, :grey)
14
+ # Creates a black RGB object.
15
+ def black
16
+ new(0,0,0)
17
+ end
22
18
 
23
- # Creates a pure red RGB object.
24
- def self.red
25
- new(1,0,0)
26
- end
19
+ # Creates a grey RGB object. Defaults to lightness 0.5, a middle grey. Black equals grey(0), white equals grey(1).
20
+ #
21
+ # ::gray is an alias for ::grey.
22
+ def grey(lightness=0.5)
23
+ new(lightness, lightness, lightness)
24
+ end
27
25
 
28
- # Creates a pure green RGB object.
29
- def self.green
30
- new(0,1,0)
31
- end
26
+ # Alias gray for grey.
27
+ alias gray grey
32
28
 
33
- # Creates a pure blue RGB object.
34
- def self.blue
35
- new(0,0,1)
36
- end
29
+ # Creates a pure red RGB object.
30
+ def red
31
+ new(1,0,0)
32
+ end
37
33
 
38
- # Creates a yellow RGB object.
39
- def self.yellow
40
- new(1,1,0)
41
- end
34
+ # Creates a pure green RGB object.
35
+ def green
36
+ new(0,1,0)
37
+ end
42
38
 
43
- # Creates a cyan RGB object.
44
- def self.cyan
45
- new(0,1,1)
46
- end
39
+ # Creates a pure blue RGB object.
40
+ def blue
41
+ new(0,0,1)
42
+ end
43
+
44
+ # Creates a yellow RGB object.
45
+ def yellow
46
+ new(1,1,0)
47
+ end
48
+
49
+ # Creates a cyan RGB object.
50
+ def cyan
51
+ new(0,1,1)
52
+ end
53
+
54
+ # Creates a magenta RGB object.
55
+ def magenta
56
+ new(1,0,1)
57
+ end
58
+
59
+ # Returns the 8 corners of the RGB cube.
60
+ def corners
61
+ [ black, red, yellow, green, cyan, blue, magenta, white ]
62
+ end
63
+
64
+ # Returns the centre of the RGB cube.
65
+ def centre
66
+ grey
67
+ end
68
+
69
+ alias center centre
47
70
 
48
- # Creates a magenta RGB object.
49
- def self.magenta
50
- new(1,0,1)
51
71
  end
52
72
 
53
73
  end
@@ -0,0 +1,8 @@
1
+ class RGB
2
+
3
+ # Returns the color in the format used by AppleScript (a 48-bit RGB triplet).
4
+ def applescript
5
+ "{%i, %i, %i}" % rrggbb
6
+ end
7
+
8
+ end
@@ -0,0 +1,21 @@
1
+ class RGB
2
+
3
+ # Matches this color to a set of colors by calculating their euclidean distance.
4
+ #
5
+ # Returns the given set of colors with their distance from this color, sorted by distance (nearest color first).
6
+ # @example What is nearer: red, grey or white?
7
+ # RGB.hex('f9c').match_distance [RGB.red, RGB.grey, RGB.white]
8
+ def match_distance(others)
9
+ others.map { |c| [ c, distance(c) ] }.sort_by { |a| a[1] }
10
+ end
11
+
12
+ # Matches this color to a set of colors using the CIE 1976 delta E formula.
13
+ #
14
+ # Returns the given set of colors with their difference from this color, sorted by difference (nearest color first).
15
+ # @example Find the 3 nearest CSS named colors
16
+ # RGB.hex('f9c').match_de76(RGB.css).first(3)
17
+ def match_de76(others)
18
+ others.map { |c| [ c, de76(c) ] }.sort_by { |a| a[1] }
19
+ end
20
+
21
+ end
@@ -3,7 +3,7 @@ class RGB
3
3
  # On Mac OS, shows the color picker to choose a color for the RGB object.
4
4
  # Not available on other platforms.
5
5
  def pick
6
- result = RGB.mac_choose(rrggbb)
6
+ result = RGB.mac_choose(self)
7
7
  if result
8
8
  self.rrggbb = result
9
9
  end
@@ -14,7 +14,7 @@ class RGB
14
14
  #
15
15
  # If no default color is specified, the picker defaults to a middle grey.
16
16
  def self.pick(default_color=RGB.new)
17
- result = RGB.mac_choose(default_color.rrggbb)
17
+ result = RGB.mac_choose(default_color)
18
18
  if result
19
19
  RGB.rrggbb result
20
20
  else
@@ -32,7 +32,7 @@ class RGB
32
32
  #
33
33
  # Applescript command documented here:
34
34
  # Standard Additions -> User Interaction -> choose color
35
- def self.mac_choose(color)
35
+ def self.mac_choose(default_color)
36
36
 
37
37
  app = case ENV['TERM_PROGRAM']
38
38
  when /iTerm\.app/
@@ -44,7 +44,7 @@ class RGB
44
44
  script = <<~ENDSCRIPT
45
45
  tell application "#{app}"
46
46
  try
47
- return choose color default color { #{color[0]}, #{color[1]}, #{color[2]} }
47
+ return choose color default color #{default_color.applescript}
48
48
  on error
49
49
  return ""
50
50
  end try
@@ -1,7 +1,7 @@
1
1
  class RGB
2
2
 
3
3
  # redgreenblue version.
4
- VERSION = '0.12.0'
4
+ VERSION = '0.13.0'
5
5
 
6
6
  # Returns RGB::VERSION.
7
7
  def self.version
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.12.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: 2020-09-08 00:00:00.000000000 Z
11
+ date: 2020-11-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -34,6 +34,8 @@ files:
34
34
  - lib/redgreenblue/inspect.rb
35
35
  - lib/redgreenblue/int.rb
36
36
  - lib/redgreenblue/lazy.rb
37
+ - lib/redgreenblue/mac.rb
38
+ - lib/redgreenblue/match.rb
37
39
  - lib/redgreenblue/math.rb
38
40
  - lib/redgreenblue/misc.rb
39
41
  - lib/redgreenblue/mix.rb