color_contrast_calc 0.5.0 → 0.9.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 +9 -1
- data/.travis.yml +13 -5
- data/README.ja.md +80 -11
- data/README.md +83 -11
- data/color_contrast_calc.gemspec +6 -6
- data/examples/color_instance.rb +2 -1
- data/examples/sort_colors.rb +17 -9
- data/lib/color_contrast_calc.rb +81 -5
- data/lib/color_contrast_calc/color.rb +115 -43
- data/lib/color_contrast_calc/color_function_parser.rb +649 -0
- data/lib/color_contrast_calc/converter.rb +52 -0
- data/lib/color_contrast_calc/data/color_keywords.json +1 -0
- data/lib/color_contrast_calc/deprecated.rb +1 -1
- data/lib/color_contrast_calc/invalid_color_representation_error.rb +44 -0
- data/lib/color_contrast_calc/sorter.rb +151 -107
- data/lib/color_contrast_calc/transparency_calc.rb +55 -0
- data/lib/color_contrast_calc/utils.rb +89 -5
- data/lib/color_contrast_calc/version.rb +1 -1
- metadata +17 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 07b766d958883d015466220f97ed047512e0fc6dd2a51288b5274c6b0287f0fe
|
4
|
+
data.tar.gz: 1e740d3598c423f07efb3632b31bf155dbf337f8f9265ede39f3b12bd3e50456
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d3becf7316ad107082efb72a1e777fb23272a4fe73239170296e55e978d7d0935ad23e7b0a9203c0dc94164ac2dca5efc5763139c49e19cf116c50c28539e1c
|
7
|
+
data.tar.gz: 6e3e1efa303a5ea42e56fd4011ccee3cd19677f0ae770a279c9a4b6ffa9340bb63d5d192224b4f7b18a1364e2b299ecab878dee3a76aa657843ee333a619341b
|
data/.rubocop.yml
CHANGED
@@ -1,8 +1,16 @@
|
|
1
1
|
AllCops:
|
2
|
-
TargetRubyVersion: 2.
|
2
|
+
TargetRubyVersion: 2.4
|
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/MethodParameterName:
|
16
|
+
MinNameLength: 1
|
data/.travis.yml
CHANGED
@@ -1,10 +1,18 @@
|
|
1
1
|
sudo: false
|
2
2
|
language: ruby
|
3
3
|
rvm:
|
4
|
-
- 2.2
|
5
|
-
- 2.3
|
6
|
-
- 2.4
|
7
4
|
- 2.5
|
5
|
+
- 2.6
|
6
|
+
- 2.7
|
7
|
+
- 3.0
|
8
8
|
- ruby-head
|
9
|
-
|
10
|
-
|
9
|
+
before_install: gem install bundler -v '~>2.0'
|
10
|
+
jobs:
|
11
|
+
include:
|
12
|
+
- rvm: 2.4
|
13
|
+
before_install:
|
14
|
+
- yes | gem update --system --force
|
15
|
+
- gem install bundler
|
16
|
+
- rvm: jruby-head
|
17
|
+
before_install:
|
18
|
+
- bundle add rake --version "13.0.0"
|
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)', 'hsl(0deg, 100%, 50%)' or
|
51
|
+
# hwb(60deg 0% 0%) instead of '#ff0000')
|
51
52
|
red = ColorContrastCalc.color_from('#ff0000')
|
52
53
|
puts red.class
|
53
54
|
puts red.name
|
@@ -69,8 +70,41 @@ red
|
|
69
70
|
|
70
71
|
```
|
71
72
|
|
73
|
+
#### `ColorContrastCalc.color_from()`の引数に使える色の表現
|
74
|
+
|
75
|
+
`ColorContrastCalc.color_from()`の第1引数は以下の形式で指定できます。
|
76
|
+
|
77
|
+
* RGB値の16進数表記: #ff0, #ffff00, #FF0, etc.
|
78
|
+
* RGB値の関数形式表記: rgb(255, 255, 0), rgb(255 255 0), etc.
|
79
|
+
* Integerの配列で表したRGB値: [255, 255, 0], etc.
|
80
|
+
* HSL値の関数形式表記: hsl(60deg, 100%, 50%), hsl(60 100% 50%), etc.
|
81
|
+
* [実験的対応] HWB値の関数形式表記: hwb(60deg 0% 0%), hwb(60 0% 0%), etc.
|
82
|
+
* [拡張カラーキーワード](https://www.w3.org/TR/css-color-3/#svg-color): white, black, red, etc.
|
83
|
+
|
72
84
|
### 例1: 2つの色のコントラスト比を計算する
|
73
85
|
|
86
|
+
#### 1.1: 最も簡便なやり方
|
87
|
+
|
88
|
+
2色間のコントラスト比を計算するために`ColorContrastCalc`のクラスメソッドである`.contrast_ratio()`が利用可能です。
|
89
|
+
|
90
|
+
例えば黄色と黒のコントラスト比をコマンドラインで計算したい場合、次のように出来ます:
|
91
|
+
|
92
|
+
```bash
|
93
|
+
$ ruby -rcolor_contrast_calc -e 'puts ColorContrastCalc.contrast_ratio("#ff0", "#000")'
|
94
|
+
19.555999999999997
|
95
|
+
```
|
96
|
+
|
97
|
+
もしくは
|
98
|
+
|
99
|
+
```bash
|
100
|
+
$ ruby -rcolor_contrast_calc -e 'puts ColorContrastCalc.contrast_ratio("rgb(255, 255, 0)", "black")'
|
101
|
+
19.555999999999997
|
102
|
+
```
|
103
|
+
|
104
|
+
(黄色を表すためには"hsl(60deg, 100%, 50%)", "#FFFF00", [255, 255, 0]等も使えます。)
|
105
|
+
|
106
|
+
#### 1.2: Colorクラスのインスタンスを使っての計算
|
107
|
+
|
74
108
|
例えば黄色と黒のコントラスト比を計算したい場合、
|
75
109
|
次のコードを`yellow_black_contrast.rb`として保存し:
|
76
110
|
|
@@ -95,7 +129,9 @@ The contrast ratio between yellow and black is 19.5560
|
|
95
129
|
The contrast ratio between #ffff00 and #000000 is 19.5560
|
96
130
|
```
|
97
131
|
|
98
|
-
|
132
|
+
#### 1.3: RGBを表す低レベルの値からの計算
|
133
|
+
|
134
|
+
2色の16進数カラーコードあるいはRGB値からコントラスト比を計算することも可能です。
|
99
135
|
|
100
136
|
次のコードを `yellow_black_hex_contrast.rb`として保存し:
|
101
137
|
|
@@ -116,10 +152,34 @@ puts "Contrast level: #{level}"
|
|
116
152
|
以下のように実行します:
|
117
153
|
|
118
154
|
```bash
|
155
|
+
$ ruby yellow_black_hex_contrast.rb
|
119
156
|
Contrast ratio between yellow and black: 19.555999999999997
|
120
157
|
Contrast level: AAA
|
121
158
|
```
|
122
159
|
|
160
|
+
#### 1.4: [実験的対応] 透明色間のコントラスト比の計算
|
161
|
+
|
162
|
+
透明色間の計算のために``ColorContrastCalc.contrast_ratio_with_opacity()``が
|
163
|
+
提供されています。
|
164
|
+
|
165
|
+
このメソッドは3つの引数として、前景色と背景色、省略可能な基調色、を取ります。
|
166
|
+
|
167
|
+
3番目の基調色は他の2色の下に配置され、完全に不透明なことが期待される点にご注意下さい。
|
168
|
+
|
169
|
+
例:
|
170
|
+
|
171
|
+
```bash
|
172
|
+
irb -r color_contrast_calc
|
173
|
+
irb(main):001:0> ColorContrastCalc.contrast_ratio_with_opacity('rgb(255 255 0 / 1.0)', 'rgb(0 255 0 / 0.5)', 'white')
|
174
|
+
=> 1.1828076947731336
|
175
|
+
irb(main):002:0> ColorContrastCalc.contrast_ratio_with_opacity('rgb(255 255 0 / 1.0)', 'rgb(0 255 0 / 0.5)') # 3番目の色のデフォルト値は白です。
|
176
|
+
=> 1.1828076947731336
|
177
|
+
irb(main):003:0> ColorContrastCalc.contrast_ratio_with_opacity('rgb(255 255 0 / 1.0)', 'rgb(0 255 0 / 0.5)', 'black')
|
178
|
+
=> 4.78414518008597
|
179
|
+
irb(main):004:0> ColorContrastCalc.contrast_ratio_with_opacity('rgb(255 255 0)', 'rgb(0 255 0 / 0.5)', 'black') # 完全に不透明な色について不透明度を指定する必要はありません。
|
180
|
+
=> 4.78414518008597
|
181
|
+
```
|
182
|
+
|
123
183
|
### 例2: ある色に対し十分なコントラスト比のある色を見つける
|
124
184
|
|
125
185
|
2色の組み合わせのうち、一方の色のbrightness/lightnessを変化させることで十分な
|
@@ -229,32 +289,40 @@ The grayscale of #ffa500 is #acacac.
|
|
229
289
|
```ruby
|
230
290
|
require 'color_contrast_calc'
|
231
291
|
|
232
|
-
color_names = ['red', '
|
233
|
-
colors = color_names.map {|c| ColorContrastCalc.color_from(c) }
|
292
|
+
color_names = ['red', 'lime', 'cyan', 'yellow', 'fuchsia', 'blue']
|
234
293
|
|
235
294
|
# Sort by hSL order. An uppercase for a component of color means
|
236
295
|
# that component should be sorted in descending order.
|
237
296
|
|
238
|
-
hsl_ordered = ColorContrastCalc.sort(
|
239
|
-
puts("Colors sorted in the order of hSL: #{hsl_ordered
|
297
|
+
hsl_ordered = ColorContrastCalc.sort(color_names, 'hSL')
|
298
|
+
puts("Colors sorted in the order of hSL: #{hsl_ordered}")
|
240
299
|
|
241
300
|
# Sort by RGB order.
|
242
301
|
|
243
|
-
rgb_ordered = ColorContrastCalc.sort(
|
244
|
-
puts("Colors sorted in the order of RGB: #{rgb_ordered
|
302
|
+
rgb_ordered = ColorContrastCalc.sort(color_names, 'RGB')
|
303
|
+
puts("Colors sorted in the order of RGB: #{rgb_ordered}")
|
245
304
|
|
246
305
|
# You can also change the precedence of components.
|
247
306
|
|
248
|
-
grb_ordered = ColorContrastCalc.sort(
|
249
|
-
puts("Colors sorted in the order of GRB: #{grb_ordered
|
307
|
+
grb_ordered = ColorContrastCalc.sort(color_names, 'GRB')
|
308
|
+
puts("Colors sorted in the order of GRB: #{grb_ordered}")
|
250
309
|
|
251
310
|
# And you can directly sort hex color codes.
|
252
311
|
|
253
312
|
## Hex color codes that correspond to the color_names given above.
|
254
|
-
hex_codes = ['#ff0000', '#
|
313
|
+
hex_codes = ['#ff0000', '#00ff00', '#0ff', '#ff0', '#f0f', '#0000FF']
|
255
314
|
|
256
315
|
hsl_ordered = ColorContrastCalc.sort(hex_codes, 'hSL')
|
257
316
|
puts("Colors sorted in the order of hSL: #{hsl_ordered}")
|
317
|
+
|
318
|
+
# If you want to sort colors in different notations,
|
319
|
+
# you should specify a key_mapper.
|
320
|
+
|
321
|
+
colors = ['rgb(255 0 0)', 'hsl(120 100% 50%)', '#0ff', 'hwb(60 0% 0%)', [255, 0, 255], '#0000ff']
|
322
|
+
|
323
|
+
key_mapper = proc {|c| ColorContrastCalc.color_from(c) }
|
324
|
+
colors_in_hsl_order = ColorContrastCalc.sort(colors, 'hSL', key_mapper)
|
325
|
+
puts("Colors sorted in the order of hSL: #{colors_in_hsl_order}")
|
258
326
|
```
|
259
327
|
|
260
328
|
以下のように実行します:
|
@@ -265,6 +333,7 @@ Colors sorted in the order of hSL: ["red", "yellow", "lime", "cyan", "blue", "fu
|
|
265
333
|
Colors sorted in the order of RGB: ["yellow", "fuchsia", "red", "cyan", "lime", "blue"]
|
266
334
|
Colors sorted in the order of GRB: ["yellow", "cyan", "lime", "fuchsia", "red", "blue"]
|
267
335
|
Colors sorted in the order of hSL: ["#ff0000", "#ff0", "#00ff00", "#0ff", "#0000FF", "#f0f"]
|
336
|
+
Colors sorted in the order of hSL: ["rgb(255 0 0)", "hwb(60 0% 0%)", "hsl(120 100% 50%)", "#0ff", "#0000ff", [255, 0, 255]]
|
268
337
|
```
|
269
338
|
|
270
339
|
### 例5: 定義済みの色のリスト
|
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)', 'hsl(0deg, 100%, 50%)' or
|
50
|
+
# hwb(60deg 0% 0%) instead of '#ff0000')
|
50
51
|
red = ColorContrastCalc.color_from('#ff0000')
|
51
52
|
puts red.class
|
52
53
|
puts red.name
|
@@ -68,8 +69,41 @@ red
|
|
68
69
|
|
69
70
|
```
|
70
71
|
|
72
|
+
#### Color units for the argument of `ColorContrastCalc.color_from()`
|
73
|
+
|
74
|
+
The following formats are supported for the first argument of `ColorContrastCalc.color_from()`.
|
75
|
+
|
76
|
+
* RGB values in hexadecimal notation: #ff0, #ffff00, #FF0, etc.
|
77
|
+
* RGB values in functional notation: rgb(255, 255, 0), rgb(255 255 0), etc.
|
78
|
+
* RGB values as an Array of Integers: [255, 255, 0], etc.
|
79
|
+
* HSL colors in functional notation: hsl(60deg, 100%, 50%), hsl(60 100% 50%), etc.
|
80
|
+
* [Experimental] HWB colors in functional notation: hwb(60deg 0% 0%), hwb(60 0% 0%), etc.
|
81
|
+
* [Extended color keywords](https://www.w3.org/TR/css-color-3/#svg-color): white, black, red, etc.
|
82
|
+
|
71
83
|
### Example 1: Calculate the contrast ratio between two colors
|
72
84
|
|
85
|
+
#### 1.1: The easiest way
|
86
|
+
|
87
|
+
To calculate the contrast ratio between two colors, a class method of `ColorContrastCalc`, `.contrast_ratio()` is available.
|
88
|
+
|
89
|
+
For example, if you want to calculate the contrast ratio between yellow and black at the command line, you can do as follows:
|
90
|
+
|
91
|
+
```bash
|
92
|
+
$ ruby -rcolor_contrast_calc -e 'puts ColorContrastCalc.contrast_ratio("#ff0", "#000")'
|
93
|
+
19.555999999999997
|
94
|
+
```
|
95
|
+
|
96
|
+
Or
|
97
|
+
|
98
|
+
```bash
|
99
|
+
$ ruby -rcolor_contrast_calc -e 'puts ColorContrastCalc.contrast_ratio("rgb(255, 255, 0)", "black")'
|
100
|
+
19.555999999999997
|
101
|
+
```
|
102
|
+
|
103
|
+
(To represent a yellow, you can also use "hsl(60deg, 100%, 50%)", "#FFFF00", [255, 255, 0]...)
|
104
|
+
|
105
|
+
#### 1.2: Calculate using Color instances
|
106
|
+
|
73
107
|
If you want to calculate the contrast ratio between yellow and black,
|
74
108
|
save the following code as `yellow_black_contrast.rb`:
|
75
109
|
|
@@ -94,7 +128,9 @@ The contrast ratio between yellow and black is 19.5560
|
|
94
128
|
The contrast ratio between #ffff00 and #000000 is 19.5560
|
95
129
|
```
|
96
130
|
|
97
|
-
|
131
|
+
#### 1.3: Calculate from low level RGB values
|
132
|
+
|
133
|
+
It is also possible to calculate the contrast ratio of two colors from
|
98
134
|
their hex color codes or RGB values.
|
99
135
|
|
100
136
|
Save the following code as `yellow_black_hex_contrast.rb`:
|
@@ -116,10 +152,37 @@ puts "Contrast level: #{level}"
|
|
116
152
|
Then execute the script:
|
117
153
|
|
118
154
|
```bash
|
155
|
+
$ ruby yellow_black_hex_contrast.rb
|
119
156
|
Contrast ratio between yellow and black: 19.555999999999997
|
120
157
|
Contrast level: AAA
|
121
158
|
```
|
122
159
|
|
160
|
+
|
161
|
+
#### 1.4: [Experimental] Calculate the contrast ratio between transparent colors
|
162
|
+
|
163
|
+
``ColorContrastCalc.contrast_ratio_with_opacity()`` is provided for the
|
164
|
+
calculation.
|
165
|
+
|
166
|
+
The method takes three arguments, foreground color, background color and an
|
167
|
+
optional base color.
|
168
|
+
|
169
|
+
Please note that the third color is placed below the other two colors and
|
170
|
+
expects to be fully opaque.
|
171
|
+
|
172
|
+
For example:
|
173
|
+
|
174
|
+
```bash
|
175
|
+
irb -r color_contrast_calc
|
176
|
+
irb(main):001:0> ColorContrastCalc.contrast_ratio_with_opacity('rgb(255 255 0 / 1.0)', 'rgb(0 255 0 / 0.5)', 'white')
|
177
|
+
=> 1.1828076947731336
|
178
|
+
irb(main):002:0> ColorContrastCalc.contrast_ratio_with_opacity('rgb(255 255 0 / 1.0)', 'rgb(0 255 0 / 0.5)') # The default value for the third parameter is white.
|
179
|
+
=> 1.1828076947731336
|
180
|
+
irb(main):003:0> ColorContrastCalc.contrast_ratio_with_opacity('rgb(255 255 0 / 1.0)', 'rgb(0 255 0 / 0.5)', 'black')
|
181
|
+
=> 4.78414518008597
|
182
|
+
irb(main):004:0> ColorContrastCalc.contrast_ratio_with_opacity('rgb(255 255 0)', 'rgb(0 255 0 / 0.5)', 'black') # For a fully opaque color, you don't need to specify the opacity.
|
183
|
+
=> 4.78414518008597
|
184
|
+
```
|
185
|
+
|
123
186
|
### Example 2: Find colors that have enough contrast ratio with a given color
|
124
187
|
|
125
188
|
If you want to find a combination of colors with sufficient contrast
|
@@ -229,32 +292,40 @@ For example, save the following code as `sort_colors.rb`:
|
|
229
292
|
```ruby
|
230
293
|
require 'color_contrast_calc'
|
231
294
|
|
232
|
-
color_names = ['red', '
|
233
|
-
colors = color_names.map {|c| ColorContrastCalc.color_from(c) }
|
295
|
+
color_names = ['red', 'lime', 'cyan', 'yellow', 'fuchsia', 'blue']
|
234
296
|
|
235
297
|
# Sort by hSL order. An uppercase for a component of color means
|
236
298
|
# that component should be sorted in descending order.
|
237
299
|
|
238
|
-
hsl_ordered = ColorContrastCalc.sort(
|
239
|
-
puts("Colors sorted in the order of hSL: #{hsl_ordered
|
300
|
+
hsl_ordered = ColorContrastCalc.sort(color_names, 'hSL')
|
301
|
+
puts("Colors sorted in the order of hSL: #{hsl_ordered}")
|
240
302
|
|
241
303
|
# Sort by RGB order.
|
242
304
|
|
243
|
-
rgb_ordered = ColorContrastCalc.sort(
|
244
|
-
puts("Colors sorted in the order of RGB: #{rgb_ordered
|
305
|
+
rgb_ordered = ColorContrastCalc.sort(color_names, 'RGB')
|
306
|
+
puts("Colors sorted in the order of RGB: #{rgb_ordered}")
|
245
307
|
|
246
308
|
# You can also change the precedence of components.
|
247
309
|
|
248
|
-
grb_ordered = ColorContrastCalc.sort(
|
249
|
-
puts("Colors sorted in the order of GRB: #{grb_ordered
|
310
|
+
grb_ordered = ColorContrastCalc.sort(color_names, 'GRB')
|
311
|
+
puts("Colors sorted in the order of GRB: #{grb_ordered}")
|
250
312
|
|
251
313
|
# And you can directly sort hex color codes.
|
252
314
|
|
253
315
|
## Hex color codes that correspond to the color_names given above.
|
254
|
-
hex_codes = ['#ff0000', '#
|
316
|
+
hex_codes = ['#ff0000', '#00ff00', '#0ff', '#ff0', '#f0f', '#0000FF']
|
255
317
|
|
256
318
|
hsl_ordered = ColorContrastCalc.sort(hex_codes, 'hSL')
|
257
319
|
puts("Colors sorted in the order of hSL: #{hsl_ordered}")
|
320
|
+
|
321
|
+
# If you want to sort colors in different notations,
|
322
|
+
# you should specify a key_mapper.
|
323
|
+
|
324
|
+
colors = ['rgb(255 0 0)', 'hsl(120 100% 50%)', '#0ff', 'hwb(60 0% 0%)', [255, 0, 255], '#0000ff']
|
325
|
+
|
326
|
+
key_mapper = proc {|c| ColorContrastCalc.color_from(c) }
|
327
|
+
colors_in_hsl_order = ColorContrastCalc.sort(colors, 'hSL', key_mapper)
|
328
|
+
puts("Colors sorted in the order of hSL: #{colors_in_hsl_order}")
|
258
329
|
```
|
259
330
|
|
260
331
|
Then execute the script:
|
@@ -265,6 +336,7 @@ Colors sorted in the order of hSL: ["red", "yellow", "lime", "cyan", "blue", "fu
|
|
265
336
|
Colors sorted in the order of RGB: ["yellow", "fuchsia", "red", "cyan", "lime", "blue"]
|
266
337
|
Colors sorted in the order of GRB: ["yellow", "cyan", "lime", "fuchsia", "red", "blue"]
|
267
338
|
Colors sorted in the order of hSL: ["#ff0000", "#ff0", "#00ff00", "#0ff", "#0000FF", "#f0f"]
|
339
|
+
Colors sorted in the order of hSL: ["rgb(255 0 0)", "hwb(60 0% 0%)", "hsl(120 100% 50%)", "#0ff", "#0000ff", [255, 0, 255]]
|
268
340
|
```
|
269
341
|
|
270
342
|
### Example 5: Lists of predefined colors
|
data/color_contrast_calc.gemspec
CHANGED
@@ -7,7 +7,7 @@ require 'color_contrast_calc/version'
|
|
7
7
|
Gem::Specification.new do |spec|
|
8
8
|
spec.name = 'color_contrast_calc'
|
9
9
|
spec.version = ColorContrastCalc::VERSION
|
10
|
-
spec.required_ruby_version = ">= 2.
|
10
|
+
spec.required_ruby_version = ">= 2.4"
|
11
11
|
spec.authors = ['HASHIMOTO, Naoki']
|
12
12
|
spec.email = ['hashimoto.naoki@gmail.com']
|
13
13
|
|
@@ -23,9 +23,9 @@ 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', '~> 1
|
27
|
-
spec.add_development_dependency 'rake', '~>
|
28
|
-
spec.add_development_dependency 'rspec', '~> 3.
|
29
|
-
spec.add_development_dependency 'rubocop', '~>
|
30
|
-
spec.add_development_dependency 'yard', '~> 0.9
|
26
|
+
spec.add_development_dependency 'bundler', '~> 2.1'
|
27
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.10'
|
29
|
+
spec.add_development_dependency 'rubocop', '~> 1.7'
|
30
|
+
spec.add_development_dependency 'yard', '~> 0.9'
|
31
31
|
end
|
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)', 'hsl(0deg, 100%, 50%)' or
|
7
|
+
# hwb(60deg 0% 0%) instead of '#ff0000')
|
7
8
|
red = ColorContrastCalc.color_from('#ff0000')
|
8
9
|
puts red.class
|
9
10
|
puts red.name
|
data/examples/sort_colors.rb
CHANGED
@@ -2,29 +2,37 @@
|
|
2
2
|
|
3
3
|
require 'color_contrast_calc'
|
4
4
|
|
5
|
-
color_names = ['red', '
|
6
|
-
colors = color_names.map {|c| ColorContrastCalc.color_from(c) }
|
5
|
+
color_names = ['red', 'lime', 'cyan', 'yellow', 'fuchsia', 'blue']
|
7
6
|
|
8
7
|
# Sort by hSL order. An uppercase for a component of color means
|
9
8
|
# that component should be sorted in descending order.
|
10
9
|
|
11
|
-
hsl_ordered = ColorContrastCalc.sort(
|
12
|
-
puts("Colors sorted in the order of hSL: #{hsl_ordered
|
10
|
+
hsl_ordered = ColorContrastCalc.sort(color_names, 'hSL')
|
11
|
+
puts("Colors sorted in the order of hSL: #{hsl_ordered}")
|
13
12
|
|
14
13
|
# Sort by RGB order.
|
15
14
|
|
16
|
-
rgb_ordered = ColorContrastCalc.sort(
|
17
|
-
puts("Colors sorted in the order of RGB: #{rgb_ordered
|
15
|
+
rgb_ordered = ColorContrastCalc.sort(color_names, 'RGB')
|
16
|
+
puts("Colors sorted in the order of RGB: #{rgb_ordered}")
|
18
17
|
|
19
18
|
# You can also change the precedence of components.
|
20
19
|
|
21
|
-
grb_ordered = ColorContrastCalc.sort(
|
22
|
-
puts("Colors sorted in the order of GRB: #{grb_ordered
|
20
|
+
grb_ordered = ColorContrastCalc.sort(color_names, 'GRB')
|
21
|
+
puts("Colors sorted in the order of GRB: #{grb_ordered}")
|
23
22
|
|
24
23
|
# And you can directly sort hex color codes.
|
25
24
|
|
26
25
|
## Hex color codes that correspond to the color_names given above.
|
27
|
-
hex_codes = ['#ff0000', '#
|
26
|
+
hex_codes = ['#ff0000', '#00ff00', '#0ff', '#ff0', '#f0f', '#0000FF']
|
28
27
|
|
29
28
|
hsl_ordered = ColorContrastCalc.sort(hex_codes, 'hSL')
|
30
29
|
puts("Colors sorted in the order of hSL: #{hsl_ordered}")
|
30
|
+
|
31
|
+
# If you want to sort colors in different notations,
|
32
|
+
# you should specify a key_mapper.
|
33
|
+
|
34
|
+
colors = ['rgb(255 0 0)', 'hsl(120 100% 50%)', '#0ff', 'hwb(60 0% 0%)', [255, 0, 255], '#0000ff']
|
35
|
+
|
36
|
+
key_mapper = proc {|c| ColorContrastCalc.color_from(c) }
|
37
|
+
colors_in_hsl_order = ColorContrastCalc.sort(colors, 'hSL', key_mapper)
|
38
|
+
puts("Colors sorted in the order of hSL: #{colors_in_hsl_order}")
|