ayin-color
Version:
由ayin开发的js版和less版的颜色表,用于js中的取色,如echarts,svg等。Please read readme.md for English description!
86 lines • 3.02 kB
JavaScript
import chroma from "chroma-js";
import json from "./ayin-color.json"
/*
code by ayin86@163.com
chsname:["红","红橙","橙","橙黄","黄","黄草","草绿","草绿-绿","绿","绿-水绿","水绿","水绿-青","青","青蓝","蓝","蓝靛","靛","靛紫","紫","紫粉","粉","粉-洋红","洋红","洋红-红","灰"],
engname:["red","red-orange","orange","orange-yellow","yellow","yellow-chartreuse","chartreuse","chartreuse-green","green","green-aquamarine","aquamarine","aquamarine-cyan","cyan","cyan-blue","blue","blue-indigo","indigo","indigo-purple","purple","purple-pink","pink","pink-violetred","violetred","violetred-red","gray"],
colors:["re","ro","or","oy","ye","yc","ch","cg","gr","ga","aq","ac","cy","cb","bl","bi","in","ip","pu","pp","pi","pv","vi","vr","gy"],
*/
//chroma.hsl(hue, saturation, lightness)
//chroma.hsv(hue, saturation, value)
//chroma.lab(Lightness, a, b)
//chroma.lch(Lightness, chroma, hue)
//chroma.hcl(hue, chroma, lightness)
//chroma.cmyk(cy, magenta, ye, black)
//chroma.gl(re, gr, bl, [alpha])
let illegal="Illegal color !"
const coFuncs={
fade:(color,num)=>{
let a=json[color]?json[color]:color
if(chroma.valid(a)){
return chroma(a).alpha(num).hex();
}else{
console.warn(illegal,color,num)
}
},
//hue色相,saturation饱和度,lightness明暗
hslh:(color,num)=>{//hue色相调整
let a=json[color]?json[color]:color
if(chroma.valid(a)){
return chroma(a).set('hsl.h', num).hex();
}else{
console.warn(illegal,color,num)
}
},
hsls:(color,num)=>{//saturation饱和度
let a=json[color]?json[color]:color
if(chroma.valid(a)){
return chroma(a).set('hsl.s', num).hex();
}else{
console.warn(illegal,color,num)
}
},
hsll:(color,num)=>{//lightness明暗
let a=json[color]?json[color]:color
if(chroma.valid(a)){
return chroma(a).set('hsl.l', num).hex();
}else{
console.warn(illegal,color,num)
}
},
darken:(color,num)=>{
let a=json[color]?json[color]:color
if(chroma.valid(a)){
return chroma(a).darken(num).hex();
}else{
console.warn(illegal,color,num)
}
},
lighten:(color,num)=>{
let a=json[color]?json[color]:color
if(chroma.valid(a)){
return chroma(a).brighten(num).hex();
}else{
console.warn(illegal,color,num)
}
},
scale:(arry,num=6,mode="lch")=>{//mode=lch,hsl,lab,lrgb
const newArray=[];
arry.forEach((item,index)=>{
if(chroma.valid(item)){
newArray.push(item)
}else if(chroma.valid(json[item])){
newArray.push(json[item])
}else{
console.warn("The color set contains illegal color ",arry,index)
}
})
if(newArray.length>0){
return chroma.scale(newArray).mode(mode).colors(num)
}else{
console.error("The color set initialization failed ",arry)
}
},
}
const $c = Object.assign(coFuncs, json);
export {chroma,$c}