diff --git a/README.md b/README.md new file mode 100644 index 0000000..42824c2 --- /dev/null +++ b/README.md @@ -0,0 +1,88 @@ +# golden-colors.js + +Random color generation for JavaScript and Node.js based on [How To Generate Random Colors Programmatically](http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/) by __Martin Ankerl__. Colors generated using Golden ratio conjugate can be used as background colors for easily readable labels, buttons, etc. + +# Installation + +For browser just download zip and include `golden-colors.js` or `golden-colors.min.js` in your HTML. + +Or you can download using Bower: + +``` +bower install golden-colors +``` + +For Node.js: + +``` +npm install golden-colors +``` + +and require it in your code: + +```js +var goldenColors = require('golden-colors'); +``` + +# Usage + +## GoldenColors + +There are three methods for color generation. All three methods return an instance of [WebColor](#webcolor). + +### getNaive() + +Just random values for R, G, B. + +### getHsvSimple(s, v) + +Random color in [HSV](https://en.wikipedia.org/wiki/HSL_and_HSV) system with random values for hue with specified saturation and value. + +### getHsvGolden(s, v) + +Random color in [HSV](https://en.wikipedia.org/wiki/HSL_and_HSV) system with random values for hue normalized using [Golden ratio conjugate](https://en.wikipedia.org/wiki/Golden_ratio#Golden_ratio_conjugate) with specified saturation and value. + +## WebColor + +An object with R, G, and B values that can be converted into CSS2-compatible string. + +### toRgb() + +Returns an Array of `[r, g, b]` with decimal values. + +### toRgbString() + +Returns a String of `rgb(r, g, b)` with decimal values. + +### toHexString() +### toString() +### toJSON() + +Returns a String of `#rgb` with hexadecimal values. + +# Examples + +See [https://alex7kom.github.io/golden-colors](https://alex7kom.github.io/golden-colors) + +# License + +The MIT License (MIT) + +Copyright (c) 2014 Alexey Komarov + +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. \ No newline at end of file diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..aa504bf --- /dev/null +++ b/bower.json @@ -0,0 +1,30 @@ +{ + "name": "golden-colors", + "version": "1.0.0", + "authors": [ + "Alexey Komarov " + ], + "description": "Generate random colors using Golden ratio conjugate", + "main": "golden-colors.js", + "moduleType": [ + "globals", + "node" + ], + "keywords": [ + "random", + "color" + ], + "homepage": "https://alex7kom.github.io/golden-colors", + "repository": { + "type": "git", + "url": "https://github.com/Alex7Kom/golden-colors.git" + }, + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ] +} diff --git a/example.html b/example.html new file mode 100644 index 0000000..aed3bd9 --- /dev/null +++ b/example.html @@ -0,0 +1,177 @@ + + + + golden-colors.js + + + + +
+
+
+
+

golden-colors.js

+ +

Random color generation for JavaScript and Node.js based on How To Generate Random Colors Programmatically by Martin Ankerl. Colors generated using Golden ratio conjugate can be used as background colors for easily readable labels, buttons, etc.

+ +

golden-colors.js on Github

+ +

Installation

+ +

For browser just download zip and include golden-colors.js or golden-colors.min.js in your HTML.

+ +

Or you can download using Bower:

+ +

+

bower install golden-colors
+

+ +

For Node.js:

+ +

+

npm install golden-colors
+

+ +

and require it in your code:

+ +

+

var goldenColors = require('golden-colors');
+

+ +

Usage

+ +

GoldenColors

+ +

There are three methods for color generation. All three methods return an instance of WebColor.

+ +

getNaive()

+ +

Just random values for R, G, B.

+ +

+

goldenColors.getNaive()
+

+ +
+ +

getHsvSimple(s, v)

+ +

Random color in HSV system with random values for hue with specified saturation and value.

+ +

+

goldenColors.getHsvSimple(0.5, 0.95)
+

+ +
+ +

getHsvGolden(s, v)

+ +

Random color in HSV system with random values for hue normalized using Golden ratio conjugate with specified saturation and value.

+ +

+

goldenColors.getHsvGolden(0.5, 0.95)
+

+ +
+

+

goldenColors.getHsvGolden(0.99, 0.99)
+

+ +
+

+

goldenColors.getHsvGolden(0.25, 0.8)
+

+ +
+

+

goldenColors.getHsvGolden(0.3, 0.99)
+

+ +
+ +

WebColor

+ +

An object with R, G, and B values that can be converted into CSS2-compatible string.

+ +

toRgb()

+ +

Returns an Array of [r, g, b] with decimal values.

+

+

[255, 255, 255]
+

+ +

toRgbString()

+ +

Returns a String of rgb(r, g, b) with decimal values.

+

+

rgb(255, 255, 255)
+

+ +

toHexString()

+

toString()

+

toJSON()

+ +

Returns a String of #rgb with hexadecimal values.

+

+

#ffffff
+

+ +

License

+ +

The MIT License (MIT)

+ +

Copyright (c) 2014 Alexey Komarov <alex7kom@gmail.com>

+ +

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.

+
+
+
+
+ + + + + + + \ No newline at end of file diff --git a/golden-colors.js b/golden-colors.js new file mode 100644 index 0000000..cf02883 --- /dev/null +++ b/golden-colors.js @@ -0,0 +1,99 @@ +/* +* golden-colors.js +* Generate random colors using Golden ratio conjugate +* Based on http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/ +* MIT License +* Copyright (c) 2014 Alexey Komarov +*/ + +(function() { + + function getRandom255() { + return Math.round(Math.random() * 255); + } + + function convertDecToHex(number) { + var result = number.toString(16); + if (result.length == 1) { + result = '0' + result; + } + return result; + } + + function convertHsvToRgb(h, s, v) { + var r, g, b; + + var i = Math.floor(h * 6); + var f = h * 6 - i; + var p = v * (1 - s); + var q = v * (1 - f * s); + var t = v * (1 - (1 - f) * s); + + switch(i % 6){ + case 0: r = v; g = t; b = p; break; + case 1: r = q; g = v; b = p; break; + case 2: r = p; g = v; b = t; break; + case 3: r = p; g = q; b = v; break; + case 4: r = t; g = p; b = v; break; + case 5: r = v; g = p; b = q; break; + } + console.log(r, g, b); + return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)]; + } + + function WebColor(r, g, b) { + this.r = r; + this.g = g; + this.b = b; + } + + WebColor.prototype.toRgb = function() { + return [this.r, this.g, this.b]; + }; + + WebColor.prototype.toRgbString = function() { + return 'rgb(' + this.r + ', ' + this.g + ', ' + this.b + ')'; + }; + + WebColor.prototype.toHexString = function() { + return '#' + + convertDecToHex(this.r) + + convertDecToHex(this.g) + + convertDecToHex(this.b); + }; + + WebColor.prototype.toString = function() { + return this.toHexString(); + }; + + WebColor.prototype.toJSON = function() { + return this.toHexString(); + }; + + var GoldenColors = function() { + this.goldenRatioConjugate = 0.618033988749895; + }; + + GoldenColors.prototype.getNaive = function() { + return new WebColor(getRandom255(), getRandom255(), getRandom255()); + }; + + GoldenColors.prototype.getHsvSimple = function(s, v) { + var color = convertHsvToRgb(Math.random(), s, v); + return new WebColor(color[0], color[1], color[2]); + }; + + GoldenColors.prototype.getHsvGolden = function(s, v) { + var h = Math.random(); + h += this.goldenRatioConjugate; + h %= 1; + var color = convertHsvToRgb(h, s, v); + return new WebColor(color[0], color[1], color[2]); + }; + + if (typeof window !== 'undefined') { + window.goldenColors = new GoldenColors(); + } else if (typeof module !== 'undefined') { + module.exports = new GoldenColors(); + } +})(); \ No newline at end of file diff --git a/golden-colors.min.js b/golden-colors.min.js new file mode 100644 index 0000000..8bd959b --- /dev/null +++ b/golden-colors.min.js @@ -0,0 +1,8 @@ +/* +* golden-colors.js +* Generate random colors using Golden ratio conjugate +* Based on http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/ +* MIT License +* Copyright (c) 2014 Alexey Komarov +*/ +!function(){function getRandom255(){return Math.round(255*Math.random())}function convertDecToHex(number){var result=number.toString(16);return 1==result.length&&(result="0"+result),result}function convertHsvToRgb(h,s,v){var r,g,b,i=Math.floor(6*h),f=6*h-i,p=v*(1-s),q=v*(1-f*s),t=v*(1-(1-f)*s);switch(i%6){case 0:r=v,g=t,b=p;break;case 1:r=q,g=v,b=p;break;case 2:r=p,g=v,b=t;break;case 3:r=p,g=q,b=v;break;case 4:r=t,g=p,b=v;break;case 5:r=v,g=p,b=q}return console.log(r,g,b),[Math.round(255*r),Math.round(255*g),Math.round(255*b)]}function WebColor(r,g,b){this.r=r,this.g=g,this.b=b}WebColor.prototype.toRgb=function(){return[this.r,this.g,this.b]},WebColor.prototype.toRgbString=function(){return"rgb("+this.r+", "+this.g+", "+this.b+")"},WebColor.prototype.toHexString=function(){return"#"+convertDecToHex(this.r)+convertDecToHex(this.g)+convertDecToHex(this.b)},WebColor.prototype.toString=function(){return this.toHexString()},WebColor.prototype.toJSON=function(){return this.toHexString()};var GoldenColors=function(){this.goldenRatioConjugate=.618033988749895};GoldenColors.prototype.getNaive=function(){return new WebColor(getRandom255(),getRandom255(),getRandom255())},GoldenColors.prototype.getHsvSimple=function(s,v){var color=convertHsvToRgb(Math.random(),s,v);return new WebColor(color[0],color[1],color[2])},GoldenColors.prototype.getHsvGolden=function(s,v){var h=Math.random();h+=this.goldenRatioConjugate,h%=1;var color=convertHsvToRgb(h,s,v);return new WebColor(color[0],color[1],color[2])},"undefined"!=typeof window?window.goldenColors=new GoldenColors:"undefined"!=typeof module&&(module.exports=new GoldenColors)}(); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..88dd181 --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "golden-colors", + "version": "1.0.0", + "description": "Generate random colors using Golden ratio conjugate", + "main": "golden-colors.js", + "keywords": [ + "random", + "color" + ], + "repository": { + "type": "git", + "url": "https://github.com/Alex7Kom/golden-colors.git" + }, + "author": "Alexey Komarov ", + "license": "MIT" +}