原文链接及内容

运行界面

hue:色调
chroma:色度
lightness:亮度

栅格数据源允许被任意地操作其像素值。在此示例中,在第二个栅格数据源被渲染之前,输入切片数据源上的RGB数值将以像素为单位进行调整。栅格数据操作会接收RGB空间中的像素,将它们转换为HCL颜色空间,根据上面的控件调整值,然后将它们转换回RGB空间进行渲染。

main.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import ImageLayer from 'ol/layer/Image.js';
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import {Raster as RasterSource, Stamen} from 'ol/source.js';

/**
* 下面的颜色处理函数改编自https://github.com/d3/d3-color.
*/
const Xn = 0.95047;
const Yn = 1;
const Zn = 1.08883;
const t0 = 4 / 29;
const t1 = 6 / 29;
const t2 = 3 * t1 * t1;
const t3 = t1 * t1 * t1;
const twoPi = 2 * Math.PI;

/**
* Convert an RGB pixel into an HCL pixel.
* 将一个RGB像素转换为一个HCL像素.
* @param {Array<number>} pixel A pixel in RGB space.一个RGB颜色空间的像素
* @return {Array<number>} A pixel in HCL space.一个HCL颜色空间的像素
*/
function rgb2hcl(pixel) {
const red = rgb2xyz(pixel[0]);
const green = rgb2xyz(pixel[1]);
const blue = rgb2xyz(pixel[2]);

const x = xyz2lab(
(0.4124564 * red + 0.3575761 * green + 0.1804375 * blue) / Xn
);
const y = xyz2lab(
(0.2126729 * red + 0.7151522 * green + 0.072175 * blue) / Yn
);
const z = xyz2lab(
(0.0193339 * red + 0.119192 * green + 0.9503041 * blue) / Zn
);

const l = 116 * y - 16;
const a = 500 * (x - y);
const b = 200 * (y - z);

const c = Math.sqrt(a * a + b * b);
let h = Math.atan2(b, a);
if (h < 0) {
h += twoPi;
}

pixel[0] = h;
pixel[1] = c;
pixel[2] = l;

return pixel;
}

/**
* Convert an HCL pixel into an RGB pixel.
* 将一个HCL颜色空间的像素转换为一个RGB颜色空间的像素.
* @param {Array<number>} pixel A pixel in HCL space.一个HCL颜色空间的像素
* @return {Array<number>} A pixel in RGB space.一个RGB颜色空间的像素
*/
function hcl2rgb(pixel) {
const h = pixel[0];
const c = pixel[1];
const l = pixel[2];

const a = Math.cos(h) * c;
const b = Math.sin(h) * c;

let y = (l + 16) / 116;
let x = isNaN(a) ? y : y + a / 500;
let z = isNaN(b) ? y : y - b / 200;

y = Yn * lab2xyz(y);
x = Xn * lab2xyz(x);
z = Zn * lab2xyz(z);

pixel[0] = xyz2rgb(3.2404542 * x - 1.5371385 * y - 0.4985314 * z);
pixel[1] = xyz2rgb(-0.969266 * x + 1.8760108 * y + 0.041556 * z);
pixel[2] = xyz2rgb(0.0556434 * x - 0.2040259 * y + 1.0572252 * z);

return pixel;
}

function xyz2lab(t) {
return t > t3 ? Math.pow(t, 1 / 3) : t / t2 + t0;
}

function lab2xyz(t) {
return t > t1 ? t * t * t : t2 * (t - t0);
}

function rgb2xyz(x) {
return (x /= 255) <= 0.04045 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4);
}

function xyz2rgb(x) {
return (
255 * (x <= 0.0031308 ? 12.92 * x : 1.055 * Math.pow(x, 1 / 2.4) - 0.055)
);
}

const raster = new RasterSource({
sources: [
new Stamen({
layer: 'watercolor',
}),
],
operation: function (pixels, data) {
const hcl = rgb2hcl(pixels[0]);

let h = hcl[0] + (Math.PI * data.hue) / 180;
if (h < 0) {
h += twoPi;
} else if (h > twoPi) {
h -= twoPi;
}
hcl[0] = h;

hcl[1] *= data.chroma / 100;
hcl[2] *= data.lightness / 100;

return hcl2rgb(hcl);
},
lib: {
rgb2hcl: rgb2hcl,
hcl2rgb: hcl2rgb,
rgb2xyz: rgb2xyz,
lab2xyz: lab2xyz,
xyz2lab: xyz2lab,
xyz2rgb: xyz2rgb,
Xn: Xn,
Yn: Yn,
Zn: Zn,
t0: t0,
t1: t1,
t2: t2,
t3: t3,
twoPi: twoPi,
},
});

const controls = {};

raster.on('beforeoperations', function (event) {
const data = event.data;
for (const id in controls) {
data[id] = Number(controls[id].value);
}
});

const map = new Map({
layers: [
new ImageLayer({
source: raster,
}),
],
target: 'map',
view: new View({
center: [0, 2500000],
zoom: 2,
maxZoom: 18,
}),
});

const controlIds = ['hue', 'chroma', 'lightness'];
controlIds.forEach(function (id) {
const control = document.getElementById(id);
const output = document.getElementById(id + 'Out');
control.addEventListener('input', function () {
output.innerText = control.value;
raster.changed();
});
output.innerText = control.value;
controls[id] = control;
});

界面布局文件index.html代码如下:

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Color Manipulation</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
table.controls td {
padding: 2px 5px;
}
table.controls td:nth-child(3) {
text-align: right;
min-width: 4.5em;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<table class="controls">
<tr>
<td><label for="hue">hue</label></td>
<td><input id="hue" type="range" min="-180" max="180" value="0"/></td>
<td><span id="hueOut"></span> °&nbsp;</td>
</tr>
<tr>
<td><label for="chroma">chroma</label></td>
<td><input id="chroma" type="range" min="0" max="100" value="100"/></td>
<td><span id="chromaOut"></span> %</td>
</tr>
<tr>
<td><label for="lightness">lightness</label></td>
<td><input id="lightness" type="range" min="0" max="100" value="100"/></td>
<td><span id="lightnessOut"></span> %</td>
</tr>
</table>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://cdn.jsdelivr.net/npm/elm-pep@1.0.6/dist/elm-pep.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>