• Stars
    star
    710
  • Rank 63,751 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 10 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generate color based on the given string (using HSL color space and SHA256).

Color Hash

Generate color based on the given string.

Demo

https://zenozeng.github.io/color-hash/demo/

Usage

Deno

import ColorHash from "https://deno.land/x/[email protected]/mod.ts"

Node.js

npm install color-hash
import ColorHash from 'color-hash'

or

const ColorHash = require('color-hash').default;

<script type="module">

<script type="module">
import ColorHash from '../dist/esm.js';
</script>

Basic

var colorHash = new ColorHash();

// in HSL, Hue ∈ [0, 360), Saturation ∈ [0, 1], Lightness ∈ [0, 1]
colorHash.hsl('Hello World'); // [ 225, 0.65, 0.35 ]

// in RGB, R, G, B ∈ [0, 255]
colorHash.rgb('Hello World'); // [ 135, 150, 197 ]

// in HEX
colorHash.hex('Hello World'); // '#8796c5'

Custom Hash Function

var customHash = function(str) {
    var hash = 0;
    for(var i = 0; i < str.length; i++) {
        hash += str.charCodeAt(i);
    }
    return hash;
};
var colorHash = new ColorHash({hash: customHash});
colorHash.hsl('Hello World!');
colorHash.rgb('Hello World!');
colorHash.hex('Hello World!');

Custom Hue

var colorHash = new ColorHash({hue: 90});
var colorHash = new ColorHash({hue: {min: 90, max: 270}});
var colorHash = new ColorHash({hue: [ {min: 30, max: 90}, {min: 180, max: 210}, {min: 270, max: 285} ]});

Custom Lightness

var colorHash = new ColorHash({lightness: 0.5});
var colorHash = new ColorHash({lightness: [0.35, 0.5, 0.65]});

Custom Saturation

var colorHash = new ColorHash({saturation: 0.5});
var colorHash = new ColorHash({saturation: [0.35, 0.5, 0.65]});

License

MIT.

FAQ

How does it work?

It uses the hash function (default is BKDRHash) to calculate the hash of the given string.

Hue = hash % 359. (Note that 359 is a prime)
Saturation = SaturationArray[hash / 360 % SaturationArray.length]
Lightness = LightnessArray[hash / 360 / SaturationArray.length % LightnessArray.length]

By default,
SaturationArray = LightnessArray = [0.35, 0.5, 0.65]

Why not LAB?

Though LAB is more perceptually uniform, HSL is easier to control. Simply sets lightness and saturation and change hue uniformly can generate uniform colors.

Dev

Test

deno test

Coverage Report

https://zenozeng.github.io/color-hash/coverage/lcov-report/lib/index.html

Follow Semantic Versioning

http://semver.org/lang/zh-CN/

More Repositories

1

fonts.css

跨平台 CSS 中文字体解决方案
TypeScript
2,087
star
2

Free-Chinese-Fonts

免费中文字体
JavaScript
1,463
star
3

p5.js-svg

SVG runtime for p5.js.
JavaScript
680
star
4

yafolding.el

Yet another folding extension for Emacs
Emacs Lisp
150
star
5

font-detect.js

Detect font without flash, simply using javascript & @font-face
JavaScript
83
star
6

node-yaqrcode

Pure Javascript QR Code Generator for NodeJS
HTML
61
star
7

p5.js-pdf

A simple PDF export module for p5.js.
JavaScript
53
star
8

apisix-javascript-plugin-runner

Node.js / Deno / WASM Plugin Runner for APISIX
TypeScript
33
star
9

css-eldoc

eldoc-mode plugin for CSS
Emacs Lisp
28
star
10

gitlab-changelog

A php script to generate changelog via gitlab api
PHP
16
star
11

cocolour

Color schemes generator based on machine learning
JavaScript
12
star
12

colors-clustering

Colors clustering based on K-means algorithm & CIE76
JavaScript
11
star
13

act.js

Create Adobe Color Table (.act) files using Uint8Array, Blob API and FileReader API.
JavaScript
10
star
14

php-eldoc

eldoc-mode plugin for PHP source code
Emacs Lisp
10
star
15

parallel-algorithm-notes

This is my notes for our Parallel Algorithms course.
HTML
9
star
16

electronic-wechat-deb

Deb package for Zhongyi Tong's awesome Electronic WeChat.
Shell
7
star
17

automatic-control-theory

自动控制理论
Python
6
star
18

colors

colors
JavaScript
5
star
19

qr-auth

QR Code Based 2-Step Authentication
CoffeeScript
4
star
20

colors-clustering-visualization

Visualization demo for Colors Clustering using RGB & K-means algorithm
JavaScript
4
star
21

Data-Visualization-Examples

一些数据可视化的例子
CSS
4
star
22

wp-cors

Yet another wordpress cross domain plugin
PHP
3
star
23

gsoc2015

My Proposals and Logs for Google Summer of Code 2015
JavaScript
3
star
24

rice-sorting

Rice sorting based on machine vision
JavaScript
2
star
25

.xmonad

My XMonad & XMobar Config
Haskell
2
star
26

The-Nature-of-Code

The Nature of Code (in processing-js)
Processing
2
star
27

upgradeneeded

浏览器更新提示。
CSS
2
star
28

interval.js

A simple interval manager with lifetime / start / stop / pause / resume / restart
CSS
2
star
29

ASM-80C51

80C51 汇编
Assembly
2
star
30

gene-pool

Genetic algorithms in a given gene pool
JavaScript
2
star
31

zju-cli

This repo was moved to https://github.com/QSCTech/zjunet.
Shell
2
star
32

apisix-plugin-waf

ModSecurity compatible web application firewall plugin for Apache APISIX
2
star
33

npmm

Install npm packages using mirrrors with package-lock.json resolved to registry.npmjs.org
Shell
2
star
34

skeleton

Detect theme colors of a webpage using phantomjs
JavaScript
1
star
35

service

互联网服务
1
star
36

dotxbindkeys

Xbindkeys Configuration Framework
Scheme
1
star
37

xbindkeys-chromemacs

Emacs keybindings for Chrome / Chromium using xbindkeys
Scheme
1
star
38

cold-plasma-rice-seeds

冷等离子处理的水稻种子吸水特性
Python
1
star
39

ASABE-Robotics-Competition

ASABE Robotics Competition
JavaScript
1
star
40

postino

C
1
star
41

socks-scripts

Scripts using red socks & socks to bgswlan or any other router or server
1
star
42

bingang-industrial-park

滨港工业城官方网站前端
CoffeeScript
1
star
43

MathJax

Self hosted MathJax plus some handy configuration.
JavaScript
1
star
44

simple-loading-bar

A simple loading bar bind on dom
JavaScript
1
star
45

chrome-time

a simple clock for chrome
JavaScript
1
star
46

Machine-Learning

Machine Learning Exercises
Clojure
1
star
47

iframe-url-binding

Iframe based url binding
JavaScript
1
star
48

Cache.coffee

Cache.coffee is a library written for data caching.
CoffeeScript
1
star
49

common-knowledge

常识
1
star
50

time

A clock Web App, using IndexedDB.
JavaScript
1
star
51

mdt

Markdown toolkit (pandoc, XeLaTeX, mermaid and LaTeX beamer slide show)
Shell
1
star
52

try-kicad

IDL
1
star
53

fs-storage

localStorage like, fileSystem based storage
CoffeeScript
1
star