color_contrast_calc 0.5.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,11 +12,14 @@ module ColorContrastCalc
12
12
  module Utils
13
13
  using Shim unless //.respond_to? :match?
14
14
 
15
+ MIN_OPACITY = 0
16
+ MAX_OPACITY = 1.0
17
+
15
18
  HSL_UPPER_LIMIT = [360, 100, 100].freeze
16
19
 
17
20
  private_constant :HSL_UPPER_LIMIT
18
21
 
19
- HEX_RE = /\A#?[0-9a-f]{3}([0-9a-f]{3})?\z/i
22
+ HEX_RE = /\A#?[0-9a-f]{3}([0-9a-f]{3})?\z/i.freeze
20
23
 
21
24
  private_constant :HEX_RE
22
25
 
@@ -196,11 +199,9 @@ module ColorContrastCalc
196
199
  # @return [true, false] true if a valid HSL value is passed
197
200
 
198
201
  def self.valid_hsl?(hsl)
199
- return false unless hsl.length == 3
200
- hsl.each_with_index do |c, i|
201
- return false if !c.is_a?(Numeric) || c < 0 || c > HSL_UPPER_LIMIT[i]
202
+ hsl.length == 3 && hsl.each_with_index.all? do |c, i|
203
+ c.is_a?(Numeric) && c >= 0 && c <= HSL_UPPER_LIMIT[i]
202
204
  end
203
- true
204
205
  end
205
206
 
206
207
  ##
@@ -236,6 +237,89 @@ module ColorContrastCalc
236
237
  def self.uppercase?(str)
237
238
  !/[[:lower:]]/.match?(str)
238
239
  end
240
+
241
+ module Hwb
242
+ HWB_UPPER_LIMIT = [360, 100, 100].freeze
243
+
244
+ def normalize_hwb(hwb)
245
+ # https://www.w3.org/TR/2019/WD-css-color-4-20191105/
246
+ h, w, b = hwb
247
+
248
+ achromatic_percent = w + b
249
+ denominator = achromatic_percent > 100 ? achromatic_percent : 100
250
+
251
+ normalized_w = w.to_f / denominator
252
+ normalized_b = b.to_f / denominator
253
+
254
+ [h, normalized_w, normalized_b]
255
+ end
256
+
257
+ private :normalize_hwb
258
+
259
+ ##
260
+ # Convert an HWB value to an RGB value.
261
+ #
262
+ # @param hwb [Array<Float>] HWB value represented as an array of numbers
263
+ # @return [Array<Integer>] RGB value represented as an array of integers
264
+
265
+ def hwb_to_rgb(hwb)
266
+ hue, white, black = normalize_hwb(hwb)
267
+ rgb = Utils.hsl_to_rgb([hue, 100, 50])
268
+
269
+ rgb.map do |c|
270
+ ((c * (1.0 - white - black)) + white * 255).round
271
+ end
272
+ end
273
+
274
+ ##
275
+ # Convert an HWB value to hex color code.
276
+ #
277
+ # @param hwb [Array<Float>] HWB value represented as an array of numbers
278
+ # @return [String] Hex color code such as "#ffff00"
279
+
280
+ def hwb_to_hex(hwb)
281
+ rgb_to_hex(hwb_to_rgb(hwb))
282
+ end
283
+
284
+ ##
285
+ # Convert an RGB value to an HWB value.
286
+ #
287
+ # @param rgb [Array<Integer>] RGB value represented as an array of
288
+ # integers
289
+ # @return [Array<Float>] HWB value represented as an array of numbers
290
+
291
+ def rgb_to_hwb(rgb)
292
+ # https://www.w3.org/TR/2020/WD-css-color-4-20201112/
293
+ hsl = Utils.rgb_to_hsl(rgb)
294
+ white = rgb.min
295
+ black = 255 - rgb.max
296
+ [hsl[0], white * 100 / 255.0, black * 100 / 255.0]
297
+ end
298
+
299
+ ##
300
+ # Convert hex color code to an HWB value.
301
+ #
302
+ # @param hex_code [String] Hex color code such as "#ffff00"
303
+ # @return [Array<Float>] HWB value represented as an array of numbers
304
+
305
+ def hex_to_hwb(hex_code)
306
+ rgb_to_hwb(Utils.hex_to_rgb(hex_code))
307
+ end
308
+
309
+ ##
310
+ # Check if a given array is a valid representation of HWB color.
311
+ #
312
+ # @param hwb [Array<Float>] HWB value represented as an array of numbers
313
+ # @return [true, false] true if a valid HWB value is passed
314
+
315
+ def valid_hwb?(hwb)
316
+ hwb.length == 3 && hwb.each_with_index.all? do |c, i|
317
+ c.is_a?(Numeric) && c >= 0 && c <= HWB_UPPER_LIMIT[i]
318
+ end
319
+ end
320
+ end
321
+
322
+ extend Hwb
239
323
  end
240
324
 
241
325
  ##
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ColorContrastCalc
4
- VERSION = '0.5.0'
4
+ VERSION = '0.9.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: color_contrast_calc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - HASHIMOTO, Naoki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-30 00:00:00.000000000 Z
11
+ date: 2021-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,70 +16,70 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.15'
19
+ version: '2.1'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.15'
26
+ version: '2.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '13.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '13.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '3.10'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.0'
54
+ version: '3.10'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rubocop
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 0.49.1
61
+ version: '1.7'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 0.49.1
68
+ version: '1.7'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: yard
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 0.9.9
75
+ version: '0.9'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 0.9.9
82
+ version: '0.9'
83
83
  description:
84
84
  email:
85
85
  - hashimoto.naoki@gmail.com
@@ -112,12 +112,15 @@ files:
112
112
  - lib/color_contrast_calc.rb
113
113
  - lib/color_contrast_calc/checker.rb
114
114
  - lib/color_contrast_calc/color.rb
115
+ - lib/color_contrast_calc/color_function_parser.rb
115
116
  - lib/color_contrast_calc/converter.rb
116
117
  - lib/color_contrast_calc/data/color_keywords.json
117
118
  - lib/color_contrast_calc/deprecated.rb
119
+ - lib/color_contrast_calc/invalid_color_representation_error.rb
118
120
  - lib/color_contrast_calc/shim.rb
119
121
  - lib/color_contrast_calc/sorter.rb
120
122
  - lib/color_contrast_calc/threshold_finder.rb
123
+ - lib/color_contrast_calc/transparency_calc.rb
121
124
  - lib/color_contrast_calc/utils.rb
122
125
  - lib/color_contrast_calc/version.rb
123
126
  homepage: https://github.com/nico-hn/color_contrast_calc_rb/
@@ -132,15 +135,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
135
  requirements:
133
136
  - - ">="
134
137
  - !ruby/object:Gem::Version
135
- version: '2.2'
138
+ version: '2.4'
136
139
  required_rubygems_version: !ruby/object:Gem::Requirement
137
140
  requirements:
138
141
  - - ">="
139
142
  - !ruby/object:Gem::Version
140
143
  version: '0'
141
144
  requirements: []
142
- rubyforge_project:
143
- rubygems_version: 2.6.11
145
+ rubygems_version: 3.1.4
144
146
  signing_key:
145
147
  specification_version: 4
146
148
  summary: Utility that helps you choose colors with sufficient contrast, WCAG 2.0 in