├── .editorconfig ├── .gitignore ├── .jshintrc ├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── img ├── 1.png ├── 2.png └── 3.png ├── index.js ├── package.json └── test ├── index.js └── spec └── index.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # Unix-style newlines with a newline ending every file 7 | [*] 8 | end_of_line = lf 9 | charset = utf-8 10 | insert_final_newline = true 11 | trim_trailing_whitespace = true 12 | 13 | # Set default charset 14 | [*.js] 15 | indent_style = space 16 | indent_size = 2 17 | 18 | [{package.json,.travis.yml}] 19 | indent_style = space 20 | indent_size = 2 21 | 22 | [*.md] 23 | trim_trailing_whitespace = false 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea 3 | npm-debug.log* 4 | 5 | /node_modules 6 | /test/coverage 7 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "strict": true, 3 | "node": true, 4 | "curly": true, 5 | "eqeqeq": true, 6 | "immed": true, 7 | "newcap": true, 8 | "noarg": true, 9 | "sub": true, 10 | "undef": true, 11 | "boss": true, 12 | "eqnull": true, 13 | "laxbreak": true, 14 | "latedef": "nofunc" 15 | } 16 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .DS_Store 3 | .editorconfig 4 | .gitignore 5 | .npmignore 6 | .jshintrc 7 | .travis.yml 8 | 9 | npm-debug.log 10 | 11 | /node_modules 12 | /test 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - 0.12 5 | 6 | cache: 7 | directories: 8 | - node_modules 9 | 10 | install: npm install 11 | 12 | script: 13 | - npm test 14 | 15 | after_script: 16 | - npm run test:rpt 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 W.Y. 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # random-color 2 | 3 | > Generate random colors based on the golden ratio, which are more visually pleasing. 4 | 5 | [![MIT License](https://img.shields.io/badge/license-MIT_License-green.svg?style=flat-square)](https://github.com/mock-end/random-color/blob/master/LICENSE) 6 | 7 | [![build:?](https://img.shields.io/travis/mock-end/random-color/master.svg?style=flat-square)](https://travis-ci.org/mock-end/random-color) 8 | [![coverage:?](https://img.shields.io/coveralls/mock-end/random-color/master.svg?style=flat-square)](https://coveralls.io/github/mock-end/random-color) 9 | 10 | 11 | This module is deeply inspired by Martin Ankerl's [blog post](http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/). 12 | 13 | The generated colors are more visually pleasing together than a simple random color generator because they are computed by moving around the color wheel in increments of the golden ratio. By default, the generated colors can used as a background for legible black text, but the color generator can be customized for other uses. 14 | 15 | 16 | ## Install 17 | 18 | ``` 19 | $ npm install --save random-color 20 | ``` 21 | 22 | ## Usage 23 | 24 | ```js 25 | var randomColor = require('random-color'); 26 | 27 | // API 28 | // - randomColor([saturation, value]); 29 | ``` 30 | 31 | By default, generate a color with `saturation = 0.5` and `value = 0.95`. 32 | 33 | Returns [color](https://github.com/harthur/color) object so you can convert the color to any color-formats easily. 34 | 35 | ```js 36 | var color = randomColor(); 37 | 38 | // ref: https://github.com/harthur/color 39 | color.hexString(); // => '#d67118' 40 | color.rgbString(); // => 'rgb(110,52,164)' 41 | ``` 42 | ![saturation=0.5, value=0.95](img/1.png?raw=true "saturation=0.5, value=0.95") 43 | 44 | 45 | Optionally specify `saturation` and `value`: 46 | 47 | ```js 48 | var color = randomColor(0.99, 0.99); 49 | ``` 50 | ![saturation=0.99, value=0.99](img/2.png?raw=true "saturation=0.99, value=0.99") 51 | 52 | ```js 53 | var color = randomColor(0.3, 0.99); 54 | ``` 55 | ![saturation=0.3, value=0.99](img/1.png?raw=true "saturation=0.3, value=0.99") 56 | 57 | ## Contributing 58 | 59 | Pull requests and stars are highly welcome. 60 | 61 | For bugs and feature requests, please [create an issue](https://github.com/mock-end/random-color/issues/new). 62 | -------------------------------------------------------------------------------- /img/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mock-end/random-color/0ab2855e633b6a1a20b182288a5853e0fe2c746e/img/1.png -------------------------------------------------------------------------------- /img/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mock-end/random-color/0ab2855e633b6a1a20b182288a5853e0fe2c746e/img/2.png -------------------------------------------------------------------------------- /img/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mock-end/random-color/0ab2855e633b6a1a20b182288a5853e0fe2c746e/img/3.png -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var color = require('color'); 4 | 5 | var ratio = 0.618033988749895; 6 | var hue = Math.random(); 7 | 8 | module.exports = function (saturation, value) { 9 | 10 | hue += ratio; 11 | hue %= 1; 12 | 13 | if (typeof saturation !== 'number') { 14 | saturation = 0.5; 15 | } 16 | 17 | if (typeof value !== 'number') { 18 | value = 0.95; 19 | } 20 | 21 | return color({ 22 | h: hue * 360, 23 | s: saturation * 100, 24 | v: value * 100 25 | }); 26 | }; 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "random-color", 3 | "version": "1.0.1", 4 | "description": "Generate random colors based on the golden ratio, which are more visually pleasing.", 5 | "main": "index.js", 6 | "scripts": { 7 | "lint": "jshint index.js", 8 | "pretest": "npm run lint", 9 | "test": "mocha -R spec", 10 | "test:cov": "rm -rf ./test/coverage && istanbul cover _mocha --dir ./test/coverage -- -R spec", 11 | "test:rpt": "npm run test:cov && coveralls < ./test/coverage/lcov.info", 12 | "prepublish": "npm test" 13 | }, 14 | "keywords": [ 15 | "color", 16 | "rgb", 17 | "hex", 18 | "random", 19 | "randomly", 20 | "randomize", 21 | "chance", 22 | "test", 23 | "dice", 24 | "mock" 25 | ], 26 | "author": { 27 | "name": "bubkoo", 28 | "email": "bubkoo.wy@gmail.com" 29 | }, 30 | "license": "MIT", 31 | "repository": { 32 | "type": "git", 33 | "url": "git+https://github.com/mock-end/random-color.git" 34 | }, 35 | "bugs": { 36 | "url": "https://github.com/mock-end/random-color/issues" 37 | }, 38 | "homepage": "https://github.com/mock-end/random-color#readme", 39 | "devDependencies": { 40 | "jshint": "^2.9.1", 41 | "chai": "^3.5.0", 42 | "coveralls": "^2.11.9", 43 | "istanbul": "^0.4.2", 44 | "mocha": "^2.4.5" 45 | }, 46 | "dependencies": { 47 | "color": "^0.11.1" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('./spec/'); 4 | -------------------------------------------------------------------------------- /test/spec/index.js: -------------------------------------------------------------------------------- 1 | var expect = require('chai').expect; 2 | 3 | describe('random-file: ', function () { 4 | 5 | var randomColor = require('../../'); 6 | 7 | it('expect to be a color', function () { 8 | 9 | var hsv = randomColor().hsv(); 10 | 11 | expect(hsv).to.be.an('object'); 12 | expect(hsv.h).to.be.a('number'); 13 | expect(hsv.s).to.be.equal(50); 14 | expect(hsv.v).to.be.equal(95); 15 | 16 | expect(randomColor().rgbString()).to.be.a('string'); 17 | }); 18 | 19 | it('expect take optional saturation and value arguments', function () { 20 | 21 | var hsv = randomColor(0.8, 0.25).hsv(); 22 | 23 | expect(hsv).to.be.an('object'); 24 | expect(hsv.h).to.be.a('number'); 25 | expect(hsv.s).to.be.equal(80); 26 | expect(hsv.v).to.be.equal(25); 27 | 28 | }); 29 | 30 | it('expect generate different colors each time', function () { 31 | 32 | var hsv = randomColor().hsv(); 33 | var last = randomColor().hsv(); 34 | 35 | for (var i = 0; i < 10; i++) { 36 | 37 | expect(hsv).to.not.eql(last); 38 | 39 | last = randomColor().hsv(); 40 | } 41 | 42 | }); 43 | }); 44 | --------------------------------------------------------------------------------