Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit d1edc61234c3906ced19700e7572e03daf7feae1 @skratchdot committed Jun 21, 2014
Showing with 497 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +14 −0 .jshintrc
  3. +9 −0 .npmignore
  4. +6 −0 .travis.yml
  5. +22 −0 LICENSE-MIT
  6. +149 −0 README.md
  7. +32 −0 gulpfile.js
  8. +132 −0 lib/color-harmony.js
  9. +62 −0 package.json
  10. +70 −0 test/color-harmony_test.js
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,14 @@
+{
+ "curly": true,
+ "eqeqeq": true,
+ "immed": true,
+ "latedef": "nofunc",
+ "newcap": true,
+ "noarg": true,
+ "sub": true,
+ "undef": true,
+ "unused": true,
+ "boss": true,
+ "eqnull": true,
+ "node": true
+}
@@ -0,0 +1,9 @@
+.git*
+_ignore/
+build/
+test/
+.DS_Store
+.npm-debug.log
+.project
+.travis.yml
+TODO.md
@@ -0,0 +1,6 @@
+language: node_js
+node_js:
+ - 0.8
+ - '0.10'
+before_script:
+ - npm install -g gulp
@@ -0,0 +1,22 @@
+Copyright (c) 2014 skratchdot
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,149 @@
+# color-harmony
+
+[![NPM version](https://badge.fury.io/js/color-harmony.svg)](http://badge.fury.io/js/color-harmony)
+[![Build Status](https://travis-ci.org/skratchdot/color-harmony.png?branch=master)](https://travis-ci.org/skratchdot/color-harmony)
+[![Code Climate](https://codeclimate.com/github/skratchdot/color-harmony.png)](https://codeclimate.com/github/skratchdot/color-harmony)
+[![Coverage Status](https://coveralls.io/repos/skratchdot/color-harmony/badge.png)](https://coveralls.io/r/skratchdot/color-harmony)
+[![Dependency Status](https://david-dm.org/skratchdot/color-harmony.svg)](https://david-dm.org/skratchdot/color-harmony)
+[![devDependency Status](https://david-dm.org/skratchdot/color-harmony/dev-status.svg)](https://david-dm.org/skratchdot/color-harmony#info=devDependencies)
+
+[![NPM](https://nodei.co/npm/color-harmony.svg)](https://npmjs.org/package/color-harmony)
+
+
+## Description
+
+A javascript library that creates color scales/harmonies by rotating the hue of the given color.
+
+There are also helper methods to create shades (mixing w/ black), tints (mixing w/ white), and
+tones (mixing w/ middle gray).
+
+
+## Getting Started
+
+Install the module with: `npm install color-harmony`
+
+```javascript
+var Harmonizer = require('color-harmony').Harmonizer;
+var harmonizer = new Harmonizer();
+harmonizer.harmonizeAll('#c820f1'); // returns a map of scales
+harmonizer.harmonize('#000', 'complementary'); // returns ['#000000', '#ffffff']
+```
+
+
+## Documentation
+
+This library uses the [onecolor](https://github.com/One-com/one-color) parser,
+so colorString can in many different formats (i.e. #ff00cc, rgb(13,42,255), etc).
+
+#### harmonizer.add(harmonyName, degreeArray)
+
+Add a new named harmony. If the harmony name already exists, it will
+be overwritten. If degreeArray is not an array of numbers, then the
+harmony will not be added.
+
+#### harmonizer.harmonizeAll(colorString)
+
+Return a map of all the harmonies for the given color string.
+
+#### harmonizer.harmonize(colorString, harmony)
+
+Return an array of hex codes based on the given color string and harmony.
+
+The harmony argument can be a 'named harmony', or it can be a custom harmony
+by passing in an array of numbers (degrees 0-360).
+
+#### harmonizer.shades(colorString, size)
+
+Return an array of hex codes container the shades of a given color (i.e. mix it with black - #000000).
+
+If size is not a valid number, then the array will default to a size of 10.
+
+#### harmonizer.tints(colorString, size)
+
+Return an array of hex codes container the tints of a given color (i.e. mix it with white - #FFFFFF).
+
+If size is not a valid number, then the array will default to a size of 10.
+
+#### harmonizer.tones(colorString, size)
+
+Return an array of hex codes container the tones of a given color (i.e. mix it with middle gray - #777777).
+
+If size is not a valid number, then the array will default to a size of 10.
+
+### Available Harmony Names (and their associated degrees of hue rotation)
+
+- complementary: [0,180]
+- splitComplementary: [0,150,320]
+- splitComplementaryCW: [0,150,300]
+- splitComplementaryCCW: [0,60,210]
+- triadic: [0,120,240]
+- clash: [0,90,270]
+- tetradic: [0,90,180,270]
+- fourToneCW: [0,60,180,240]
+- fourToneCCW: [0,120,180,300]
+- fiveToneA: [0,115,155,205,245]
+- fiveToneB: [0,40,90,130,245]
+- fiveToneC: [0,50,90,205,320]
+- fiveToneD: [0,40,155,270,310]
+- fiveToneE: [0,115,230,270,320]
+- sixToneCW: [0,30,120,150,240,270]
+- sixToneCCW: [0,90,120,210,240,330]
+- neutral: [0,15,30,45,60,75]
+- analogous: [0,30,60,90,120,150]
+
+**NOTE:** This list was compiled based on the data in [color-js](https://github.com/brehaut/color-js)
+
+
+## Examples
+
+Get all the stored harmonies
+```javascript
+harmonizer.harmonizeAll('#c820f1'); // returns a map of scales
+```
+
+Get a named harmony
+```javascript
+harmonizer.harmonize('#000', 'complementary'); // returns ['#000000', '#ffffff']
+```
+
+Get a custom harmony
+```javascript
+harmonizer.harmonize('#000', [0, 10, 20, 30, 40]); // returns an array of colors
+```
+
+Add a custom harmony that can later be referenced by name
+```javascript
+harmonizer.add('foo', [0, 25, 45]);
+harmonizer.harmonize('#00cc00', 'foo'); // returns your custom data
+```
+
+Return the shades of a given color (mix it with black - #000000)
+```javascript
+harmonizer.shades('#925719'); // returns 10 by default
+harmonizer.shades('#925719', 14); // you can return a custom amount (an array of 14)
+```
+
+Return the tints of a given color (mix it with white - #FFFFFF)
+```javascript
+harmonizer.tints('#925719'); // returns 10 by default
+harmonizer.tints('#925719', 14); // you can return a custom amount (an array of 14)
+```
+
+Return the tones of a given color (mix it with middle gray - #777777)
+```javascript
+harmonizer.tones('#925719'); // returns 10 by default
+harmonizer.tones('#925719', 14); // you can return a custom amount (an array of 14)
+```
+
+
+## Release History
+
+#### v0.1.0 - Released June 21, 2014
+
+- initial release
+
+
+## License
+
+Copyright (c) 2014 skratchdot
+Licensed under the MIT license.
@@ -0,0 +1,32 @@
+var gulp = require('gulp');
+var jshint = require('gulp-jshint');
+var nodeunit = require('gulp-nodeunit');
+
+var jshintTask = function (task, paths) {
+ gulp.task(task, function () {
+ gulp.src(paths)
+ .pipe(jshint())
+ .pipe(jshint.reporter('default'));
+ });
+};
+
+gulp.task('test', function () {
+ gulp.src('test/**/*.js')
+ .pipe(nodeunit());
+});
+
+gulp.task('watch', function () {
+ gulp.watch(['lib/**/*.js', 'test/**/*.js'], ['jshint:lib', 'jshint:test', 'test']);
+ gulp.watch(['*.js', '*.json'], ['jshint:root']);
+});
+
+// setup tasks
+jshintTask('jshint:root', ['*.js', '*.json']);
+jshintTask('jshint:lib', 'lib/**/*.js');
+jshintTask('jshint:test', 'test/**/*.js');
+gulp.task('default', ['jshint:root', 'jshint:lib', 'jshint:test', 'test', 'watch']);
+
+// handle errors
+process.on('uncaughtException', function (e) {
+ console.error(e);
+});
@@ -0,0 +1,132 @@
+/*
+ * color-harmony
+ * https://github.com/skratchdot/color-harmony
+ *
+ * Copyright (c) 2014 skratchdot
+ * Licensed under the MIT license.
+ */
+
+'use strict';
+
+var onecolor = require('onecolor');
+var util = require('util');
+
+var Harmonizer = function () {
+ var api = this;
+
+ /* degree arrays taken from: https://github.com/brehaut/color-js/ */
+ var harmonies = {
+ complementary: [0,180],
+ splitComplementary: [0,150,320],
+ splitComplementaryCW: [0,150,300],
+ splitComplementaryCCW: [0,60,210],
+ triadic: [0,120,240],
+ clash: [0,90,270],
+ tetradic: [0,90,180,270],
+ fourToneCW: [0,60,180,240],
+ fourToneCCW: [0,120,180,300],
+ fiveToneA: [0,115,155,205,245],
+ fiveToneB: [0,40,90,130,245],
+ fiveToneC: [0,50,90,205,320],
+ fiveToneD: [0,40,155,270,310],
+ fiveToneE: [0,115,230,270,320],
+ sixToneCW: [0,30,120,150,240,270],
+ sixToneCCW: [0,90,120,210,240,330],
+ neutral: [0,15,30,45,60,75],
+ analogous: [0,30,60,90,120,150]
+ };
+
+ var parseColor = function (colorString) {
+ var color = onecolor(colorString);
+ if (!color) {
+ color = onecolor('#000000');
+ }
+ return color;
+ };
+
+ var harmonize = function (color, degrees) {
+ var ret = [], h, s, l, a, i, degree;
+ h = color.hue();
+ s = color.saturation();
+ l = color.lightness();
+ a = color.alpha();
+ for (i = 0; i < degrees.length; i++) {
+ degree = degrees[i];
+ if (isFinite(degree) && typeof degree === 'number') {
+ ret.push(new onecolor.HSL((h + (1 / 360 * degree)) % 1, s, l, a).hex());
+ }
+ }
+ return ret;
+ };
+
+ var scaleTo = function (color, size, scale) {
+ var i, ret = [], r, g, b, a, scaleR, scaleG, scaleB;
+ if (!isFinite(size) || typeof size !== 'number') {
+ size = 10;
+ }
+ r = color.red();
+ g = color.green();
+ b = color.blue();
+ a = color.alpha();
+ scaleR = (scale - r) / size;
+ scaleG = (scale - g) / size;
+ scaleB = (scale - b) / size;
+ for (i = 0; i < size; i++) {
+ ret.push(new onecolor.RGB(r, g, b, a).hex());
+ r += scaleR;
+ g += scaleG;
+ b += scaleB;
+ }
+ return ret;
+ };
+
+ api.add = function (harmonyName, degreeArray) {
+ if (util.isArray(degreeArray)) {
+ harmonies[harmonyName] = degreeArray;
+ }
+ };
+
+ api.harmonizeAll = function (colorString) {
+ var ret = {};
+ var color = parseColor(colorString);
+ for (var harmonyName in harmonies) {
+ if (harmonies.hasOwnProperty(harmonyName)) {
+ ret[harmonyName] = harmonize(color, harmonies[harmonyName]);
+ }
+ }
+ return ret;
+ };
+
+ api.harmonize = function (colorString, harmony) {
+ var color = parseColor(colorString);
+ if (harmonies.hasOwnProperty(harmony)) {
+ harmony = harmonies[harmony];
+ }
+ if (util.isArray(harmony)) {
+ return harmonize(color, harmony);
+ } else {
+ return [];
+ }
+ };
+
+ // mix with black (#000000)
+ api.shades = function (colorString, size) {
+ return scaleTo(parseColor(colorString), size, 0);
+ };
+
+ // mix with white (#ffffff)
+ api.tints = function (colorString, size) {
+ return scaleTo(parseColor(colorString), size, 1);
+ };
+
+ // mix with middle gray (#777777)
+ api.tones = function (colorString, size) {
+ return scaleTo(parseColor(colorString), size, 0.5);
+ };
+
+ return api;
+};
+
+exports.Harmonizer = function () {
+ return new Harmonizer();
+};
Oops, something went wrong.

0 comments on commit d1edc61

Please sign in to comment.