color-hash

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

  • 所有者: zenozeng/color-hash
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

Color Hash

Generate color based on the given string.

Demo

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

Usage

Browser

bower install color-hash

A UMD version of ColorHash is located in dist/.

Note that Array.prototype.map is used in color-hash,
a polyfill must be provided if you want to use it in IE8.

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]});

Node.js

npm install color-hash --save
var ColorHash = require('color-hash');

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

npm install
npm test

Coverage Report

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

Build browser js

npm install
npm run build

Follow Semantic Versioning

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

主要指標

概覽
名稱與所有者zenozeng/color-hash
主編程語言TypeScript
編程語言Makefile (語言數: 2)
平台
許可證MIT License
所有者活动
創建於2015-02-06 12:53:01
推送於2023-08-31 03:10:37
最后一次提交
發布數9
最新版本名稱v2.0.1 (發布於 )
第一版名稱v0.1.0 (發布於 )
用户参与
星數721
關注者數8
派生數40
提交數97
已啟用問題?
問題數33
打開的問題數5
拉請求數9
打開的拉請求數0
關閉的拉請求數2
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?