Skip to content

Commit f5f9785

Browse files
committed
remove colorpicker dependency - use native functionality
1 parent 7575f0c commit f5f9785

5 files changed

Lines changed: 78 additions & 52 deletions

File tree

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@
6161
},
6262
"homepage": "https://github.com/CreativeCodingLab/TextAnnotationGraphs#readme",
6363
"dependencies": {
64-
"chroma-js": "^1.3.5",
65-
"colorPicker": "git+https://github.com/PitPik/colorPicker.git",
6664
"d3": "^4.12.2",
6765
"lodash": "^4.17.4",
6866
"svg.draggable.js": "^2.2.1",

src/css/app.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ label[for="file-input"] {
210210
pointer-events:none;
211211
opacity:0.5;
212212
}
213-
.cp-app {
214-
z-index:1001;
213+
#taxonomy input.colorpicker:focus {
214+
border-color: white;
215215
}
216216

217217
body > #tree-svg {

src/js/colorpicker.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class ColorPicker {
2+
constructor(className, options = {}) {
3+
this.initialColor = options.initialColor || '#000000';
4+
this.llColor = options.lightLabelColor || '#dddddd';
5+
this.dlColor = options.darkLabelColor || '#333333';
6+
this.changeCallback = options.changeCallback || null;
7+
8+
this.currentInput;
9+
this.picker = document.createElement('input');
10+
this.picker.type = 'color';
11+
this.picker.style = 'position:absolute; display: block; opacity: 0; z-index:-100;';
12+
this.picker.onchange = () => {
13+
if (this.currentInput) {
14+
this.setColor(this.currentInput, this.picker.value);
15+
if (this.changeCallback) {
16+
this.changeCallback(this.currentInput);
17+
}
18+
}
19+
}
20+
document.body.appendChild(this.picker);
21+
22+
const nodes = document.querySelectorAll(`input.${className}`);
23+
this.registerInputs(nodes);
24+
}
25+
26+
registerInputs(nodes) {
27+
nodes.forEach(input => {
28+
this.setColor(input, this.initialColor);
29+
30+
input.setAttribute('readonly', true);
31+
32+
input.onclick = () => {
33+
this.currentInput = input;
34+
this.picker.focus();
35+
this.picker.click();
36+
input.focus();
37+
this.picker.value = input.colorValue;
38+
}
39+
});
40+
}
41+
42+
// make label color light on a dark background and dark on a light one
43+
getLabelColor(bgColor) {
44+
return (this.RGBLum(bgColor) > 128) ? this.dlColor : this.llColor;
45+
}
46+
47+
// set the color of an input
48+
setColor(input, hex) {
49+
input.colorValue = hex;
50+
input.value = hex;
51+
input.style.backgroundColor = hex;
52+
input.style.color = this.getLabelColor(hex);
53+
}
54+
55+
// return the perceptive luminescence from a hex value for color contrast
56+
RGBLum(hex) {
57+
let c = parseInt(hex.slice(1), 16);
58+
59+
let r = (c >> 16) & 0xff;
60+
let g = (c >> 8) & 0xff;
61+
let b = (c >> 0) & 0xff;
62+
63+
return 0.299 * r + 0.587 * g + 0.114 * b;
64+
}
65+
}
66+
67+
module.exports = ColorPicker;

src/js/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ const Main = (function() {
139139
// window event listeners
140140
// resize function
141141
function resizeWindow() {
142-
body = document.body.getBoundingClientRect();
142+
let body = document.body.getBoundingClientRect();
143143
links.forEach(l => l.hide());
144144
svg.width(body.width);
145145
rm.width(body.width);

src/js/managers/taxonomy.js

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as jsColorPicker from 'colorPicker/colorPicker.js';
1+
import ColorPicker from '../colorpicker.js';
22
import Word from '../components/word.js';
33

44
module.exports = (function() {
@@ -169,46 +169,10 @@ module.exports = (function() {
169169
// bind events to data
170170
attachHandlers() {
171171
// initialize colorpicker
172-
let self = this;
173-
jsColorPicker('input.colorpicker', {
174-
customBG: '#000',
175-
init: function(elm, colors) {
176-
elm.style.backgroundColor = elm.value || '#000';
177-
elm.style.color = colors.rgbaMixCustom.luminance > 0.22 ? '#222' : '#ddd';
178-
},
179-
renderCallback: function(colors, mode) {
180-
/* ---- code taken from jsColor.js ---- */
181-
var options = this,
182-
input = options.input,
183-
patch = options.patch,
184-
RGB = colors.RND.rgb,
185-
HSL = colors.RND.hsl,
186-
AHEX = options.isIE8 ? (colors.alpha < 0.16 ? '0' : '') +
187-
(Math.round(colors.alpha * 100)).toString(16).toUpperCase() + colors.HEX : '',
188-
RGBInnerText = RGB.r + ', ' + RGB.g + ', ' + RGB.b,
189-
RGBAText = 'rgba(' + RGBInnerText + ', ' + colors.alpha + ')',
190-
isAlpha = colors.alpha !== 1 && !options.isIE8,
191-
colorMode = input.getAttribute('data-colorMode');
192-
193-
patch.style.cssText =
194-
'color:' + (colors.rgbaMixCustom.luminance > 0.22 ? '#222' : '#ddd') + ';' + // Black...???
195-
'background-color:' + RGBAText + ';' +
196-
'filter:' + (options.isIE8 ? 'progid:DXImageTransform.Microsoft.gradient(' + // IE<9
197-
'startColorstr=#' + AHEX + ',' + 'endColorstr=#' + AHEX + ')' : '');
198-
199-
input.value = (colorMode === 'HEX' && !isAlpha ? '#' + (options.isIE8 ? AHEX : colors.HEX) :
200-
colorMode === 'rgb' || (colorMode === 'HEX' && isAlpha) ?
201-
(!isAlpha ? 'rgb(' + RGBInnerText + ')' : RGBAText) :
202-
('hsl' + (isAlpha ? 'a(' : '(') + HSL.h + ', ' + HSL.s + '%, ' + HSL.l + '%' +
203-
(isAlpha ? ', ' + colors.alpha : '') + ')')
204-
);
205-
206-
if (options.displayCallback) {
207-
options.displayCallback(colors, mode, options);
208-
}
209-
210-
/* -- manually invoke callback -- */
211-
self.setColor(this.input.node);
172+
this.colorpicker = new ColorPicker('colorpicker', {
173+
initialColor: '#000000',
174+
changeCallback: (input) => {
175+
this.setColor(input.node);
212176
}
213177
});
214178

@@ -253,10 +217,7 @@ module.exports = (function() {
253217

254218
// manually set color
255219
if (color) {
256-
const c = new Colors({ color: color });
257-
const labelColor = c.colors.RGBLuminance > 0.22 ? '#222' : '#ddd';
258-
picker.value = color;
259-
picker.style.cssText = `color:${labelColor}; background-color:${color};`;
220+
this.colorpicker.setColor(picker, color);
260221
}
261222

262223
if (tagTypes[node.val]) {
@@ -286,7 +247,7 @@ module.exports = (function() {
286247
}
287248

288249
remove(object) {
289-
// TODO: fix the fuck out of this
250+
// FIXME
290251
return;
291252
let tag = object.val;
292253
let entity = object.entity;
@@ -302,7 +263,7 @@ module.exports = (function() {
302263
}
303264

304265
getColor(label, object) {
305-
//FIXME: fix me the fuck up
266+
//FIXME
306267
return;
307268
let keys = Object.keys(tagTypes);
308269
if (tagTypes[label]) {

0 commit comments

Comments
 (0)