Overview
uniqolor is a fast and lightweight javascript library for generating unique and beautiful colors from any texts or numbers.
Why uniqolor?
- There is no need to store colors in the database anymore, just use uniqolor to generate colors at runtime and it will generate the same output every time, on any platform (Server, Browser or Mobile).
- You can generate a unique color from UUID, MongoDB ObjectId or anything that can be converted to a string or number
- You can generate a random color
- You can control the color saturation and lightness
- There is no need for an extra color library to change the color format or indicating whether the color brightness is light or dark
- It's lightweight (~1.4KB gzipped)
Quick start
Using npm
or yarn
$ npm install uniqolor
# or
$ yarn add uniqolor
ES6 Import:
import uniqolor from 'uniqolor';
CommonJS (like nodejs, webpack, and browserify):
const uniqolor = require('uniqolor');
AMD (like RequireJS):
define(['uniqolor'], function (uniqolor) {
})
Using <script>
Include uniqolor.js
or uniqolor.min.js
into your html file:
<script src="https://unpkg.com/uniqolor/dist/uniqolor.min.js" type="text/javascript"></script>
<script type="text/javascript">
var color = uniqolor('Hello world!');
</script>
Usage
uniqolor('Hello world!')
uniqolor('bf545d4c-5360-4158-a572-bd3e204185a9', { format: 'rgb' })
uniqolor(123, {
saturation: [35, 70],
lightness: 25,
})
uniqolor(123, {
saturation: [35, 70],
lightness: 25,
differencePoint: 50,
})
uniqolor.random()
uniqolor.random({ format: 'hsl' })
uniqolor.random({
saturation: 80,
lightness: [70, 80],
})
uniqolor.random({
excludeHue: [[0, 20], [325, 359]],
})
Examples
API
uniqolor(value, [options]) ⇒ Object
Generate unique color from value
Params:
value
(type: string|number
)options
(type: Object
, default: {}
)options.format
(type: string
, default: 'hex'
): The color format, it can be one of hex
, rgb
or hsl
options.saturation
(type: number|Array
, default: [50, 55]
): Determines the color saturation, it can be a number or a range between 0 and 100options.lightness
(type: number|Array
, default: [50, 60]
): Determines the color lightness, it can be a number or a range between 0 and 100options.differencePoint
(type: number
, defualt: 130
): Determines the color brightness difference point. We use it to obtain the isLight
value in the output, it can be a number between 0 and 255
Output:
color
(type: string
): The generated colorisLight
(type: boolean
): Determines whether the color
is a light color or a dark color (It's good for choosing a foreground color, like font color)
uniqolor.random([options]) ⇒ Object
Generate random color
Params:
options
(type: Object
, default: {}
)options.format
(type: string
, default: 'hex'
): The color format, it can be one of hex
, rgb
or hsl
options.saturation
(type: number|Array
, default: [50, 55]
): Determines the color saturation, it can be a number or a range between 0 and 100options.lightness
(type: number|Array
, default: [50, 60]
): Determines the color lightness, it can be a number or a range between 0 and 100options.differencePoint
(type: number
, default: 130
): Determines the color brightness difference point. We use it to obtain the isLight
value in the output, it can be a number between 0 and 255options.excludeHue
(type: Array
): Exclude certain hue ranges. For example to exclude red color range: [[0, 20], [325, 359]]
.
Contributing
Your ideas and contributions are welcome; check out our contributing guide
The unicorn shape in the logo made by Freepik is licensed by CC 3.0 BY
MIT © 2017 Rasool Dastoori