├── .gitignore
├── .npmignore
├── .travis.yml
├── LICENSE
├── Makefile
├── README.md
├── examples
├── examples.cjsx
├── index.cjsx
├── index.html
└── main.css
├── package.json
├── src
├── contrastSearch.coffee
└── index.coffee
├── test
└── tests.coffee
├── webpack.config.js
└── webpack.production.config.js
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 |
5 | # Runtime data
6 | pids
7 | *.pid
8 | *.seed
9 |
10 | # Directory for instrumented libs generated by jscoverage/JSCover
11 | lib-cov
12 |
13 | # Coverage directory used by tools like istanbul
14 | coverage
15 |
16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17 | .grunt
18 |
19 | # Compiled binary addons (http://nodejs.org/api/addons.html)
20 | build/Release
21 |
22 | # Dependency directory
23 | # Commenting this out is preferred by some people, see
24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
25 | node_modules
26 |
27 | # Users Environment Variables
28 | .lock-wscript
29 | dist/
30 | examples/bundle.js
31 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 |
5 | # Runtime data
6 | pids
7 | *.pid
8 | *.seed
9 |
10 | # Directory for instrumented libs generated by jscoverage/JSCover
11 | lib-cov
12 |
13 | # Coverage directory used by tools like istanbul
14 | coverage
15 |
16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17 | .grunt
18 |
19 | # Compiled binary addons (http://nodejs.org/api/addons.html)
20 | build/Release
21 |
22 | # Dependency directory
23 | # Commenting this out is preferred by some people, see
24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
25 | node_modules
26 |
27 | # Users Environment Variables
28 | .lock-wscript
29 | src/
30 | examples/bundle.js
31 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 |
3 | node_js:
4 | - "0.10"
5 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Kyle Mathews
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 all
13 | 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 THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | BIN = ./node_modules/.bin
2 |
3 | release-patch:
4 | @$(call release,patch)
5 |
6 | release-minor:
7 | @$(call release,minor)
8 |
9 | release-major:
10 | @$(call release,major)
11 |
12 | build:
13 | @$(BIN)/coffee -cb -o dist src/
14 | @$(BIN)/webpack
15 |
16 | publish:
17 | git push --tags origin HEAD:master
18 | @$(BIN)/coffee -cb -o dist src/
19 | npm publish
20 |
21 | publish-gh-pages:
22 | git checkout gh-pages
23 | git merge master
24 | @$(BIN)/webpack --config webpack.production.config.js
25 | cp examples/* .
26 | git add --all .
27 | git commit -m "New release"
28 | git push origin gh-pages
29 | git checkout master
30 |
31 | define release
32 | npm version $(1)
33 | endef
34 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](http://travis-ci.org/KyleAMathews/color-pairs-picker)
3 |
4 | color-pairs-picker
5 | ==================
6 |
7 | Given a color, it picks a pleasing and [well contrasted](http://contrastrebellion.com/) background and foreground colors
8 |
9 | ## Demo
10 | http://kyleamathews.github.io/color-pairs-picker/
11 |
12 | ## Install
13 | `npm install color-pairs-picker`
14 |
15 | ## Usage
16 |
17 | This module attempts to pick pleasing color pairs that satisfy the
18 | following constraints:
19 |
20 | * Contrast > 4.5 for easy readability
21 | * Avoid pure blacks and pure whites.
22 | * Keep the background color > 0.15 luminance
23 |
24 | ```javascript
25 | var colorPairsPicker = require 'color-pairs-picker';
26 |
27 | var baseColor = "#BA55D3";
28 |
29 | var pair = colorPairsPicker(baseColor);
30 |
31 | // Set a higher contrast over the default 5.5.
32 | // Note, the more saturated your colors the less contrasty they'll be.
33 | var pair = colorPairsPicker(baseColor, {contrast: 8});
34 | ```
35 |
--------------------------------------------------------------------------------
/examples/examples.cjsx:
--------------------------------------------------------------------------------
1 | React = require('react/addons')
2 | ColorPicker = require 'react-color-picker'
3 | chroma = require 'chroma-js'
4 | colorPairsPicker = require '../src/index'
5 |
6 | module.exports = React.createClass
7 |
8 | mixins: [React.addons.LinkedStateMixin]
9 |
10 | getInitialState: ->
11 | color = "red"
12 |
13 | return {
14 | contrast: 5
15 | foregroundMin: 0.02
16 | foregroundMax: 0.98
17 | backgroundMin: 0.15
18 | backgroundMax: 0.85
19 | color: color
20 | }
21 |
22 | render: ->
23 | {bg, fg} = colorPairsPicker(@state.color, {
24 | contrast: parseFloat(@state.contrast)
25 | foregroundMin: parseFloat(@state.foregroundMin)
26 | foregroundMax: parseFloat(@state.foregroundMax)
27 | backgroundMax: parseFloat(@state.backgroundMax)
28 | backgroundMin: parseFloat(@state.backgroundMin)
29 | })
30 |
31 |
32 |
Color Pairs Picker
33 |
Given a color, it picks a pleasing and well contrasted background and foreground colors
34 |
Code on Github
35 |
36 |
37 |
Pick a color
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
Background luminance: {chroma(bg).luminance()}
56 |
Foreground luminance: {chroma(fg).luminance()}
57 |
Contrast: {chroma.contrast(fg, bg)}
58 |
DIY PBR&B Portland paleo, listicle Carles meggings seitan salvia hoodie McSweeney's whatever direct trade polaroid. Seitan organic health goth, photo booth kogi bespoke banjo pug cray gluten-free Shoreditch. Retro slow-carb shabby chic, flexitarian Pitchfork Vice scenester. Kogi Echo Park High Life flannel. Stumptown Neutra banjo distillery. Odd Future fixie Blue Bottle, pickled mixtape listicle semiotics vinyl Thundercats forage ennui gluten-free before they sold out Austin. PBR&B sartorial kitsch, sriracha Banksy flannel single-origin coffee kale chips Shoreditch.
59 |
60 |
61 | handleChange: (color) ->
62 | @setState {
63 | color: color
64 | }
65 |
--------------------------------------------------------------------------------
/examples/index.cjsx:
--------------------------------------------------------------------------------
1 | React = require('react')
2 | Examples = require './examples'
3 |
4 | React.render(, document.body)
5 |
--------------------------------------------------------------------------------
/examples/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Example
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/examples/main.css:
--------------------------------------------------------------------------------
1 | /*! normalize.css v3.0.2 | MIT License | git.io/normalize */
2 |
3 | /**
4 | * 1. Set default font family to sans-serif.
5 | * 2. Prevent iOS text size adjust after orientation change, without disabling
6 | * user zoom.
7 | */
8 |
9 | html {
10 | font-family: sans-serif; /* 1 */
11 | -ms-text-size-adjust: 100%; /* 2 */
12 | -webkit-text-size-adjust: 100%; /* 2 */
13 | }
14 |
15 | /**
16 | * Remove default margin.
17 | */
18 |
19 | body {
20 | margin: 0;
21 | }
22 |
23 | /* HTML5 display definitions
24 | ========================================================================== */
25 |
26 | /**
27 | * Correct `block` display not defined for any HTML5 element in IE 8/9.
28 | * Correct `block` display not defined for `details` or `summary` in IE 10/11
29 | * and Firefox.
30 | * Correct `block` display not defined for `main` in IE 11.
31 | */
32 |
33 | article,
34 | aside,
35 | details,
36 | figcaption,
37 | figure,
38 | footer,
39 | header,
40 | hgroup,
41 | main,
42 | menu,
43 | nav,
44 | section,
45 | summary {
46 | display: block;
47 | }
48 |
49 | /**
50 | * 1. Correct `inline-block` display not defined in IE 8/9.
51 | * 2. Normalize vertical alignment of `progress` in Chrome, Firefox, and Opera.
52 | */
53 |
54 | audio,
55 | canvas,
56 | progress,
57 | video {
58 | display: inline-block; /* 1 */
59 | vertical-align: baseline; /* 2 */
60 | }
61 |
62 | /**
63 | * Prevent modern browsers from displaying `audio` without controls.
64 | * Remove excess height in iOS 5 devices.
65 | */
66 |
67 | audio:not([controls]) {
68 | display: none;
69 | height: 0;
70 | }
71 |
72 | /**
73 | * Address `[hidden]` styling not present in IE 8/9/10.
74 | * Hide the `template` element in IE 8/9/11, Safari, and Firefox < 22.
75 | */
76 |
77 | [hidden],
78 | template {
79 | display: none;
80 | }
81 |
82 | /* Links
83 | ========================================================================== */
84 |
85 | /**
86 | * Remove the gray background color from active links in IE 10.
87 | */
88 |
89 | a {
90 | background-color: transparent;
91 | }
92 |
93 | /**
94 | * Improve readability when focused and also mouse hovered in all browsers.
95 | */
96 |
97 | a:active,
98 | a:hover {
99 | outline: 0;
100 | }
101 |
102 | /* Text-level semantics
103 | ========================================================================== */
104 |
105 | /**
106 | * Address styling not present in IE 8/9/10/11, Safari, and Chrome.
107 | */
108 |
109 | abbr[title] {
110 | border-bottom: 1px dotted;
111 | }
112 |
113 | /**
114 | * Address style set to `bolder` in Firefox 4+, Safari, and Chrome.
115 | */
116 |
117 | b,
118 | strong {
119 | font-weight: bold;
120 | }
121 |
122 | /**
123 | * Address styling not present in Safari and Chrome.
124 | */
125 |
126 | dfn {
127 | font-style: italic;
128 | }
129 |
130 | /**
131 | * Address variable `h1` font-size and margin within `section` and `article`
132 | * contexts in Firefox 4+, Safari, and Chrome.
133 | */
134 |
135 | h1 {
136 | font-size: 2em;
137 | margin: 0.67em 0;
138 | }
139 |
140 | /**
141 | * Address styling not present in IE 8/9.
142 | */
143 |
144 | mark {
145 | background: #ff0;
146 | color: #000;
147 | }
148 |
149 | /**
150 | * Address inconsistent and variable font size in all browsers.
151 | */
152 |
153 | small {
154 | font-size: 80%;
155 | }
156 |
157 | /**
158 | * Prevent `sub` and `sup` affecting `line-height` in all browsers.
159 | */
160 |
161 | sub,
162 | sup {
163 | font-size: 75%;
164 | line-height: 0;
165 | position: relative;
166 | vertical-align: baseline;
167 | }
168 |
169 | sup {
170 | top: -0.5em;
171 | }
172 |
173 | sub {
174 | bottom: -0.25em;
175 | }
176 |
177 | /* Embedded content
178 | ========================================================================== */
179 |
180 | /**
181 | * Remove border when inside `a` element in IE 8/9/10.
182 | */
183 |
184 | img {
185 | border: 0;
186 | }
187 |
188 | /**
189 | * Correct overflow not hidden in IE 9/10/11.
190 | */
191 |
192 | svg:not(:root) {
193 | overflow: hidden;
194 | }
195 |
196 | /* Grouping content
197 | ========================================================================== */
198 |
199 | /**
200 | * Address margin not present in IE 8/9 and Safari.
201 | */
202 |
203 | figure {
204 | margin: 1em 40px;
205 | }
206 |
207 | /**
208 | * Address differences between Firefox and other browsers.
209 | */
210 |
211 | hr {
212 | -moz-box-sizing: content-box;
213 | box-sizing: content-box;
214 | height: 0;
215 | }
216 |
217 | /**
218 | * Contain overflow in all browsers.
219 | */
220 |
221 | pre {
222 | overflow: auto;
223 | }
224 |
225 | /**
226 | * Address odd `em`-unit font size rendering in all browsers.
227 | */
228 |
229 | code,
230 | kbd,
231 | pre,
232 | samp {
233 | font-family: monospace, monospace;
234 | font-size: 1em;
235 | }
236 |
237 | /* Forms
238 | ========================================================================== */
239 |
240 | /**
241 | * Known limitation: by default, Chrome and Safari on OS X allow very limited
242 | * styling of `select`, unless a `border` property is set.
243 | */
244 |
245 | /**
246 | * 1. Correct color not being inherited.
247 | * Known issue: affects color of disabled elements.
248 | * 2. Correct font properties not being inherited.
249 | * 3. Address margins set differently in Firefox 4+, Safari, and Chrome.
250 | */
251 |
252 | button,
253 | input,
254 | optgroup,
255 | select,
256 | textarea {
257 | color: inherit; /* 1 */
258 | font: inherit; /* 2 */
259 | margin: 0; /* 3 */
260 | }
261 |
262 | /**
263 | * Address `overflow` set to `hidden` in IE 8/9/10/11.
264 | */
265 |
266 | button {
267 | overflow: visible;
268 | }
269 |
270 | /**
271 | * Address inconsistent `text-transform` inheritance for `button` and `select`.
272 | * All other form control elements do not inherit `text-transform` values.
273 | * Correct `button` style inheritance in Firefox, IE 8/9/10/11, and Opera.
274 | * Correct `select` style inheritance in Firefox.
275 | */
276 |
277 | button,
278 | select {
279 | text-transform: none;
280 | }
281 |
282 | /**
283 | * 1. Avoid the WebKit bug in Android 4.0.* where (2) destroys native `audio`
284 | * and `video` controls.
285 | * 2. Correct inability to style clickable `input` types in iOS.
286 | * 3. Improve usability and consistency of cursor style between image-type
287 | * `input` and others.
288 | */
289 |
290 | button,
291 | html input[type="button"], /* 1 */
292 | input[type="reset"],
293 | input[type="submit"] {
294 | -webkit-appearance: button; /* 2 */
295 | cursor: pointer; /* 3 */
296 | }
297 |
298 | /**
299 | * Re-set default cursor for disabled elements.
300 | */
301 |
302 | button[disabled],
303 | html input[disabled] {
304 | cursor: default;
305 | }
306 |
307 | /**
308 | * Remove inner padding and border in Firefox 4+.
309 | */
310 |
311 | button::-moz-focus-inner,
312 | input::-moz-focus-inner {
313 | border: 0;
314 | padding: 0;
315 | }
316 |
317 | /**
318 | * Address Firefox 4+ setting `line-height` on `input` using `!important` in
319 | * the UA stylesheet.
320 | */
321 |
322 | input {
323 | line-height: normal;
324 | }
325 |
326 | /**
327 | * It's recommended that you don't attempt to style these elements.
328 | * Firefox's implementation doesn't respect box-sizing, padding, or width.
329 | *
330 | * 1. Address box sizing set to `content-box` in IE 8/9/10.
331 | * 2. Remove excess padding in IE 8/9/10.
332 | */
333 |
334 | input[type="checkbox"],
335 | input[type="radio"] {
336 | box-sizing: border-box; /* 1 */
337 | padding: 0; /* 2 */
338 | }
339 |
340 | /**
341 | * Fix the cursor style for Chrome's increment/decrement buttons. For certain
342 | * `font-size` values of the `input`, it causes the cursor style of the
343 | * decrement button to change from `default` to `text`.
344 | */
345 |
346 | input[type="number"]::-webkit-inner-spin-button,
347 | input[type="number"]::-webkit-outer-spin-button {
348 | height: auto;
349 | }
350 |
351 | /**
352 | * 1. Address `appearance` set to `searchfield` in Safari and Chrome.
353 | * 2. Address `box-sizing` set to `border-box` in Safari and Chrome
354 | * (include `-moz` to future-proof).
355 | */
356 |
357 | input[type="search"] {
358 | -webkit-appearance: textfield; /* 1 */
359 | -moz-box-sizing: content-box;
360 | -webkit-box-sizing: content-box; /* 2 */
361 | box-sizing: content-box;
362 | }
363 |
364 | /**
365 | * Remove inner padding and search cancel button in Safari and Chrome on OS X.
366 | * Safari (but not Chrome) clips the cancel button when the search input has
367 | * padding (and `textfield` appearance).
368 | */
369 |
370 | input[type="search"]::-webkit-search-cancel-button,
371 | input[type="search"]::-webkit-search-decoration {
372 | -webkit-appearance: none;
373 | }
374 |
375 | /**
376 | * Define consistent border, margin, and padding.
377 | */
378 |
379 | fieldset {
380 | border: 1px solid #c0c0c0;
381 | margin: 0 2px;
382 | padding: 0.35em 0.625em 0.75em;
383 | }
384 |
385 | /**
386 | * 1. Correct `color` not being inherited in IE 8/9/10/11.
387 | * 2. Remove padding so people aren't caught out if they zero out fieldsets.
388 | */
389 |
390 | legend {
391 | border: 0; /* 1 */
392 | padding: 0; /* 2 */
393 | }
394 |
395 | /**
396 | * Remove default vertical scrollbar in IE 8/9/10/11.
397 | */
398 |
399 | textarea {
400 | overflow: auto;
401 | }
402 |
403 | /**
404 | * Don't inherit the `font-weight` (applied by a rule above).
405 | * NOTE: the default cannot safely be changed in Chrome and Safari on OS X.
406 | */
407 |
408 | optgroup {
409 | font-weight: bold;
410 | }
411 |
412 | /* Tables
413 | ========================================================================== */
414 |
415 | /**
416 | * Remove most spacing between table cells.
417 | */
418 |
419 | table {
420 | border-collapse: collapse;
421 | border-spacing: 0;
422 | }
423 |
424 | td,
425 | th {
426 | padding: 0;
427 | }
428 |
429 | .color-picker,
430 | .color-picker *,
431 | .cp-saturation-spectrum,
432 | .cp-saturation-spectrum *,
433 | .cp-hue-spectrum,
434 | .cp-hue-spectrum * {
435 | box-sizing: border-box;
436 | }
437 | .cp-saturation-spectrum,
438 | .cp-hue-spectrum {
439 | position: relative;
440 | display: inline-block;
441 | }
442 | .cp-saturation-white,
443 | .cp-saturation-black {
444 | position: relative;
445 | width: 100%;
446 | height: 100%;
447 | }
448 | .cp-saturation-white {
449 | background: linear-gradient(to right, #fff, rgba(204,154,129,0));
450 | }
451 | .cp-saturation-black {
452 | background: linear-gradient(to top, #000, rgba(204,154,129,0));
453 | }
454 | .cp-saturation-spectrum {
455 | cursor: pointer;
456 | }
457 | .cp-saturation-spectrum .cp-saturation-drag {
458 | display: none;
459 | border: 1px solid #fff;
460 | border-radius: 10px;
461 | position: absolute;
462 | top: 0px;
463 | left: 0px;
464 | }
465 | .cp-saturation-spectrum .cp-saturation-drag .inner {
466 | position: relative;
467 | width: 100%;
468 | height: 100%;
469 | border: 1px solid #000;
470 | border-radius: 10px;
471 | }
472 | .cp-hue-spectrum {
473 | background: linear-gradient(to bottom, #f00 0%, #ff0 17%, #0f0 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00 100%);
474 | cursor: pointer;
475 | }
476 | .cp-hue-spectrum .cp-hue-drag {
477 | display: none;
478 | position: absolute;
479 | top: 0px;
480 | left: 0px;
481 | width: 100%;
482 | border: 1px solid #000;
483 | }
484 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "color-pairs-picker",
3 | "description": "Given a color, it picks a pleasing and well contrasted background and foreground colors",
4 | "version": "1.3.6",
5 | "author": "Kyle Mathews ",
6 | "bugs": {
7 | "url": "https://github.com/KyleAMathews/color-pairs-picker/issues"
8 | },
9 | "dependencies": {
10 | "chroma-js": "0.7.2",
11 | "is-object": "^1.0.1",
12 | "object-assign": "^2.0.0"
13 | },
14 | "devDependencies": {
15 | "chai": "^2.3.0",
16 | "cjsx-loader": "^2.0.1",
17 | "coffee-loader": "^0.7.2",
18 | "coffee-react": "^3.2.0",
19 | "coffee-script": "^1.9.2",
20 | "css-loader": "^0.12.1",
21 | "gulp": "^3.8.11",
22 | "mocha": "^2.2.5",
23 | "mocha-unfunk-reporter": "^0.4.0",
24 | "node-libs-browser": "^0.5.0",
25 | "pre-commit": "^1.0.7",
26 | "react": "^0.13.3",
27 | "react-color-picker": "^2.1.9",
28 | "react-hot-loader": "^1.2.7",
29 | "style-loader": "^0.12.2",
30 | "underscore": "^1.8.3",
31 | "webpack": "^1.9.7",
32 | "webpack-dev-server": "^1.8.2"
33 | },
34 | "directories": {
35 | "example": "examples"
36 | },
37 | "homepage": "https://github.com/KyleAMathews/color-pairs-picker",
38 | "keywords": [
39 | "color",
40 | "chroma",
41 | "text"
42 | ],
43 | "license": "MIT",
44 | "main": "dist/index.js",
45 | "repository": {
46 | "type": "git",
47 | "url": "https://github.com/KyleAMathews/color-pairs-picker.git"
48 | },
49 | "scripts": {
50 | "test-watch": "NODE_ENV=test node_modules/.bin/mocha -w --recursive --compilers coffee:coffee-script/register -R mocha-unfunk-reporter",
51 | "test": "NODE_ENV=test node_modules/.bin/mocha --recursive --compilers coffee:coffee-script/register -R mocha-unfunk-reporter",
52 | "watch": "./node_modules/.bin/webpack-dev-server --hot"
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/contrastSearch.coffee:
--------------------------------------------------------------------------------
1 | chroma = require 'chroma-js'
2 | #debug = require('debug')('color-pairs-picker-binary-search-contrast')
3 |
4 | counter = 0
5 |
6 | module.exports = contrastSearch = (targetColor, compareColor=targetColor, start=0, end=1, direction="end", contrast=5) ->
7 | counter += 1
8 | #console.log "\n"
9 | #console.log "new recurse"
10 |
11 | middle = (start + end) / 2
12 |
13 | #console.log targetColor, compareColor, start, end, middle, direction
14 |
15 | # Create new middle color.
16 | b = chroma(targetColor, 'lab')
17 | #console.log b.luminance(middle).css()
18 | color = b.luminance(middle).lab()
19 | #console.log "color", color
20 | #console.log chroma(color).css()
21 |
22 | # Calculate contrast.
23 | curContrast = chroma.contrast(chroma(compareColor).css(), b.css())
24 | #console.log "contrast", curContrast
25 |
26 | if counter > 15
27 | #console.log "too many cycles!!!", counter
28 | counter = 0
29 | #return color
30 | return color
31 |
32 | # Found!
33 | if (contrast - 0.1) < curContrast and curContrast < (contrast + 0.1)
34 | #console.log "found!"
35 | #console.log 'cycles', counter
36 | counter = 0
37 | return color
38 |
39 | # Contrast is too high
40 | if curContrast > contrast
41 | #console.log "too high"
42 | if direction is "end"
43 | contrastSearch(targetColor, compareColor, start, middle-0.001, direction, contrast)
44 | else
45 | contrastSearch(targetColor, compareColor, middle+0.001, end, direction, contrast)
46 | else if curContrast < contrast
47 | #console.log "too low"
48 | if direction is "start"
49 | contrastSearch(targetColor, compareColor, start, middle-0.001, direction, contrast)
50 | else
51 | contrastSearch(targetColor, compareColor, middle+0.001, end, direction, contrast)
52 |
53 | color = 'rgb(8, 27, 44)'
54 | targetColor = 'rgb(124, 171, 217)'
55 | color = 'red'
56 | targetColor = 'red'
57 | #console.log "the perfect contrast:", contrastSearch(targetColor, color, 0, .97, "end", 4.5)
58 |
--------------------------------------------------------------------------------
/src/index.coffee:
--------------------------------------------------------------------------------
1 | chroma = require 'chroma-js'
2 | #debug = require('debug')('color-pairs-picker')
3 | objectAssign = require 'object-assign'
4 | contrastSearch = require './contrastSearch'
5 | isObject = require 'is-object'
6 |
7 | module.exports = colorPicker = (color, targetColor, options={}) ->
8 | unless targetColor?
9 | targetColor = color
10 |
11 | if isObject(targetColor)
12 | options = targetColor
13 | targetColor = color
14 |
15 | options = objectAssign({
16 | colorIsBackground: true
17 | contrast: 5
18 | foregroundMax: 0.98
19 | foregroundMin: 0.02
20 | backgroundMax: 0.85
21 | backgroundMin: 0.15
22 | direction: undefined
23 | }, options)
24 |
25 | # Get start/end
26 | if options.colorIsBackground
27 | start = options.foregroundMin
28 | end = options.foregroundMax
29 | else
30 | start = options.backgroundMin
31 | end = options.backgroundMax
32 |
33 | #console.log "start,end", start,end
34 | #console.log "luminance of base color is:", chroma(color, 'lab').luminance()
35 | # Decide which direction to go.
36 | if options.direction?
37 | secondColor = contrastSearch(targetColor, color, start, end, options.direction, options.contrast)
38 | else
39 | # Decide which direction to search for the second color.
40 | endColor = chroma(targetColor, 'lab').luminance(end)
41 | startColor = chroma(targetColor, 'lab').luminance(start)
42 | #console.log startColor, endColor
43 | contrastToStart = chroma.contrast(color, startColor.css())
44 | #console.log "contrast to start", contrastToStart
45 | contrastToEnd = chroma.contrast(color, endColor.css())
46 | #console.log "contrast to end", contrastToEnd
47 |
48 | if contrastToEnd > options.contrast
49 | #console.log "Finding second color in direction of end"
50 | secondColor = contrastSearch(targetColor, color, start, end, 'end', options.contrast)
51 | else if contrastToStart > options.contrast
52 | #console.log "Finding second color in direction of start"
53 | secondColor = contrastSearch(targetColor, color, start, end, 'start', options.contrast)
54 | else
55 | #console.log "contrast isn't high enough, modifying base color now from direction
56 | #of highest contrast endpoint"
57 | # We can't get the second color to the proper contrast without modifying
58 | # the base color. So we'll chose the endpoint with the highest contrast
59 | # and make that the new base color.
60 |
61 | # Start and end are now based on either the foreground if a background
62 | # color was passed in or vise versa.
63 | if options.colorIsBackground
64 | newStart = options.backgroundMin
65 | newEnd = options.backgroundMax
66 | else
67 | newStart = options.foregroundMin
68 | newEnd = options.foregroundMax
69 |
70 | if contrastToEnd > contrastToStart
71 | secondColor = chroma(targetColor, 'lab').luminance(end)
72 | #console.log "contrast to end is highest. new second color is", secondColor
73 |
74 | highestPossibleContrast = chroma.contrast(secondColor.css(), chroma(color, 'lab').luminance(newStart))
75 | #console.log 'possible contrast', highestPossibleContrast
76 | if highestPossibleContrast > options.contrast
77 | color = contrastSearch(color, secondColor.css(), newStart, newEnd, 'start', options.contrast)
78 | else
79 | color = chroma(color, 'lab').luminance(newStart).lab()
80 |
81 | secondColor = secondColor.lab()
82 | else
83 | secondColor = chroma(color, 'lab').luminance(start)
84 | #console.log "contrast to start is highest. new second color is", secondColor
85 |
86 | highestPossibleContrast = chroma.contrast(secondColor.css(), chroma(color, 'lab').luminance(newEnd))
87 | #console.log 'possible contrast', highestPossibleContrast
88 | if highestPossibleContrast > options.contrast
89 | color = contrastSearch(color, secondColor.css(), newStart, newEnd, 'end', options.contrast)
90 | else
91 | color = chroma(color, 'lab').luminance(newEnd).lab()
92 |
93 | secondColor = secondColor.lab()
94 |
95 | #console.log color, secondColor
96 |
97 | if options.colorIsBackground
98 | bg = color
99 | fg = secondColor
100 | else
101 | bg = secondColor
102 | fg = color
103 |
104 |
105 |
106 | return {bg: chroma.lab(bg).css(), fg: chroma.lab(fg).css()}
107 |
108 | #console.log colorPicker('#007EE5', '#007EE5', contrast: 4.5)
109 |
--------------------------------------------------------------------------------
/test/tests.coffee:
--------------------------------------------------------------------------------
1 | chai = require 'chai'
2 | expect = chai.expect
3 | _ = require 'underscore'
4 | chroma = require 'chroma-js'
5 |
6 | colorPicker = require '../src/'
7 | contrastSearch = require '../src/contrastSearch'
8 |
9 | describe 'contrastSearch', ->
10 | it 'should exist', ->
11 | expect(contrastSearch).to.exist
12 |
13 | it 'should return a lab value', ->
14 | expect(contrastSearch('blue')).to.be.array
15 | expect(contrastSearch('blue').length).to.equal(3)
16 |
17 | it 'should return color with contrast that is within 0.1 of what is requested', ->
18 | color = "blue"
19 | result = chroma.lab(contrastSearch(color))
20 | contrast = chroma.contrast(result, chroma(color))
21 | expect(4.9 <= contrast <= 5.1).to.be.true
22 |
23 | it 'should return results for impossibly high contrast', ->
24 | result = chroma.lab(contrastSearch('black', 'black', 0, 1, 'end', 100))
25 | expect(result.css()).to.exist
26 |
27 | describe 'color pairs picker', ->
28 | it 'should exist', ->
29 | expect(colorPicker).to.exist
30 |
31 | it 'should return an object', ->
32 | expect(colorPicker('blue')).to.be.object
33 |
34 | it 'should work when asking for low contrast between colors', ->
35 | color = "blue"
36 | result = colorPicker(color, contrast: 3)
37 | contrast = chroma.contrast(result.bg, result.fg)
38 | #console.log color, result, contrast
39 | # We give ourselves a bit more leniency as converting from lab -> hex is
40 | # lossy so contrast can change slightly.
41 | expect(2.9 <= contrast <= 3.1).to.be.true
42 |
43 | it 'should work when asking for high contrast between colors', ->
44 | color = "lightgrey"
45 | result = colorPicker(color, contrast: 12)
46 | contrast = chroma.contrast(result.bg, result.fg)
47 | # We give ourselves a bit more leniency as converting from lab -> hex is
48 | # lossy so contrast can change slightly.
49 | expect(11.9 <= contrast <= 12.1).to.be.true
50 |
51 | it 'should return color with contrast that is within 0.1 of what is requested', ->
52 | colorPickerTester = (color) ->
53 | result = colorPicker(color)
54 | contrast = chroma.contrast(result.bg, result.fg)
55 | #console.log color, result, contrast
56 | # We give ourselves a bit more leniency as converting from lab -> hex is
57 | # lossy so contrast can change slightly.
58 | expect(4.9 <= contrast <= 5.1).to.be.true
59 |
60 | colorPickerTester('blue')
61 | colorPickerTester('pink')
62 | colorPickerTester('white')
63 | colorPickerTester('black')
64 | colorPickerTester('purple')
65 | colorPickerTester('red')
66 | colorPickerTester('orange')
67 | colorPickerTester('green')
68 | colorPickerTester('aqua')
69 | colorPickerTester('fuchsia')
70 | colorPickerTester('gray')
71 | colorPickerTester('lime')
72 | colorPickerTester('maroon')
73 | colorPickerTester('navy')
74 | colorPickerTester('olive')
75 | colorPickerTester('teal')
76 | colorPickerTester('yellow')
77 |
--------------------------------------------------------------------------------
/webpack.config.js:
--------------------------------------------------------------------------------
1 | var path = require('path');
2 | var webpack = require('webpack');
3 |
4 | module.exports = {
5 | entry: [
6 | "webpack-dev-server/client?http://0.0.0.0:8080",
7 | 'webpack/hot/only-dev-server',
8 | './examples/index'
9 | ],
10 | devServer: {
11 | contentBase: './examples/'
12 | },
13 | devtool: "eval",
14 | debug: true,
15 | output: {
16 | path: path.join(__dirname, 'examples'),
17 | filename: 'bundle.js',
18 | },
19 | resolveLoader: {
20 | modulesDirectories: ['node_modules']
21 | },
22 | plugins: [
23 | new webpack.HotModuleReplacementPlugin(),
24 | new webpack.NoErrorsPlugin(),
25 | new webpack.IgnorePlugin(/un~$/)
26 | ],
27 | resolve: {
28 | extensions: ['', '.js', '.cjsx', '.coffee']
29 | },
30 | module: {
31 | loaders: [
32 | { test: /\.css$/, loaders: ['style', 'css']},
33 | { test: /\.cjsx$/, loaders: ['react-hot', 'coffee', 'cjsx']},
34 | { test: /\.coffee$/, loader: 'coffee' }
35 | ]
36 | }
37 | };
38 |
--------------------------------------------------------------------------------
/webpack.production.config.js:
--------------------------------------------------------------------------------
1 | var path = require('path');
2 | var webpack = require('webpack');
3 |
4 |
5 | module.exports = {
6 | entry: [
7 | './examples/index'
8 | ],
9 | output: {
10 | path: path.join(__dirname, 'examples'),
11 | filename: 'bundle.js',
12 | },
13 | resolveLoader: {
14 | modulesDirectories: ['node_modules']
15 | },
16 | plugins: [
17 | new webpack.DefinePlugin({
18 | "process.env": {
19 | NODE_ENV: JSON.stringify("production")
20 | }
21 | }),
22 | new webpack.optimize.DedupePlugin(),
23 | new webpack.optimize.UglifyJsPlugin()
24 | ],
25 | resolve: {
26 | extensions: ['', '.js', '.cjsx', '.coffee']
27 | },
28 | module: {
29 | loaders: [
30 | { test: /\.css$/, loaders: ['style', 'css']},
31 | { test: /\.cjsx$/, loaders: ['coffee', 'cjsx']},
32 | { test: /\.coffee$/, loader: 'coffee' }
33 | ]
34 | }
35 | };
36 |
--------------------------------------------------------------------------------