color_contrast_calc 0.1.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 +7 -0
- data/.gitignore +15 -0
- data/.rspec +2 -0
- data/.rubocop.yml +8 -0
- data/.travis.yml +9 -0
- data/.yardopts +4 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.ja.md +296 -0
- data/README.md +295 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/color_contrast_calc.gemspec +30 -0
- data/examples/color_instance.rb +12 -0
- data/examples/color_lists.rb +26 -0
- data/examples/grayscale.rb +10 -0
- data/examples/sort_colors.rb +30 -0
- data/examples/yellow_black_contrast.rb +12 -0
- data/examples/yellow_orange_contrast.rb +32 -0
- data/exe/color_contrast_calc +4 -0
- data/lib/color_contrast_calc.rb +108 -0
- data/lib/color_contrast_calc/checker.rb +126 -0
- data/lib/color_contrast_calc/color.rb +409 -0
- data/lib/color_contrast_calc/converter.rb +191 -0
- data/lib/color_contrast_calc/data/color_keywords.json +149 -0
- data/lib/color_contrast_calc/shim.rb +32 -0
- data/lib/color_contrast_calc/sorter.rb +227 -0
- data/lib/color_contrast_calc/threshold_finder.rb +276 -0
- data/lib/color_contrast_calc/utils.rb +240 -0
- data/lib/color_contrast_calc/version.rb +5 -0
- metadata +146 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1a8775f32887a986970172b51ad16f6ecfed4eef
|
4
|
+
data.tar.gz: e7b5c2e6b91390bd2d9e2ad198bba4a02d4fd739
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 976ca9c5262cb36a3c293f6ee856f2c5ba485a6b98ed62cb545dd730623edfcc3ff449b18f5f242953cc9d408185d0ef9c5365e53ca0203dad6f327da0743f16
|
7
|
+
data.tar.gz: 5cd83c0fa63c5a601162a0ae79d5e09149594333c5218d058d17a9c3af54d6c98effbf412adb57c471cac5a1bd9147ad503671a20d0c43536bfcaf22726369fd
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 HASHIMOTO, Naoki
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.ja.md
ADDED
@@ -0,0 +1,296 @@
|
|
1
|
+
# ColorContrastCalc
|
2
|
+
|
3
|
+
`ColorContrastCalc`は、十分なコントラストのある色をWCAG 2.0を念頭に置きながら
|
4
|
+
選択することを支援するユーティリティとして開発しています。
|
5
|
+
|
6
|
+
このユーティリティを使い次のことができます:
|
7
|
+
|
8
|
+
* 2つの色のコントラスト比を確認する
|
9
|
+
* ある色に対し十分なコントラストがある色を(もしあれば)見つける
|
10
|
+
* ある色の属性を調整し、新しい色を作る
|
11
|
+
* 色をソートする
|
12
|
+
|
13
|
+
## インストール
|
14
|
+
|
15
|
+
Gemfileに次の行を追加し:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'color_contrast_calc'
|
19
|
+
```
|
20
|
+
|
21
|
+
次のコマンドを実行します:
|
22
|
+
|
23
|
+
$ bundle install
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install color_contrast_calc
|
28
|
+
|
29
|
+
## 使い方
|
30
|
+
|
31
|
+
### 色の表現
|
32
|
+
|
33
|
+
ユーティリティ内で色を表わすクラスとしてColorContrastCalc::Color`
|
34
|
+
が用意されています。
|
35
|
+
このクラスはユーティリティ内のほとんどの操作で利用されます。
|
36
|
+
|
37
|
+
例えば赤色を表す`Color`のインスタンスを生成したい場合、
|
38
|
+
`ColorContrastCalc.color_from`というメソッドが利用できます。
|
39
|
+
|
40
|
+
次のコードを`color_instance.rb`として保存し:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
require 'color_contrast_calc'
|
44
|
+
|
45
|
+
# Create an instance of Color from a hex code
|
46
|
+
# (You can pass 'red' or [255, 0, 0] instead of '#ff0000')
|
47
|
+
red = ColorContrastCalc.color_from('#ff0000')
|
48
|
+
puts red.class
|
49
|
+
puts red.name
|
50
|
+
puts red.hex
|
51
|
+
puts red.rgb.to_s
|
52
|
+
puts red.hsl.to_s
|
53
|
+
|
54
|
+
```
|
55
|
+
|
56
|
+
以下のように実行します:
|
57
|
+
|
58
|
+
```bash
|
59
|
+
$ ruby color_instance.rb
|
60
|
+
ColorContrastCalc::Color
|
61
|
+
red
|
62
|
+
#ff0000
|
63
|
+
[255, 0, 0]
|
64
|
+
[0.0, 100.0, 50.0]
|
65
|
+
|
66
|
+
```
|
67
|
+
|
68
|
+
### 例1: 2つの色のコントラスト比を計算する
|
69
|
+
|
70
|
+
例えば黄色と黒のコントラスト比を計算したい場合、
|
71
|
+
次のコードを`yellow_black_contrast.rb`として保存し:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
require 'color_contrast_calc'
|
75
|
+
|
76
|
+
yellow = ColorContrastCalc.color_from('yellow')
|
77
|
+
black = ColorContrastCalc.color_from('black')
|
78
|
+
|
79
|
+
contrast_ratio = yellow.contrast_ratio_against(black)
|
80
|
+
|
81
|
+
report = 'The contrast ratio between %s and %s is %2.4f'
|
82
|
+
puts(format(report, yellow.name, black.name, contrast_ratio))
|
83
|
+
puts(format(report, yellow.hex, black.hex, contrast_ratio))
|
84
|
+
```
|
85
|
+
|
86
|
+
以下のように実行します:
|
87
|
+
|
88
|
+
```bash
|
89
|
+
$ ruby yellow_black_contrast.rb
|
90
|
+
The contrast ratio between yellow and black is 19.5560
|
91
|
+
The contrast ratio between #ffff00 and #000000 is 19.5560
|
92
|
+
```
|
93
|
+
|
94
|
+
### 例2: ある色に対し十分なコントラスト比のある色を見つける
|
95
|
+
|
96
|
+
2色の組み合わせのうち、一方の色のbrightness/lightnessを変化させることで十分な
|
97
|
+
コントラストのある色を見つけたい場合、次のコードを`yellow_orange_contrast.rb`
|
98
|
+
として保存し:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
require 'color_contrast_calc'
|
102
|
+
|
103
|
+
yellow = ColorContrastCalc.color_from('yellow')
|
104
|
+
orange = ColorContrastCalc.color_from('orange')
|
105
|
+
|
106
|
+
report = 'The contrast ratio between %s and %s is %2.4f'
|
107
|
+
|
108
|
+
# Find brightness adjusted colors.
|
109
|
+
|
110
|
+
a_orange = yellow.find_brightness_threshold(orange, 'A')
|
111
|
+
a_contrast_ratio = yellow.contrast_ratio_against(a_orange)
|
112
|
+
|
113
|
+
aa_orange = yellow.find_brightness_threshold(orange, 'AA')
|
114
|
+
aa_contrast_ratio = yellow.contrast_ratio_against(aa_orange)
|
115
|
+
|
116
|
+
puts('# Brightness adjusted colors')
|
117
|
+
puts(format(report, yellow.hex, a_orange.hex, a_contrast_ratio))
|
118
|
+
puts(format(report, yellow.hex, aa_orange.hex, aa_contrast_ratio))
|
119
|
+
|
120
|
+
# Find lightness adjusted colors.
|
121
|
+
|
122
|
+
a_orange = yellow.find_lightness_threshold(orange, 'A')
|
123
|
+
a_contrast_ratio = yellow.contrast_ratio_against(a_orange)
|
124
|
+
|
125
|
+
aa_orange = yellow.find_lightness_threshold(orange, 'AA')
|
126
|
+
aa_contrast_ratio = yellow.contrast_ratio_against(aa_orange)
|
127
|
+
|
128
|
+
puts('# Lightness adjusted colors')
|
129
|
+
puts(format(report, yellow.hex, a_orange.hex, a_contrast_ratio))
|
130
|
+
puts(format(report, yellow.hex, aa_orange.hex, aa_contrast_ratio))
|
131
|
+
```
|
132
|
+
|
133
|
+
以下のように実行します:
|
134
|
+
|
135
|
+
```bash
|
136
|
+
$ ruby yellow_orange_contrast.rb
|
137
|
+
# Brightness adjusted colors
|
138
|
+
The contrast ratio between #ffff00 and #c68000 is 3.0138
|
139
|
+
The contrast ratio between #ffff00 and #9d6600 is 4.5121
|
140
|
+
# Lightness adjusted colors
|
141
|
+
The contrast ratio between #ffff00 and #c78000 is 3.0012
|
142
|
+
The contrast ratio between #ffff00 and #9d6600 is 4.5121
|
143
|
+
```
|
144
|
+
|
145
|
+
### 例3: ある色のグレースケール
|
146
|
+
|
147
|
+
ある色のグレースケールを得るために`ColorContrastCalc::Color` には
|
148
|
+
`new_grayscale_color`というインスタンスメソッドがあります。
|
149
|
+
|
150
|
+
例えば次のコードを`grayscale.rb`として保存し:
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
require 'color_contrast_calc'
|
154
|
+
|
155
|
+
yellow = ColorContrastCalc.color_from('yellow')
|
156
|
+
orange = ColorContrastCalc.color_from('orange')
|
157
|
+
|
158
|
+
report = 'The grayscale of %s is %s.'
|
159
|
+
puts(format(report, yellow.hex, yellow.new_grayscale_color))
|
160
|
+
puts(format(report, orange.hex, orange.new_grayscale_color))
|
161
|
+
```
|
162
|
+
|
163
|
+
以下のように実行します:
|
164
|
+
|
165
|
+
```bash
|
166
|
+
$ ruby grayscale.rb
|
167
|
+
The grayscale of #ffff00 is #ededed.
|
168
|
+
The grayscale of #ffa500 is #acacac.
|
169
|
+
```
|
170
|
+
|
171
|
+
また`new_grayscale_color`以外に、以下のインスタンスメッソドが
|
172
|
+
`ColorContrastCalc::Color`では利用できます。:
|
173
|
+
|
174
|
+
* `new_brightness_color`
|
175
|
+
* `new_contrast_color`
|
176
|
+
* `new_hue_rotate_color`
|
177
|
+
* `new_invert_color`
|
178
|
+
* `new_saturate_color`
|
179
|
+
|
180
|
+
### 例4: 色をソートする
|
181
|
+
|
182
|
+
`ColorContrastCalc::Sorter.sort`を使って色のソートができます。
|
183
|
+
|
184
|
+
またこのメソッドの2番目の引数でソート順を指定することもできます。
|
185
|
+
|
186
|
+
例えば次のコードを`sort_colors.rb`として保存し:
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
require 'color_contrast_calc'
|
190
|
+
|
191
|
+
color_names = ['red', 'yellow', 'lime', 'cyan', 'fuchsia', 'blue']
|
192
|
+
colors = color_names.map {|c| ColorContrastCalc.color_from(c) }
|
193
|
+
|
194
|
+
# sort by hSL order. An uppercase for a component of color means
|
195
|
+
# that component should be sorted in descending order.
|
196
|
+
|
197
|
+
hsl_ordered = ColorContrastCalc::Sorter.sort(colors, 'hSL')
|
198
|
+
puts("Colors sorted in the order of hSL: #{hsl_ordered.map(&:name)}")
|
199
|
+
|
200
|
+
# sort by RGB order.
|
201
|
+
|
202
|
+
rgb_ordered = ColorContrastCalc::Sorter.sort(colors, 'RGB')
|
203
|
+
puts("Colors sorted in the order of RGB: #{rgb_ordered.map(&:name)}")
|
204
|
+
|
205
|
+
# You can also change the precedence of components.
|
206
|
+
|
207
|
+
grb_ordered = ColorContrastCalc::Sorter.sort(colors, 'GRB')
|
208
|
+
puts("Colors sorted in the order of GRB: #{grb_ordered.map(&:name)}")
|
209
|
+
|
210
|
+
# And you can directly sort hex color codes.
|
211
|
+
|
212
|
+
## Hex color codes that correspond to the color_names given above.
|
213
|
+
hex_codes = ['#ff0000', '#ff0', '#00ff00', '#0ff', '#f0f', '#0000FF']
|
214
|
+
|
215
|
+
hsl_ordered = ColorContrastCalc::Sorter.sort(hex_codes, 'hSL')
|
216
|
+
puts("Colors sorted in the order of hSL: #{hsl_ordered}")
|
217
|
+
```
|
218
|
+
|
219
|
+
以下のように実行します:
|
220
|
+
|
221
|
+
```bash
|
222
|
+
$ ruby sort_colors.rb
|
223
|
+
Colors sorted in the order of hSL: ["red", "yellow", "lime", "cyan", "blue", "fuchsia"]
|
224
|
+
Colors sorted in the order of RGB: ["yellow", "fuchsia", "red", "cyan", "lime", "blue"]
|
225
|
+
Colors sorted in the order of GRB: ["yellow", "cyan", "lime", "fuchsia", "red", "blue"]
|
226
|
+
Colors sorted in the order of hSL: ["#ff0000", "#ff0", "#00ff00", "#0ff", "#0000FF", "#f0f"]
|
227
|
+
```
|
228
|
+
|
229
|
+
### 例5: 定義済みの色のリスト
|
230
|
+
|
231
|
+
[拡張カラーキーワードで定義された色](https://www.w3.org/TR/SVG/types.html#ColorKeywords)・
|
232
|
+
ウェブセーフカラーの2つのリストが予め定義されています。
|
233
|
+
|
234
|
+
|
235
|
+
また、HSLでsaturation・lightnessが共通する色のリストを生成する
|
236
|
+
`ColorContrastCalc::Color::List.hsl_colors`というメソッドがあります。
|
237
|
+
|
238
|
+
例えば次のコードを`color_lists.rb`として保存し:
|
239
|
+
|
240
|
+
```ruby
|
241
|
+
require 'color_contrast_calc'
|
242
|
+
|
243
|
+
# Named colors
|
244
|
+
named_colors = ColorContrastCalc.named_colors
|
245
|
+
|
246
|
+
puts("The number of named colors: #{named_colors.size}")
|
247
|
+
puts("The first of named colors: #{named_colors[0].name}")
|
248
|
+
puts("The last of named colors: #{named_colors[-1].name}")
|
249
|
+
|
250
|
+
# Web safe colors
|
251
|
+
web_safe_colors = ColorContrastCalc.web_safe_colors
|
252
|
+
|
253
|
+
puts("The number of web safe colors: #{web_safe_colors.size}")
|
254
|
+
puts("The first of web safe colors: #{web_safe_colors[0].name}")
|
255
|
+
puts("The last of web safe colors: #{web_safe_colors[-1].name}")
|
256
|
+
|
257
|
+
# HSL colors
|
258
|
+
hsl_colors = ColorContrastCalc.hsl_colors
|
259
|
+
|
260
|
+
puts("The number of HSL colors: #{hsl_colors.size}")
|
261
|
+
puts("The first of HSL colors: #{hsl_colors[0].name}")
|
262
|
+
puts("The 60th of HSL colors: #{hsl_colors[60].name}")
|
263
|
+
puts("The 120th of HSL colors: #{hsl_colors[120].name}")
|
264
|
+
puts("The last of HSL colors: #{hsl_colors[-1].name}")
|
265
|
+
```
|
266
|
+
|
267
|
+
以下のように実行します:
|
268
|
+
|
269
|
+
```bash
|
270
|
+
$ ruby color_lists.rb
|
271
|
+
The number of named colors: 147
|
272
|
+
The first of named colors: aliceblue
|
273
|
+
The last of named colors: yellowgreen
|
274
|
+
The number of web safe colors: 216
|
275
|
+
The first of web safe colors: black
|
276
|
+
The last of web safe colors: white
|
277
|
+
The number of HSL colors: 361
|
278
|
+
The first of HSL colors: #ff0000
|
279
|
+
The 60th of HSL colors: #ffff00
|
280
|
+
The 120th of HSL colors: #00ff00
|
281
|
+
The last of HSL colors: #ff0000
|
282
|
+
```
|
283
|
+
|
284
|
+
## Development
|
285
|
+
|
286
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
287
|
+
|
288
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
289
|
+
|
290
|
+
## Contributing
|
291
|
+
|
292
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nico-hn/color_contrast_calc_rb.
|
293
|
+
|
294
|
+
## License
|
295
|
+
|
296
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/README.md
ADDED
@@ -0,0 +1,295 @@
|
|
1
|
+
# ColorContrastCalc
|
2
|
+
|
3
|
+
`ColorContrastCalc` is a utility that helps you choose colors with
|
4
|
+
sufficient contrast, WCAG 2.0 in mind.
|
5
|
+
|
6
|
+
With this library, you can do following things:
|
7
|
+
|
8
|
+
* Check the contrast ratio between two colors
|
9
|
+
* Find (if exists) a color that has suffcient contrast to a given color
|
10
|
+
* Create a new color from a given color by adjusting properies of the latter
|
11
|
+
* Sort colors
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'color_contrast_calc'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install color_contrast_calc
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
### Representing a color
|
32
|
+
|
33
|
+
To represent a color, class `ColorContrastCalc::Color` is provided.
|
34
|
+
And most of the operations in this utility use this class.
|
35
|
+
|
36
|
+
As an illustration, if you want to create an instance of `Color` for red,
|
37
|
+
you may use a method `ColorContrastCalc.color_from`
|
38
|
+
|
39
|
+
Save the following code as `color_instance.rb`:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require 'color_contrast_calc'
|
43
|
+
|
44
|
+
# Create an instance of Color from a hex code
|
45
|
+
# (You can pass 'red' or [255, 0, 0] instead of '#ff0000')
|
46
|
+
red = ColorContrastCalc.color_from('#ff0000')
|
47
|
+
puts red.class
|
48
|
+
puts red.name
|
49
|
+
puts red.hex
|
50
|
+
puts red.rgb.to_s
|
51
|
+
puts red.hsl.to_s
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
Then execute the script:
|
56
|
+
|
57
|
+
```bash
|
58
|
+
$ ruby color_instance.rb
|
59
|
+
ColorContrastCalc::Color
|
60
|
+
red
|
61
|
+
#ff0000
|
62
|
+
[255, 0, 0]
|
63
|
+
[0.0, 100.0, 50.0]
|
64
|
+
|
65
|
+
```
|
66
|
+
|
67
|
+
### Example 1: Calculate the contrast ratio between two colors
|
68
|
+
|
69
|
+
If you want to calculate the contrast ratio between yellow and black,
|
70
|
+
save the following code as `yellow_black_contrast.rb`:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
require 'color_contrast_calc'
|
74
|
+
|
75
|
+
yellow = ColorContrastCalc.color_from('yellow')
|
76
|
+
black = ColorContrastCalc.color_from('black')
|
77
|
+
|
78
|
+
contrast_ratio = yellow.contrast_ratio_against(black)
|
79
|
+
|
80
|
+
report = 'The contrast ratio between %s and %s is %2.4f'
|
81
|
+
puts(format(report, yellow.name, black.name, contrast_ratio))
|
82
|
+
puts(format(report, yellow.hex, black.hex, contrast_ratio))
|
83
|
+
```
|
84
|
+
|
85
|
+
Then execute the script:
|
86
|
+
|
87
|
+
```bash
|
88
|
+
$ ruby yellow_black_contrast.rb
|
89
|
+
The contrast ratio between yellow and black is 19.5560
|
90
|
+
The contrast ratio between #ffff00 and #000000 is 19.5560
|
91
|
+
```
|
92
|
+
|
93
|
+
### Example 2: Find colors that have enough contrast ratio with a given color
|
94
|
+
|
95
|
+
If you want to find a combination of colors with sufficient contrast
|
96
|
+
by changing the brightness/lightness of one of those colors, save the
|
97
|
+
following code as `yellow_orange_contrast.rb`:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
require 'color_contrast_calc'
|
101
|
+
|
102
|
+
yellow = ColorContrastCalc.color_from('yellow')
|
103
|
+
orange = ColorContrastCalc.color_from('orange')
|
104
|
+
|
105
|
+
report = 'The contrast ratio between %s and %s is %2.4f'
|
106
|
+
|
107
|
+
# Find brightness adjusted colors.
|
108
|
+
|
109
|
+
a_orange = yellow.find_brightness_threshold(orange, 'A')
|
110
|
+
a_contrast_ratio = yellow.contrast_ratio_against(a_orange)
|
111
|
+
|
112
|
+
aa_orange = yellow.find_brightness_threshold(orange, 'AA')
|
113
|
+
aa_contrast_ratio = yellow.contrast_ratio_against(aa_orange)
|
114
|
+
|
115
|
+
puts('# Brightness adjusted colors')
|
116
|
+
puts(format(report, yellow.hex, a_orange.hex, a_contrast_ratio))
|
117
|
+
puts(format(report, yellow.hex, aa_orange.hex, aa_contrast_ratio))
|
118
|
+
|
119
|
+
# Find lightness adjusted colors.
|
120
|
+
|
121
|
+
a_orange = yellow.find_lightness_threshold(orange, 'A')
|
122
|
+
a_contrast_ratio = yellow.contrast_ratio_against(a_orange)
|
123
|
+
|
124
|
+
aa_orange = yellow.find_lightness_threshold(orange, 'AA')
|
125
|
+
aa_contrast_ratio = yellow.contrast_ratio_against(aa_orange)
|
126
|
+
|
127
|
+
puts('# Lightness adjusted colors')
|
128
|
+
puts(format(report, yellow.hex, a_orange.hex, a_contrast_ratio))
|
129
|
+
puts(format(report, yellow.hex, aa_orange.hex, aa_contrast_ratio))
|
130
|
+
```
|
131
|
+
|
132
|
+
Then execute the script:
|
133
|
+
|
134
|
+
```bash
|
135
|
+
$ ruby yellow_orange_contrast.rb
|
136
|
+
# Brightness adjusted colors
|
137
|
+
The contrast ratio between #ffff00 and #c68000 is 3.0138
|
138
|
+
The contrast ratio between #ffff00 and #9d6600 is 4.5121
|
139
|
+
# Lightness adjusted colors
|
140
|
+
The contrast ratio between #ffff00 and #c78000 is 3.0012
|
141
|
+
The contrast ratio between #ffff00 and #9d6600 is 4.5121
|
142
|
+
```
|
143
|
+
|
144
|
+
### Example 3: Grayscale of given colors
|
145
|
+
|
146
|
+
For getting grayscale, `ColorContrastCalc::Color` has an instance method
|
147
|
+
`new_grayscale_color`.
|
148
|
+
For example, save the following code as `grayscale.rb`:
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
require 'color_contrast_calc'
|
152
|
+
|
153
|
+
yellow = ColorContrastCalc.color_from('yellow')
|
154
|
+
orange = ColorContrastCalc.color_from('orange')
|
155
|
+
|
156
|
+
report = 'The grayscale of %s is %s.'
|
157
|
+
puts(format(report, yellow.hex, yellow.new_grayscale_color))
|
158
|
+
puts(format(report, orange.hex, orange.new_grayscale_color))
|
159
|
+
```
|
160
|
+
|
161
|
+
Then execute the script:
|
162
|
+
|
163
|
+
```bash
|
164
|
+
$ ruby grayscale.rb
|
165
|
+
The grayscale of #ffff00 is #ededed.
|
166
|
+
The grayscale of #ffa500 is #acacac.
|
167
|
+
```
|
168
|
+
|
169
|
+
And other than `new_grayscale_color`, following instance methods
|
170
|
+
are available for `ColorContrastCalc::Color`:
|
171
|
+
|
172
|
+
* `new_brightness_color`
|
173
|
+
* `new_contrast_color`
|
174
|
+
* `new_hue_rotate_color`
|
175
|
+
* `new_invert_color`
|
176
|
+
* `new_saturate_color`
|
177
|
+
|
178
|
+
### Example 4: Sort colors
|
179
|
+
|
180
|
+
You can sort colors using a method `ColorContrastCalc::Sorter.sort`.
|
181
|
+
|
182
|
+
And by passing the second argument to this method, you can also specify
|
183
|
+
the sort order.
|
184
|
+
|
185
|
+
For example, save the following code as `sort_colors.rb`:
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
require 'color_contrast_calc'
|
189
|
+
|
190
|
+
color_names = ['red', 'yellow', 'lime', 'cyan', 'fuchsia', 'blue']
|
191
|
+
colors = color_names.map {|c| ColorContrastCalc.color_from(c) }
|
192
|
+
|
193
|
+
# sort by hSL order. An uppercase for a component of color means
|
194
|
+
# that component should be sorted in descending order.
|
195
|
+
|
196
|
+
hsl_ordered = ColorContrastCalc::Sorter.sort(colors, 'hSL')
|
197
|
+
puts("Colors sorted in the order of hSL: #{hsl_ordered.map(&:name)}")
|
198
|
+
|
199
|
+
# sort by RGB order.
|
200
|
+
|
201
|
+
rgb_ordered = ColorContrastCalc::Sorter.sort(colors, 'RGB')
|
202
|
+
puts("Colors sorted in the order of RGB: #{rgb_ordered.map(&:name)}")
|
203
|
+
|
204
|
+
# You can also change the precedence of components.
|
205
|
+
|
206
|
+
grb_ordered = ColorContrastCalc::Sorter.sort(colors, 'GRB')
|
207
|
+
puts("Colors sorted in the order of GRB: #{grb_ordered.map(&:name)}")
|
208
|
+
|
209
|
+
# And you can directly sort hex color codes.
|
210
|
+
|
211
|
+
## Hex color codes that correspond to the color_names given above.
|
212
|
+
hex_codes = ['#ff0000', '#ff0', '#00ff00', '#0ff', '#f0f', '#0000FF']
|
213
|
+
|
214
|
+
hsl_ordered = ColorContrastCalc::Sorter.sort(hex_codes, 'hSL')
|
215
|
+
puts("Colors sorted in the order of hSL: #{hsl_ordered}")
|
216
|
+
```
|
217
|
+
|
218
|
+
Then execute the script:
|
219
|
+
|
220
|
+
```bash
|
221
|
+
$ ruby sort_colors.rb
|
222
|
+
Colors sorted in the order of hSL: ["red", "yellow", "lime", "cyan", "blue", "fuchsia"]
|
223
|
+
Colors sorted in the order of RGB: ["yellow", "fuchsia", "red", "cyan", "lime", "blue"]
|
224
|
+
Colors sorted in the order of GRB: ["yellow", "cyan", "lime", "fuchsia", "red", "blue"]
|
225
|
+
Colors sorted in the order of hSL: ["#ff0000", "#ff0", "#00ff00", "#0ff", "#0000FF", "#f0f"]
|
226
|
+
```
|
227
|
+
|
228
|
+
### Example 5: Lists of predefined colors
|
229
|
+
|
230
|
+
Two lists of colors are provided, one is for
|
231
|
+
[named colors](https://www.w3.org/TR/SVG/types.html#ColorKeywords)
|
232
|
+
and the other for the web safe colors.
|
233
|
+
|
234
|
+
And there is a method `ColorContrastCalc::Color::List.hsl_colors` that
|
235
|
+
generates a list of HSL colors that share same saturation and lightness.
|
236
|
+
|
237
|
+
For example, save the following code as `color_lists.rb`:
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
require 'color_contrast_calc'
|
241
|
+
|
242
|
+
# Named colors
|
243
|
+
named_colors = ColorContrastCalc.named_colors
|
244
|
+
|
245
|
+
puts("The number of named colors: #{named_colors.size}")
|
246
|
+
puts("The first of named colors: #{named_colors[0].name}")
|
247
|
+
puts("The last of named colors: #{named_colors[-1].name}")
|
248
|
+
|
249
|
+
# Web safe colors
|
250
|
+
web_safe_colors = ColorContrastCalc.web_safe_colors
|
251
|
+
|
252
|
+
puts("The number of web safe colors: #{web_safe_colors.size}")
|
253
|
+
puts("The first of web safe colors: #{web_safe_colors[0].name}")
|
254
|
+
puts("The last of web safe colors: #{web_safe_colors[-1].name}")
|
255
|
+
|
256
|
+
# HSL colors
|
257
|
+
hsl_colors = ColorContrastCalc.hsl_colors
|
258
|
+
|
259
|
+
puts("The number of HSL colors: #{hsl_colors.size}")
|
260
|
+
puts("The first of HSL colors: #{hsl_colors[0].name}")
|
261
|
+
puts("The 60th of HSL colors: #{hsl_colors[60].name}")
|
262
|
+
puts("The 120th of HSL colors: #{hsl_colors[120].name}")
|
263
|
+
puts("The last of HSL colors: #{hsl_colors[-1].name}")
|
264
|
+
```
|
265
|
+
|
266
|
+
Then execute the script:
|
267
|
+
|
268
|
+
```bash
|
269
|
+
$ ruby color_lists.rb
|
270
|
+
The number of named colors: 147
|
271
|
+
The first of named colors: aliceblue
|
272
|
+
The last of named colors: yellowgreen
|
273
|
+
The number of web safe colors: 216
|
274
|
+
The first of web safe colors: black
|
275
|
+
The last of web safe colors: white
|
276
|
+
The number of HSL colors: 361
|
277
|
+
The first of HSL colors: #ff0000
|
278
|
+
The 60th of HSL colors: #ffff00
|
279
|
+
The 120th of HSL colors: #00ff00
|
280
|
+
The last of HSL colors: #ff0000
|
281
|
+
```
|
282
|
+
|
283
|
+
## Development
|
284
|
+
|
285
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
286
|
+
|
287
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
288
|
+
|
289
|
+
## Contributing
|
290
|
+
|
291
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nico-hn/color_contrast_calc_rb.
|
292
|
+
|
293
|
+
## License
|
294
|
+
|
295
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|