title: 純javascript--簡單的colorPicker取色器
date: 2018-04-26 00:04:45
tags:
功能需求
- 取色板可點擊遭贸,長按移動火惊,獲取顏色
- 取色條拖動改變色相
- 可修改十六進制值隅很,RGB,HSL桐猬,HSV值改變所有顯示數(shù)據(jù)
- 用戶輸入的數(shù)據(jù)進行校驗(十六進制自動補全冷离;不正確的十六進制自動顯示為黑色;rgb值的極限范圍澡谭,S,V,L 小數(shù)點輸入的實時校驗并糾正)
主思路
一愿题、三部分組成:取色板,取色條蛙奖,輸入框
二潘酗、hsv rgb hsl 三者關(guān)系
HSVL:
H: 色相,0°-360°雁仲,每隔60°有不同的主色相
S: 飽和度崎脉,0-100%,越低越灰暗
V: 明度伯顶,L: 亮度 0-100%
s隨著水平軸的向右從0-1囚灼,v隨著垂直軸的向上從0-1骆膝,所以可以用鼠標(biāo)在取色板中的位置(width, height)
與整個面板的width和height的比例求出(s, v)
,h值可根據(jù)在取色條上的高度灶体,求出hsv值阅签,再來個轉(zhuǎn)換公式rgb,hsl也呼之欲出~
常規(guī)的取色器
三蝎抽、取色板和取色條的渲染 [ canvas ]
取色條渲染簡單~政钟,用canvas的api
addColorStop
取色板看來得用個透明灰板加個主顏色漸變來渲染,不過要注意只要h發(fā)生變化樟结,主顏色就會發(fā)生變化养交,所以得重新渲染主顏色層
四、數(shù)據(jù)變化
觸發(fā)更新的兩種方法:
i. 通過拖動取色板或者取色條瓢宦,通過get方法獲取hsv等
ii. 通過改變輸入板上的值碎连,通過value方法獲取hsv等
更新任務(wù):
* 更新數(shù)據(jù)渲染頁面:
* 獲取當(dāng)前hsv rgb hsl值,
* 實時顯示input中的值
* 實時顯示當(dāng)前顏色
* 更新渲染picker-panel 取色板
* 更新handle的background-color
* 更新handle的位置
取色板驮履,取色條渲染
///先完成取色板鱼辙,取色條的渲染
<div class="main-panel">
<div class="main">
<canvas width="460" height="400" id="selectColor">You browser is not support canvas</canvas>
<span id="main-handle" class="handle"></span>
</div>
<div class="bar">
<canvas width="12" height="400" id="dragColor"></canvas>
<span id="drag-handle" class="handle"></span>
</div>
</div>
////取色板渲染
PickerPanel.prototype.render = function (color) {
///一層是透明灰層
var lightLinear = this.ctxPicker.createLinearGradient(0, 0, 0, this.height);
lightLinear.addColorStop(0, 'rgba(0, 0, 0, 0)');
lightLinear.addColorStop(1, 'rgba(0, 0, 0, 1)');
///一層是顏色層,通過傳進來的color決定色相
var colorLinear = this.ctxPicker.createLinearGradient(0, 0, this.width, 0);
colorLinear.addColorStop(0, 'rgba(255, 255, 255, 1)');
colorLinear.addColorStop(1, color);
this.ctxPicker.fillStyle = colorLinear;
this.ctxPicker.fillRect(0, 0, this.width, this.height);
this.ctxPicker.fillStyle = lightLinear;
this.ctxPicker.fillRect(0, 0, this.width, this.height);
}
////取色器渲染
BarPanel.prototype.render = function () {
var dragLinear = this.ctxPicker.createLinearGradient(0, 0, 0, this.height);
////整個取色器分成6等分玫镐,每等分的增量 1 / 6
dragLinear.addColorStop(0, '#ff0000');
dragLinear.addColorStop(0.167, '#ffff00');
dragLinear.addColorStop(0.334, '#00ff00');
dragLinear.addColorStop(0.501, '#00ffff');
dragLinear.addColorStop(0.668, '#0000ff');
dragLinear.addColorStop(0.835, '#ff00ff');
dragLinear.addColorStop(1, '#ff0000');
this.ctxPicker.fillStyle = dragLinear;
this.ctxPicker.fillRect(0, 0, this.width, this.height);
}
確定顏色坐標(biāo)(HSV)
///通過取色條的位置與總高度比獲取h
///通過鼠標(biāo)x與總width之比獲取s倒戏,鼠標(biāo)y與總height之比獲取v
function getHSV() {
var h = barIns.y * 360 / barIns.height,
s = pickerIns.x * 1 / pickerIns.width,
v = 1 - ( pickerIns.y * 1 / pickerIns.height );
return {
h: parseInt(h) === 360? 0 : parseInt(h),
s: s.toFixed(2),
v: v.toFixed(2)
}
}
HSV 轉(zhuǎn)換成 RGB HSL
var colorConvert = {
hsv2rbg: function (h, s, v) {
var r, g, b;
var i = Math.floor(h / 60),
f = h / 60 - i,
p = v * (1 - s),
q = v * (1 - f * s),
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0:
r = v, g = t, b = p;
break;
case 1:
r = q, g = v, b = p;
break;
case 2:
r = p, g = v, b = t;
break;
case 3:
r = p, g = q, b = v;
break;
case 4:
r = t, g = p, b = v;
break;
case 5:
r = v, g = p, b = q;
break;
}
return {
r: parseInt(r * 255),
g: parseInt(g * 255),
b: parseInt(b * 255)
}
},
rgb2hsv: function (r, g, b) {
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b),
min = Math.min(r, g, b);
var h, s, v;
v = max;
var d = max - min;
s = max == 0 ? 0 : d / max;
if (max == min) {
h = 0;
} else {
switch (max) {
case r:
h = 60 * ((g - b) / d + (g < b ? 6 : 0));
break;
case g:
h = 60 * ((b - r) / d + 2);
break;
case b:
h = 60 * ((r - g) / d + 4);
break;
}
}
return {
h: parseInt(h),
s: s.toFixed(2),
v: v.toFixed(2)
}
},
hsl2rgb: function (h, s, l) {
h /= 360;
var r, g, b;
if (s == 0) {
r = g = b = l; // achromatic
} else {
function hue2rgb(p, q, t) {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1 / 6) return p + (q - p) * 6 * t;
if (t < 1 / 2) return q;
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
return p;
}
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1 / 3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1 / 3);
}
return {
r: parseInt(r * 255),
g: parseInt(g * 255),
b: parseInt(b * 255)
}
},
rgb2hsl: function (r, g, b) {
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b),
min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;
if (max == min) {
h = s = 0; // achromatic
} else {
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r:
h = 60 * ((g - b) / d + (g < b ? 6 : 0));
break;
case g:
h = 60 * ((b - r) / d + 2);
break;
case b:
h = 60 * ((r - g) / d + 4);
break;
}
}
return {
h: parseInt(h),
s: s.toFixed(2),
l: l.toFixed(2)
}
},
hsv2hsl: function (h, s, v) {
var rgb = this.hsv2rbg(h, s, v);
return this.rgb2hsl(rgb.r, rgb.g, rgb.b);
},
hsl2hsv: function (h, s, l) {
var rgb = this.hsl2rgb(h, s, l);
return this.rgb2hsv(rgb.r, rgb.g, rgb.b);
}
}
var DecimalConvert = {
d2h: function (value) {
var hArr = [], a = ['A', 'B', 'C', 'D', 'E', 'F'];
if (value === 0) {
hArr.unshift(0);
}
while (value !== 0) {
var mol = value % 16;
mol = mol - 9 > 0? a[mol - 10] : mol;
hArr.unshift(mol);
value = parseInt(value / 16);
}
////////////////////////////
//////////應(yīng)用于顏色十進制轉(zhuǎn)換成十六進制 //
/////////////////////////s///
if (hArr.length < 2) {
hArr.unshift(0);
}
return hArr.join('');
},
h2d: function (value) {
var hArr = value.split(/(\d\d)|(\w\w)/).filter(h => !!h).map(h => parseInt(h, 16));
return {
r: hArr[0],
g: hArr[1],
b: hArr[2]
}
}
}
輸入數(shù)據(jù)的校驗,用正則表達(dá)式處理
InputPanel.prototype.dataFormat = function(e, id, value) {
switch (id) {
case 'R':
case 'G':
case 'B':
value = Number(value);
if (value > 255) value = 255;
e.target.value = isNaN(value)? 0 : value;
pickByRGB();
break;
case 'H':
value = Number(value);
if (value >= 360) value = 360;
e.target.value = isNaN(value)? 0 : value;
pickByHSL();
break;
case 'S':
case 'L':
value = value.replace(/^[^01]/, '').replace(/([01])\d?(\.)*(\d)?(\d)?.?/, '$1$2$3$4');
e.target.value = (value * 100) > 100? '1.00' : value;
if (value.charAt(value.length - 1) !== '.') {
pickByHSL();
}
break;
default: break;
}
}
剩下的就主要是一些dom的事件綁定和數(shù)據(jù)的更新~
gif質(zhì)量有點差哈哈恐似,各位看官將就看吧~
參考:https://zh.wikipedia.org/wiki/HSL%E5%92%8CHSV%E8%89%B2%E5%BD%A9%E7%A9%BA%E9%97%B4