color_contrast_calc 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.rubocop.yml +8 -0
- data/.travis.yml +3 -2
- data/README.ja.md +28 -2
- data/README.md +28 -2
- data/color_contrast_calc.gemspec +1 -1
- data/examples/color_instance.rb +2 -1
- data/lib/color_contrast_calc.rb +21 -1
- data/lib/color_contrast_calc/color.rb +33 -15
- data/lib/color_contrast_calc/color_function_parser.rb +245 -0
- data/lib/color_contrast_calc/invalid_color_representation_error.rb +38 -0
- data/lib/color_contrast_calc/utils.rb +1 -1
- data/lib/color_contrast_calc/version.rb +1 -1
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5fd16bbd48389db33f0f8cca8b247ff7d5f9a2b9722d8ec75bc700eda5a3ad31
|
4
|
+
data.tar.gz: ce493824b5a6fa1103a475073ca00bbd92e0b738863cfd338ebf6b2f8f419fd5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7174b51afe73c2a632afa7d583e8f60a738891fb4f5a7e5c5deb5c50b849f7236fbf14440be3a94f80d3c077c0ad6a79d886ab8de7c3a30524c6184e98658741
|
7
|
+
data.tar.gz: 595e1f552a82d94bad86b93c7a5cf656634bdfb23cc88885789b850f142f09f5f5d673135c5658516b40267deb2fe2863f35a318c31304285e3921ce40e71e76
|
data/.rubocop.yml
CHANGED
@@ -2,7 +2,15 @@ AllCops:
|
|
2
2
|
TargetRubyVersion: 2.2
|
3
3
|
Layout/SpaceInsideBlockBraces:
|
4
4
|
SpaceBeforeBlockParameters: false
|
5
|
+
Layout/EmptyLineAfterGuardClause:
|
6
|
+
Enabled: false
|
5
7
|
Metrics/BlockLength:
|
6
8
|
Exclude: ["spec/**/*", "test/**/*"]
|
7
9
|
Documentation:
|
8
10
|
Enabled: false
|
11
|
+
Style/FormatStringToken:
|
12
|
+
Enabled: false
|
13
|
+
Style/AccessModifierDeclarations:
|
14
|
+
EnforcedStyle: inline
|
15
|
+
Naming/UncommunicativeMethodParamName:
|
16
|
+
MinNameLength: 1
|
data/.travis.yml
CHANGED
data/README.ja.md
CHANGED
@@ -47,7 +47,8 @@ Or install it yourself as:
|
|
47
47
|
require 'color_contrast_calc'
|
48
48
|
|
49
49
|
# Create an instance of Color from a hex code
|
50
|
-
# (You can pass 'red'
|
50
|
+
# (You can pass 'red', [255, 0, 0], 'rgb(255, 0, 0)' or 'hsl(0deg, 100%, 50%)'
|
51
|
+
# instead of '#ff0000')
|
51
52
|
red = ColorContrastCalc.color_from('#ff0000')
|
52
53
|
puts red.class
|
53
54
|
puts red.name
|
@@ -71,6 +72,28 @@ red
|
|
71
72
|
|
72
73
|
### 例1: 2つの色のコントラスト比を計算する
|
73
74
|
|
75
|
+
#### 1.1: 最も簡便なやり方
|
76
|
+
|
77
|
+
2色間のコントラスト比を計算するために`ColorContrastCalc`のクラスメソッドである`.contrast_ratio()`が利用可能です。
|
78
|
+
|
79
|
+
例えば黄色と黒のコントラスト比をコマンドラインで計算したい場合、次のように出来ます:
|
80
|
+
|
81
|
+
```bash
|
82
|
+
$ ruby -rcolor_contrast_calc -e 'puts ColorContrastCalc.contrast_ratio("#ff0", "#000")'
|
83
|
+
19.555999999999997
|
84
|
+
```
|
85
|
+
|
86
|
+
もしくは
|
87
|
+
|
88
|
+
```bash
|
89
|
+
$ ruby -rcolor_contrast_calc -e 'puts ColorContrastCalc.contrast_ratio("rgb(255, 255, 0)", "black")'
|
90
|
+
19.555999999999997
|
91
|
+
```
|
92
|
+
|
93
|
+
(黄色を表すためには"hsl(60deg, 100%, 50%)", "#FFFF00", [255, 255, 0]等も使えます。)
|
94
|
+
|
95
|
+
#### 1.2: Colorクラスのインスタンスを使っての計算
|
96
|
+
|
74
97
|
例えば黄色と黒のコントラスト比を計算したい場合、
|
75
98
|
次のコードを`yellow_black_contrast.rb`として保存し:
|
76
99
|
|
@@ -95,7 +118,9 @@ The contrast ratio between yellow and black is 19.5560
|
|
95
118
|
The contrast ratio between #ffff00 and #000000 is 19.5560
|
96
119
|
```
|
97
120
|
|
98
|
-
|
121
|
+
#### 1.3: RGBを表す低レベルの値からの計算
|
122
|
+
|
123
|
+
2色の16進数カラーコードあるいはRGB値からコントラスト比を計算することも可能です。
|
99
124
|
|
100
125
|
次のコードを `yellow_black_hex_contrast.rb`として保存し:
|
101
126
|
|
@@ -116,6 +141,7 @@ puts "Contrast level: #{level}"
|
|
116
141
|
以下のように実行します:
|
117
142
|
|
118
143
|
```bash
|
144
|
+
$ ruby yellow_black_hex_contrast.rb
|
119
145
|
Contrast ratio between yellow and black: 19.555999999999997
|
120
146
|
Contrast level: AAA
|
121
147
|
```
|
data/README.md
CHANGED
@@ -46,7 +46,8 @@ Save the following code as `color_instance.rb`:
|
|
46
46
|
require 'color_contrast_calc'
|
47
47
|
|
48
48
|
# Create an instance of Color from a hex code
|
49
|
-
# (You can pass 'red'
|
49
|
+
# (You can pass 'red', [255, 0, 0], 'rgb(255, 0, 0)' or 'hsl(0deg, 100%, 50%)'
|
50
|
+
# instead of '#ff0000')
|
50
51
|
red = ColorContrastCalc.color_from('#ff0000')
|
51
52
|
puts red.class
|
52
53
|
puts red.name
|
@@ -70,6 +71,28 @@ red
|
|
70
71
|
|
71
72
|
### Example 1: Calculate the contrast ratio between two colors
|
72
73
|
|
74
|
+
#### 1.1: The easiest way
|
75
|
+
|
76
|
+
To calculate the contrast ratio between two colors, a class method of `ColorContrastCalc`, `.contrast_ratio()` is available.
|
77
|
+
|
78
|
+
For example, if you want calculate the contrast ratio between yellow and black at the command line, you can do as follows:
|
79
|
+
|
80
|
+
```bash
|
81
|
+
$ ruby -rcolor_contrast_calc -e 'puts ColorContrastCalc.contrast_ratio("#ff0", "#000")'
|
82
|
+
19.555999999999997
|
83
|
+
```
|
84
|
+
|
85
|
+
Or
|
86
|
+
|
87
|
+
```bash
|
88
|
+
$ ruby -rcolor_contrast_calc -e 'puts ColorContrastCalc.contrast_ratio("rgb(255, 255, 0)", "black")'
|
89
|
+
19.555999999999997
|
90
|
+
```
|
91
|
+
|
92
|
+
(To represent a yellow, you can also use "hsl(60deg, 100%, 50%)", "#FFFF00", [255, 255, 0]...)
|
93
|
+
|
94
|
+
#### 1.2: Calculate using Color instances
|
95
|
+
|
73
96
|
If you want to calculate the contrast ratio between yellow and black,
|
74
97
|
save the following code as `yellow_black_contrast.rb`:
|
75
98
|
|
@@ -94,7 +117,9 @@ The contrast ratio between yellow and black is 19.5560
|
|
94
117
|
The contrast ratio between #ffff00 and #000000 is 19.5560
|
95
118
|
```
|
96
119
|
|
97
|
-
|
120
|
+
#### 1.3: Calculate from low level RGB values
|
121
|
+
|
122
|
+
It is also possible to calculate the contrast ratio of two colors from
|
98
123
|
their hex color codes or RGB values.
|
99
124
|
|
100
125
|
Save the following code as `yellow_black_hex_contrast.rb`:
|
@@ -116,6 +141,7 @@ puts "Contrast level: #{level}"
|
|
116
141
|
Then execute the script:
|
117
142
|
|
118
143
|
```bash
|
144
|
+
$ ruby yellow_black_hex_contrast.rb
|
119
145
|
Contrast ratio between yellow and black: 19.555999999999997
|
120
146
|
Contrast level: AAA
|
121
147
|
```
|
data/color_contrast_calc.gemspec
CHANGED
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
|
24
24
|
spec.require_paths = ['lib']
|
25
25
|
|
26
|
-
spec.add_development_dependency 'bundler', '~>
|
26
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
27
27
|
spec.add_development_dependency 'rake', '~> 10.0'
|
28
28
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
29
29
|
spec.add_development_dependency 'rubocop', '~> 0.49.1'
|
data/examples/color_instance.rb
CHANGED
@@ -3,7 +3,8 @@ require 'color_contrast_calc'
|
|
3
3
|
require 'color_contrast_calc'
|
4
4
|
|
5
5
|
# Create an instance of Color from a hex code
|
6
|
-
# (You can pass 'red'
|
6
|
+
# (You can pass 'red', [255, 0, 0], 'rgb(255, 0, 0)' or 'hsl(0deg, 100%, 50%)'
|
7
|
+
# instead of '#ff0000')
|
7
8
|
red = ColorContrastCalc.color_from('#ff0000')
|
8
9
|
puts red.class
|
9
10
|
puts red.name
|
data/lib/color_contrast_calc.rb
CHANGED
@@ -17,7 +17,7 @@ module ColorContrastCalc
|
|
17
17
|
# as [255, 255, 0] or "#ffff00". +name+ is assigned to the returned
|
18
18
|
# instance.
|
19
19
|
# @param color_value [String, Array<Integer>] Name of a predefined
|
20
|
-
# color, hex color code or RGB value
|
20
|
+
# color, hex color code, rgb/hsl functions or RGB value
|
21
21
|
# @param name [String] Without specifying a name, a color keyword name
|
22
22
|
# (if exists) or the value of normalized hex color code is assigned
|
23
23
|
# to Color#name
|
@@ -52,6 +52,26 @@ module ColorContrastCalc
|
|
52
52
|
Sorter.sort(colors, color_order, key_mapper)
|
53
53
|
end
|
54
54
|
|
55
|
+
##
|
56
|
+
# Calculate the contrast ratio of given colors.
|
57
|
+
#
|
58
|
+
# The definition of contrast ratio is given at
|
59
|
+
# {https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef}
|
60
|
+
#
|
61
|
+
# Please note that this method may be slow, as it internally creates
|
62
|
+
# Color instances.
|
63
|
+
#
|
64
|
+
# @param color1 [String, Array<Integer>] RGB color given as a string or
|
65
|
+
# an array of integers. Yellow, for example, can be given as "#ffff00",
|
66
|
+
# "#ff0", "rgb(255, 255, 0)", "hsl(60deg, 100%, 50%)" or [255, 255, 0].
|
67
|
+
# @param color2 [String, Array<Integer>] RGB color given as a string or
|
68
|
+
# an array of integers.
|
69
|
+
# @return [Float] Contrast ratio
|
70
|
+
|
71
|
+
def self.contrast_ratio(color1, color2)
|
72
|
+
Color.as_color(color1).contrast_ratio_against(Color.as_color(color2))
|
73
|
+
end
|
74
|
+
|
55
75
|
##
|
56
76
|
# Return an array of named colors.
|
57
77
|
#
|
@@ -2,16 +2,13 @@
|
|
2
2
|
|
3
3
|
require 'color_contrast_calc/utils'
|
4
4
|
require 'color_contrast_calc/checker'
|
5
|
+
require 'color_contrast_calc/invalid_color_representation_error'
|
5
6
|
require 'color_contrast_calc/threshold_finder'
|
7
|
+
require 'color_contrast_calc/color_function_parser'
|
6
8
|
require 'color_contrast_calc/deprecated'
|
7
9
|
require 'json'
|
8
10
|
|
9
11
|
module ColorContrastCalc
|
10
|
-
##
|
11
|
-
# Error raised if creating a Color instance with invalid value.
|
12
|
-
|
13
|
-
class InvalidColorRepresentationError < StandardError; end
|
14
|
-
|
15
12
|
##
|
16
13
|
# Represent specific colors.
|
17
14
|
#
|
@@ -86,20 +83,22 @@ module ColorContrastCalc
|
|
86
83
|
# as [255, 255, 0] or "#ffff00". +name+ is assigned to the returned
|
87
84
|
# instance.
|
88
85
|
# @param color_value [String, Array<Integer>] Name of a predefined
|
89
|
-
# color, hex color code or RGB value
|
86
|
+
# color, hex color code, rgb/hsl functions or RGB value
|
90
87
|
# @param name [String] Without specifying a name, a color keyword name
|
91
88
|
# (if exists) or the value of normalized hex color code is assigned
|
92
89
|
# to Color#name
|
93
90
|
# @return [Color] Instance of Color
|
94
91
|
|
95
92
|
def color_from(color_value, name = nil)
|
96
|
-
error_message = 'A color should be given as an array or string.'
|
97
|
-
|
98
93
|
if !color_value.is_a?(String) && !color_value.is_a?(Array)
|
99
|
-
raise InvalidColorRepresentationError
|
94
|
+
raise InvalidColorRepresentationError.from_value(color_value)
|
100
95
|
end
|
101
96
|
|
102
97
|
return color_from_rgb(color_value, name) if color_value.is_a?(Array)
|
98
|
+
|
99
|
+
if /\A(?:rgb|hsl)/i =~ color_value
|
100
|
+
return color_from_func(color_value, name)
|
101
|
+
end
|
103
102
|
color_from_str(color_value, name)
|
104
103
|
end
|
105
104
|
|
@@ -127,10 +126,8 @@ module ColorContrastCalc
|
|
127
126
|
end
|
128
127
|
|
129
128
|
def color_from_rgb(rgb_value, name = nil)
|
130
|
-
error_message = 'An RGB value should be given in form of [r, g, b].'
|
131
|
-
|
132
129
|
unless Utils.valid_rgb?(rgb_value)
|
133
|
-
raise InvalidColorRepresentationError
|
130
|
+
raise InvalidColorRepresentationError.from_value(rgb_value)
|
134
131
|
end
|
135
132
|
|
136
133
|
hex_code = Utils.rgb_to_hex(rgb_value)
|
@@ -139,14 +136,23 @@ module ColorContrastCalc
|
|
139
136
|
|
140
137
|
private :color_from_rgb
|
141
138
|
|
142
|
-
def
|
143
|
-
|
139
|
+
def color_from_func(color_value, name = nil)
|
140
|
+
conv = ColorFunctionParser.parse(color_value)
|
141
|
+
if conv.scheme == ColorFunctionParser::Scheme::RGB
|
142
|
+
return color_from_rgb(conv.to_a, name || color_value)
|
143
|
+
end
|
144
|
+
|
145
|
+
from_hsl(conv.to_a, name || color_value)
|
146
|
+
end
|
147
|
+
|
148
|
+
private :color_from_func
|
144
149
|
|
150
|
+
def color_from_str(color_value, name = nil)
|
145
151
|
named_color = !name && List::NAME_TO_COLOR[color_value]
|
146
152
|
return named_color if named_color
|
147
153
|
|
148
154
|
unless Utils.valid_hex?(color_value)
|
149
|
-
raise InvalidColorRepresentationError
|
155
|
+
raise InvalidColorRepresentationError.from_value(color_value)
|
150
156
|
end
|
151
157
|
|
152
158
|
hex_code = Utils.normalize_hex(color_value)
|
@@ -294,6 +300,18 @@ module ColorContrastCalc
|
|
294
300
|
generate_new_color(Converter::Grayscale, ratio, name)
|
295
301
|
end
|
296
302
|
|
303
|
+
##
|
304
|
+
# Return a complementary color of the original color.
|
305
|
+
#
|
306
|
+
# @param name [String] You can name the color to be created.
|
307
|
+
# Without this option, the value of normalized hex color
|
308
|
+
# code is assigned instead.
|
309
|
+
# @return [Color] New complementary color
|
310
|
+
def complementary(name = nil)
|
311
|
+
minmax = rgb.minmax.reduce {|min, max| min + max }
|
312
|
+
self.class.new(rgb.map {|c| minmax - c }, name)
|
313
|
+
end
|
314
|
+
|
297
315
|
##
|
298
316
|
# Try to find a color who has a satisfying contrast ratio.
|
299
317
|
#
|
@@ -0,0 +1,245 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'strscan'
|
4
|
+
require 'stringio'
|
5
|
+
require 'color_contrast_calc/utils'
|
6
|
+
require 'color_contrast_calc/invalid_color_representation_error'
|
7
|
+
|
8
|
+
module ColorContrastCalc
|
9
|
+
##
|
10
|
+
# Module that converts RGB/HSL functions into data apt for calculation.
|
11
|
+
|
12
|
+
module ColorFunctionParser
|
13
|
+
##
|
14
|
+
# Define types of color functions.
|
15
|
+
|
16
|
+
module Scheme
|
17
|
+
RGB = 'rgb'
|
18
|
+
HSL = 'hsl'
|
19
|
+
end
|
20
|
+
|
21
|
+
##
|
22
|
+
# Hold information about a parsed RGB/HSL function.
|
23
|
+
#
|
24
|
+
# This class is intended to be used internally in ColorFunctionParser,
|
25
|
+
# so do not rely on the current class name and its interfaces.
|
26
|
+
# They may change in the future.
|
27
|
+
|
28
|
+
class Converter
|
29
|
+
##
|
30
|
+
# @!attribute [r] scheme
|
31
|
+
# @return [String] Type of function: 'rgb' or 'hsl'
|
32
|
+
# @!attribute [r] source
|
33
|
+
# @return [String] The original RGB/HSL function before the conversion
|
34
|
+
|
35
|
+
attr_reader :scheme, :source
|
36
|
+
|
37
|
+
##
|
38
|
+
# @private
|
39
|
+
#
|
40
|
+
# Parameters passed to this method is generated by
|
41
|
+
# ColorFunctionParser.parse() and the manual creation of
|
42
|
+
# instances of this class by end users is not expected.
|
43
|
+
|
44
|
+
def initialize(parsed_value, original_value)
|
45
|
+
@scheme = parsed_value[:scheme]
|
46
|
+
@params = parsed_value[:parameters]
|
47
|
+
@source = original_value
|
48
|
+
@normalized = normalize_params
|
49
|
+
end
|
50
|
+
|
51
|
+
def normalize_params
|
52
|
+
raise NotImplementedError, 'Overwrite the method in a subclass'
|
53
|
+
end
|
54
|
+
|
55
|
+
private :normalize_params
|
56
|
+
|
57
|
+
##
|
58
|
+
# Return the RGB value gained from a RGB/HSL function.
|
59
|
+
#
|
60
|
+
# @return [Array<Integer>] RGB value represented as an array
|
61
|
+
|
62
|
+
def rgb
|
63
|
+
raise NotImplementedError, 'Overwrite the method in a subclass'
|
64
|
+
end
|
65
|
+
|
66
|
+
##
|
67
|
+
# Return the parameters of a RGB/HSL function as an array of
|
68
|
+
# Integer/Float.
|
69
|
+
# The unit for H, S, L is assumed to be deg, %, % respectively.
|
70
|
+
#
|
71
|
+
# @return [Array<Integer, Float>] RGB/HSL value represented as an array
|
72
|
+
|
73
|
+
def to_a
|
74
|
+
@normalized
|
75
|
+
end
|
76
|
+
|
77
|
+
# @private
|
78
|
+
class Rgb < self
|
79
|
+
def normalize_params
|
80
|
+
@params.map do |param|
|
81
|
+
if param[:unit] == '%'
|
82
|
+
(param[:number] * 255.0 / 100).round
|
83
|
+
else
|
84
|
+
param[:number].to_i
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
alias rgb to_a
|
90
|
+
end
|
91
|
+
|
92
|
+
# @private
|
93
|
+
class Hsl < self
|
94
|
+
def normalize_params
|
95
|
+
@params.map do |param|
|
96
|
+
param[:number].to_f
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def rgb
|
101
|
+
Utils.hsl_to_rgb(to_a)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# @private
|
106
|
+
def self.create(parsed_value, original_value)
|
107
|
+
case parsed_value[:scheme]
|
108
|
+
when Scheme::RGB
|
109
|
+
Rgb.new(parsed_value, original_value)
|
110
|
+
when Scheme::HSL
|
111
|
+
Hsl.new(parsed_value, original_value)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
# @private
|
117
|
+
module TokenRe
|
118
|
+
SPACES = /\s+/.freeze
|
119
|
+
SCHEME = /(rgb|hsl)/i.freeze
|
120
|
+
OPEN_PAREN = /\(/.freeze
|
121
|
+
CLOSE_PAREN = /\)/.freeze
|
122
|
+
COMMA = /,/.freeze
|
123
|
+
NUMBER = /(\d+)(:?\.\d+)?/.freeze
|
124
|
+
UNIT = /(%|deg)/.freeze
|
125
|
+
end
|
126
|
+
|
127
|
+
def self.format_error_message(scanner, re)
|
128
|
+
out = StringIO.new
|
129
|
+
color_value = scanner.string
|
130
|
+
[
|
131
|
+
format('"%s" is not a valid code. An error occurred at:', color_value),
|
132
|
+
color_value,
|
133
|
+
"#{' ' * scanner.charpos}^ while searching with #{re}"
|
134
|
+
].each do |line|
|
135
|
+
out.puts line
|
136
|
+
end
|
137
|
+
|
138
|
+
out.string
|
139
|
+
end
|
140
|
+
|
141
|
+
private_class_method :format_error_message
|
142
|
+
|
143
|
+
def self.skip_spaces!(scanner)
|
144
|
+
scanner.scan(TokenRe::SPACES)
|
145
|
+
end
|
146
|
+
|
147
|
+
private_class_method :skip_spaces!
|
148
|
+
|
149
|
+
def self.read_token!(scanner, re)
|
150
|
+
skip_spaces!(scanner)
|
151
|
+
token = scanner.scan(re)
|
152
|
+
|
153
|
+
return token if token
|
154
|
+
|
155
|
+
error_message = format_error_message(scanner, re)
|
156
|
+
raise InvalidColorRepresentationError, error_message
|
157
|
+
end
|
158
|
+
|
159
|
+
private_class_method :read_token!
|
160
|
+
|
161
|
+
def self.read_scheme!(scanner)
|
162
|
+
scheme = read_token!(scanner, TokenRe::SCHEME)
|
163
|
+
|
164
|
+
parsed_value = {
|
165
|
+
scheme: scheme.downcase,
|
166
|
+
parameters: []
|
167
|
+
}
|
168
|
+
|
169
|
+
read_open_paren!(scanner, parsed_value)
|
170
|
+
end
|
171
|
+
|
172
|
+
private_class_method :read_scheme!
|
173
|
+
|
174
|
+
def self.read_open_paren!(scanner, parsed_value)
|
175
|
+
read_token!(scanner, TokenRe::OPEN_PAREN)
|
176
|
+
|
177
|
+
read_parameters!(scanner, parsed_value)
|
178
|
+
end
|
179
|
+
|
180
|
+
private_class_method :read_open_paren!
|
181
|
+
|
182
|
+
def self.read_close_paren!(scanner)
|
183
|
+
scanner.scan(TokenRe::CLOSE_PAREN)
|
184
|
+
end
|
185
|
+
|
186
|
+
private_class_method :read_close_paren!
|
187
|
+
|
188
|
+
def self.read_parameters!(scanner, parsed_value)
|
189
|
+
read_number!(scanner, parsed_value)
|
190
|
+
end
|
191
|
+
|
192
|
+
private_class_method :read_parameters!
|
193
|
+
|
194
|
+
def self.read_number!(scanner, parsed_value)
|
195
|
+
number = read_token!(scanner, TokenRe::NUMBER)
|
196
|
+
|
197
|
+
parsed_value[:parameters].push({ number: number, unit: nil })
|
198
|
+
|
199
|
+
read_unit!(scanner, parsed_value)
|
200
|
+
end
|
201
|
+
|
202
|
+
private_class_method :read_number!
|
203
|
+
|
204
|
+
def self.read_unit!(scanner, parsed_value)
|
205
|
+
unit = scanner.scan(TokenRe::UNIT)
|
206
|
+
|
207
|
+
parsed_value[:parameters].last[:unit] = unit if unit
|
208
|
+
|
209
|
+
read_comma!(scanner, parsed_value)
|
210
|
+
end
|
211
|
+
|
212
|
+
private_class_method :read_unit!
|
213
|
+
|
214
|
+
def self.read_comma!(scanner, parsed_value)
|
215
|
+
return parsed_value if read_close_paren!(scanner)
|
216
|
+
|
217
|
+
read_token!(scanner, TokenRe::COMMA)
|
218
|
+
read_number!(scanner, parsed_value)
|
219
|
+
end
|
220
|
+
|
221
|
+
private_class_method :read_comma!
|
222
|
+
|
223
|
+
##
|
224
|
+
# Parse an RGB/HSL function and store the result as an instance of
|
225
|
+
# ColorFunctionParser::Converter.
|
226
|
+
#
|
227
|
+
# @param color_value [String] RGB/HSL function defined at
|
228
|
+
# https://www.w3.org/TR/css-color-4/
|
229
|
+
# @return [Converter] An instance of ColorFunctionParser::Converter
|
230
|
+
|
231
|
+
def self.parse(color_value)
|
232
|
+
parsed_value = read_scheme!(StringScanner.new(color_value))
|
233
|
+
Converter.create(parsed_value, color_value)
|
234
|
+
end
|
235
|
+
|
236
|
+
##
|
237
|
+
# Return An RGB value gained from an RGB/HSL function.
|
238
|
+
#
|
239
|
+
# @return [Array<Integer>] RGB value represented as an array
|
240
|
+
|
241
|
+
def self.to_rgb(color_value)
|
242
|
+
parse(color_value).rgb
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module ColorContrastCalc
|
2
|
+
##
|
3
|
+
# Error raised if creating a Color instance with invalid value.
|
4
|
+
|
5
|
+
class InvalidColorRepresentationError < StandardError
|
6
|
+
module Template
|
7
|
+
RGB = 'An RGB value should be in form of [r, g, b], but %s.'
|
8
|
+
COLOR_NAME = '%s seems to be an undefined color name.'
|
9
|
+
HEX = 'A hex code #xxxxxx where 0 <= x <= f is expected, but %s.'
|
10
|
+
UNEXPECTED = 'A color should be given as an array or string, but %s.'
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.may_be_name?(value)
|
14
|
+
# all of the color keywords contain an alphabet between g-z.
|
15
|
+
/^#/ !~ value && /[g-z]/i =~ value
|
16
|
+
end
|
17
|
+
|
18
|
+
private_class_method :may_be_name?
|
19
|
+
|
20
|
+
def self.select_message_template(value)
|
21
|
+
case value
|
22
|
+
when Array
|
23
|
+
Template::RGB
|
24
|
+
when String
|
25
|
+
may_be_name?(value) ? Template::COLOR_NAME : Template::HEX
|
26
|
+
else
|
27
|
+
Template::UNEXPECTED
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private_class_method :select_message_template
|
32
|
+
|
33
|
+
def self.from_value(value)
|
34
|
+
message = format(select_message_template(value), value)
|
35
|
+
new(message)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
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.
|
4
|
+
version: 0.6.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:
|
11
|
+
date: 2020-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.0'
|
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: '
|
26
|
+
version: '2.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -112,9 +112,11 @@ 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
|
@@ -140,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
142
|
version: '0'
|
141
143
|
requirements: []
|
142
144
|
rubyforge_project:
|
143
|
-
rubygems_version: 2.6
|
145
|
+
rubygems_version: 2.7.6
|
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
|