redgreenblue 0.13.0 → 0.14.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: 65191c26a6dcae9a6bb52abe182d376e0975b79672686120185c9a2b3d49e4e9
4
- data.tar.gz: 3fb9dc542660f5edaec8283a2e74ec0687e369e05475bf6840344ca5cca0304f
3
+ metadata.gz: a61701a3122fc8e8b4f4352142862f7e3d63ed317cea1321cfe1bafd5d697a42
4
+ data.tar.gz: 4a76d444f8080acb74656963654375e975eb91eb0068f6c4012e9233daf58bb7
5
5
  SHA512:
6
- metadata.gz: 4c0d6a47aa611822dba62189c14ebf38c0ef6315f08ac9b5eb9efe04b28487d2c95105c0e86536cb52fdc9ce36df445d060623a14e99f2dbab34df5e0d10e515
7
- data.tar.gz: 756bdaa8c6859c685e51342ee8a6d34e7340ea0580bd79b4487443d97b04629ed1b7c8f1631bf3ac26a5570e6c243aba7f7b931243ea967f07d8437fa223d495
6
+ metadata.gz: 502bc188625f8df6fc3a55fa53454728602843ddd39f61c8199009f0e55c1fbd38a496d27c889c565b578ae85dd9cd051a457deec87979374d89006833f37707
7
+ data.tar.gz: b811b0e1bc0bd366406be48eead2f11328c8304eee6eb34192bc92c40c31a4683dc5771a3944650749707df7e5a40e330d64c02b7f7818c0064dca5253ffe019
data/lib/redgreenblue.rb CHANGED
@@ -11,7 +11,7 @@ end
11
11
 
12
12
  hsl hsv hsb
13
13
 
14
- ostwald
14
+ ostwald hwb
15
15
 
16
16
  gamma
17
17
 
@@ -64,13 +64,16 @@ class RGB
64
64
  end
65
65
  end
66
66
 
67
- private
68
-
67
+ # Returns a header for a .gpl file (Gimp color palette). Includes an optional name and number of columns.
68
+ #
69
+ # @example
70
+ # RGB.gpl_header('Spring')
71
+ #
69
72
  # Reverse-engineered from:
70
73
  # - https://github.com/GNOME/gimp/blob/5d79fba8238a27b8691556489898d33b3fa0dda0/app/core/gimppalette-load.c
71
- def gpl_header(name, columns=nil)
74
+ def gpl_header(name=nil, columns: nil)
72
75
  "GIMP Palette\n" +
73
- "Name: #{name}\n" +
76
+ ( name ? "Name: #{name}\n" : '' ) +
74
77
  ( columns ? "Columns: #{columns}\n" : '' )
75
78
  end
76
79
 
@@ -0,0 +1,14 @@
1
+ class RGB
2
+
3
+ # Returns the color's hue (0..360), whiteness (0..1), and blackness (0..1), as defined by the HWB color model.
4
+ #
5
+ # For achromatic colors, hue is nil.
6
+ #
7
+ # Based on:
8
+ # - http://alvyray.com/Papers/CG/HWB_JGTv208.pdf (PDF)
9
+ # - https://en.wikipedia.org/wiki/HWB_color_model
10
+ def hwb
11
+ [ hsv_h, cwk[1,2] ].flatten
12
+ end
13
+
14
+ end
@@ -32,12 +32,12 @@ class RGB
32
32
  # Based on:
33
33
  # - Color for the Sciences, pp. 575–591
34
34
  # - https://lirias.kuleuven.be/retrieve/306124 (PDF)
35
- def ostwald_cwk
35
+ def ostwald_cwk(round: true)
36
36
  [
37
37
  color_portion = values.max - values.min,
38
38
  white_portion = values.min,
39
39
  1 - color_portion - white_portion
40
- ]
40
+ ].map { |v| round ? v.round(8) : v }
41
41
  end
42
42
 
43
43
  alias cwk ostwald_cwk
@@ -1,7 +1,7 @@
1
1
  class RGB
2
2
 
3
3
  # redgreenblue version.
4
- VERSION = '0.13.0'
4
+ VERSION = '0.14.0'
5
5
 
6
6
  # Returns RGB::VERSION.
7
7
  def self.version
@@ -62,4 +62,48 @@ class RGB
62
62
  "##{hex true}"
63
63
  end
64
64
 
65
+ # Returns the names of CSS named colors identical to this color.
66
+ # @example
67
+ # RGB.hex('f0f').css_names
68
+ def css_names
69
+ RGB.css(self).map &:name
70
+ end
71
+
72
+ # Returns the color's relative luminance, as defined by the Web Content Accessibility Guidelines (WCAG) 2.0.
73
+ #
74
+ # This is different from the Y component of CIE 1931 XYZ.
75
+ #
76
+ # Based on:
77
+ # - https://www.w3.org/TR/WCAG20/#relativeluminancedef
78
+ def wcag20_luminance(round: true)
79
+ if color_space == 'sRGB'
80
+ values.map { |v|
81
+ if v <= 0.03928
82
+ v / 12.92
83
+ else
84
+ ( ( v + 0.055 ) / 1.055 ) ** 2.4
85
+ end
86
+ }.zip( [0.2126, 0.7152, 0.0722] ).map { |c,f|
87
+ c * f
88
+ }.inject(:+).instance_eval { |v| round ? v.round(8) : v }
89
+ else
90
+ raise "can not calculate luminance for color space '#{color_space}'"
91
+ end
92
+ end
93
+
94
+ # Returns the contrast ratio for this color and another color, as defined by the Web Content Accessibility Guidelines (WCAG) 2.0.
95
+ #
96
+ # Based on:
97
+ # - https://www.w3.org/TR/WCAG20/#contrast-ratiodef
98
+ def wcag20_contrast_ratio(another, round: true)
99
+ luminances = [
100
+ wcag20_luminance(round: false),
101
+ another.wcag20_luminance(round: false)
102
+ ].sort.reverse
103
+
104
+ contrast_ratio = ( luminances[0] + 0.05 ) / ( luminances[1] + 0.05 )
105
+
106
+ round ? contrast_ratio.round(8) : contrast_ratio
107
+ end
108
+
65
109
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redgreenblue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - lllist.eu
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-09 00:00:00.000000000 Z
11
+ date: 2021-05-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description:
14
- email:
13
+ description:
14
+ email:
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
@@ -31,6 +31,7 @@ files:
31
31
  - lib/redgreenblue/hsl.rb
32
32
  - lib/redgreenblue/hsv.rb
33
33
  - lib/redgreenblue/hsx_shared.rb
34
+ - lib/redgreenblue/hwb.rb
34
35
  - lib/redgreenblue/inspect.rb
35
36
  - lib/redgreenblue/int.rb
36
37
  - lib/redgreenblue/lazy.rb
@@ -58,7 +59,7 @@ metadata:
58
59
  homepage_uri: https://github.com/lllisteu/redgreenblue
59
60
  changelog_uri: https://github.com/lllisteu/redgreenblue/blob/master/History.md
60
61
  documentation_uri: https://www.rubydoc.info/gems/redgreenblue/RGB
61
- post_install_message:
62
+ post_install_message:
62
63
  rdoc_options: []
63
64
  require_paths:
64
65
  - lib
@@ -73,8 +74,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
74
  - !ruby/object:Gem::Version
74
75
  version: '0'
75
76
  requirements: []
76
- rubygems_version: 3.1.2
77
- signing_key:
77
+ rubygems_version: 3.1.4
78
+ signing_key:
78
79
  specification_version: 4
79
80
  summary: A simple Ruby library for handling RGB colors.
80
81
  test_files: []