2.0.2 • Published 1 year ago

color-hash v2.0.2

Weekly downloads
62,164
License
MIT
Repository
github
Last release
1 year ago

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/color_hash@v2.0.0/mod.ts"

Node.js

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

or

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

<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/

balena-cli@balena/jellyfish-ui-components@dxos/editor@dxos/messenger-app@dxos/messenger-pad@dxos/messenger-components@statisticsnorway/dapla-lineage-viewergrowlerendition@everything-registry/sub-chunk-1355yuki-labs-test-delegate-uiyuki-governance-uimc-benchmarkonespot-dashboard-prototypeovermind-devtools-clientmtxtrpcv2patchcorepg-componentspeer-star-peer-colorreact-cismapreact-completor@infinitebrahmanuniverse/nolb-color-nfs-lcdp-pc-designerprotvista-interpro-adapterbabel-plugin-tracingcolor-hash-clireact-oneteamcallipyge-coreresin-clites-int-mattersealioussimple-log-webpack-plugindive-dsastate-machine-editorssb-patchworksp-frontendserviceblenddatocms-plugin-content-calendarhapiverse@nyaruka/temba-components@plasma-platform/plasma-quark@plasma-platform/quarkhubot-aws-insightdyu-commentsduodashi-web@spartez-software/test-utils@react-bratus/app@eidellev/react-tag-input@jasonetco/action-record@mhlabs/cfn-diagram@metapair/ui@midiate/app-chord-recognizer@midiate/app-shortcuts@midiate/midiate@borf/server@perses-dev/panels-pluginhello-sudo@haztivity/hz-soupletter@gun-vue/composables@reltio/mdm-sdk@d3vd/gatsby-theme-code-notes@cotype/ui@dr.cash/componentswatchman-corevzcode@darkh0le/gatsby-theme-code-notes@datagrok/usage-analysisvue-media-annotatorvue-md-uixray-first-react@dormakaba/digital-reactnative-visual@dormakaba/taibika-visualzack-search@glific/temba-components@graphcentral/graph@grantcodes/themer@fibery/ui-kit@lamdahq/file-formats@restorecommerce/gql-bot@woofjs/server@yrpri/apiferment@zalastax/nolb-color-friends@xenie/xenie-ui@wirelineio/editor@ubnt/unifi-led-uikouxiangtang@verto/uiant-aigatsby-theme-insutanto-code-notesgatsby-theme-code-notesluminol
2.0.2

1 year ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.1

3 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago