-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcolor.js
95 lines (81 loc) · 2.26 KB
/
color.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var EventEmitter = require('events').EventEmitter
var ColorPicker = require('simple-color-picker')
var inherits = require('inherits')
var css = require('dom-css')
var tinycolor = require('tinycolor2')
module.exports = Color
inherits(Color, EventEmitter)
function Color (root, opts, theme, uuid) {
if (!(this instanceof Color)) return new Color(root, opts, theme, uuid)
opts = opts || {}
opts.format = opts.format || 'rgb'
opts.initial = opts.initial || '#123456'
var self = this
var container = require('./container')(root, opts.label)
require('./label')(container, opts.label, theme)
var icon = container.appendChild(document.createElement('span'))
icon.className = 'control-panel-color-' + uuid
var value = require('./value')(container, '', theme, '46%')
icon.onmouseover = function () {
picker.$el.style.display = ''
}
var initial = opts.initial
switch (opts.format) {
case 'rgb':
initial = tinycolor(initial).toHexString()
break
case 'hex':
initial = tinycolor(initial).toHexString()
break
case 'array':
initial = tinycolor.fromRatio({r: initial[0], g: initial[1], b: initial[2]}).toHexString()
break
default:
break
}
var picker = new ColorPicker({
el: icon,
color: initial,
background: theme.background1,
width: 125,
height: 100
})
css(picker.$el, {
marginTop: '20px',
display: 'none',
position: 'absolute'
})
css(icon, {
position: 'relative',
display: 'inline-block',
width: '12.5%',
height: '20px',
backgroundColor: picker.getHexString()
})
icon.onmouseout = function (e) {
picker.$el.style.display = 'none'
}
setTimeout(function () {
self.emit('initialized', initial)
})
picker.onChange(function (hex) {
value.innerHTML = format(hex)
css(icon, {backgroundColor: hex})
self.emit('input', format(hex))
})
function format (hex) {
switch (opts.format) {
case 'rgb':
return tinycolor(hex).toRgbString()
case 'hex':
return tinycolor(hex).toHexString()
case 'array':
var rgb = tinycolor(hex).toRgb()
return [rgb.r / 255, rgb.g / 255, rgb.b / 255].map(function (x) {
return x.toFixed(2)
})
default:
return hex
}
}
}