-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorPicker.html
More file actions
152 lines (135 loc) · 5.81 KB
/
ColorPicker.html
File metadata and controls
152 lines (135 loc) · 5.81 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<style>
.spectrum-wrapper {
cursor: crosshair;
width: 240px;
position: relative;
height: 120px;
user-select: none;
user-drag: none;
margin: 0 auto;
/*outline: 0px solid black;*/
}
.spectrum-layer {
height: 100%;
width: 100%;
background: linear-gradient( to right, rgb(255 0 0), rgb(255 255 0), rgb(0 255 0), rgb(0 255 255), rgb(0 0 255), rgb(255 0 255), rgb(255 0 0) );
border-radius: 5px;
}
.saturation-white {
background: -webkit-linear-gradient(to top, rgba(255, 255, 255, 0), #fff);
background: linear-gradient(to top, rgba(255, 255, 255, 0), #fff);
position: absolute;
width: 100%;
height: 60px;
top: 0px;
right: 0px;
bottom: 60px;
left: 0px;
border-radius: 5px;
}
.saturation-black {
background: -webkit-linear-gradient(to bottom, rgba(0, 0, 0, 0), #000);
background: linear-gradient(to bottom, rgba(0, 0, 0, 0), #000);
position: absolute;
width: 100%;
height: 60px;
top: 60px;
right: 0px;
bottom: 0px;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="spectrum-wrapper">
<div class="spectrum-layer"></div>
<div class="saturation-white"></div>
<div class="saturation-black"></div>
</div>
<!--<div>Red: <span class="red"></span></div>-->
<div><!--Green: <span class="green"></span>--></div>
<div><!--Blue: <span class="blue"></span>--></div>
<div><!--<br />Hex: <span class="hex"></span>--></div>
<script>
// Initial function for MATLAB data exchange
function setup(htmlComponent) {
const getSpectrumWrapper = () => document.querySelector(".spectrum-wrapper");
const spectrumRanges = [
{ from: [255, 0, 0], to: [255, 255, 0] },
{ from: [255, 255, 0], to: [0, 255, 0] },
{ from: [0, 255, 0], to: [0, 255, 255] },
{ from: [0, 255, 255], to: [0, 0, 255] },
{ from: [0, 0, 255], to: [255, 0, 255] },
{ from: [255, 0, 255], to: [255, 0, 0] }
];
const findColorValue = (from, to, leftDistRatio) => {
return Math.round(from + (to - from) * leftDistRatio);
};
const findRgbFromMousePosition = (event) => {
const { left, width } = getSpectrumWrapper().getBoundingClientRect();
const leftDistance = Math.min(Math.max(event.clientX - left, 0), width - 1);
const rangeWidth = width / spectrumRanges.length;
const includedRange = Math.floor(leftDistance / rangeWidth);
const leftDistRatio = ((leftDistance % rangeWidth) / rangeWidth).toFixed(2);
const { from, to } = spectrumRanges[includedRange];
return {
r: findColorValue(from[0], to[0], leftDistRatio),
g: findColorValue(from[1], to[1], leftDistRatio),
b: findColorValue(from[2], to[2], leftDistRatio)
};
};
const darken = (color, ratio) => Math.round((1 - ratio) * color);
const whiten = (color, ratio) => Math.round(color + (255 - color) * ratio);
const adjustSaturation = ({ r, g, b }) => (ratio, adjustmentFn) => {
return {
r: adjustmentFn(r, ratio),
g: adjustmentFn(g, ratio),
b: adjustmentFn(b, ratio)
};
};
const saturate = (rgb, e) => {
const { top, height } = getSpectrumWrapper().getBoundingClientRect();
const topDistance = Math.min(Math.max(e.clientY - top, 0), height);
const topDistRatio = (topDistance / height).toFixed(2);
if (topDistRatio > 0.5) {
const darknessRatio = (topDistRatio - 0.5) / 0.5;
return adjustSaturation(rgb)(darknessRatio, darken);
}
if (topDistRatio < 0.5) {
const whitenessRatio = (0.5 - topDistRatio) / 0.5;
return adjustSaturation(rgb)(whitenessRatio, whiten);
}
return rgb;
};
const rgbToHex = (r, g, b) => {
const toHex = (rgb) => {
let hex = Number(rgb).toString(16);
if (hex.length < 2) {
hex = `0${hex}`;
}
return hex;
};
const red = toHex(r);
const green = toHex(g);
const blue = toHex(b);
return `#${red}${green}${blue}`;
};
getSpectrumWrapper().addEventListener("click", (e) => {
const rgb = findRgbFromMousePosition(e);
const { r, g, b } = saturate(rgb, e);
const hexValue = rgbToHex(r, g, b);
//document.querySelector(".red").innerText = r;
//document.querySelector(".green").innerText = g;
//document.querySelector(".blue").innerText = b;
//document.querySelector(".hex").innerText = hexValue;
htmlComponent.Data = [r, g, b]; //Return result to MATLAB in array
});
}
</script>
</body>
</html>