-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgridchrome.js
96 lines (89 loc) · 2.98 KB
/
gridchrome.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
"use strict";
/*
* 2011/01/29- (c) [email protected]
*/
document.addEventListener("DOMContentLoaded", function(event) {
main();
});
function main() {
// console.debug("main");
const srcCanvas = document.getElementById("srcCanvas");
const dstCanvas = document.getElementById("dstCanvas");
const srcImage = new Image();
const params = {};
srcImage.onload = function() {
drawSrcImageAndGridChrome(srcImage, srcCanvas, dstCanvas, params);
}
srcImage.src = "./img/RGBCube.png"
dropFunction(document, function(dataURL) {
srcImage.src = dataURL;
}, "DataURL");
bindFunction({"maxWidthHeightRange":"maxWidthHeightText",
"periodRange":"periodText",
"widthRange":"widthText",
"cheatRange":"cheatText"},
function() {
drawSrcImageAndGridChrome(srcImage, srcCanvas, dstCanvas,
params);
}, params);
}
function drawSrcImageAndGridChrome(srcImage, srcCanvas, dstCancas, params) {
const maxWidthHeight = params.maxWidthHeightRange;
drawSrcImage(srcImage, srcCanvas, maxWidthHeight);
drawGridChrome(srcCanvas, dstCanvas, params);
}
function grayColor(rgba) {
const [r, g, b, a] = rgba;
// CIE XYZ (BT.709 coeff & linear)
const lr = 0.2126 * Math.pow(r / 255, 2.2);
const lg = 0.7152 * Math.pow(g / 255, 2.2);
const lb = 0.0722 * Math.pow(b / 255, 2.2);
const v = Math.pow(lr + lg + lb, 1 / 2.2) * 255;
return [v, v, v, a];
}
function isGrid(x, y, period, width) {
const xy1 = (x + y) % period;
if ((xy1 < width) || (xy1 > (period-width))) {
return true;
}
const xy2 = Math.abs(x - y) % period;
if ((xy2 < width) || (xy2 > (period-width))) {
return true;
}
return false;
}
function gridChrome(x, y, rgba, params) {
const [r, g, b, a] = rgba;
const period = params.periodRange;
const width = params.widthRange;
const cheat = params.cheatRange;
if (isGrid(x, y, period, width/2)) {
return rgba;
}
if (isGrid(x, y, period, width)) {
return grayColor(rgba).map(function(v, i) {
return (rgba[i]*(cheat/10) + v) / ((cheat/10)+ 1);
});
}
return grayColor(rgba).map(function(v, i) {
return (rgba[i]*(cheat/100) + v) / ((cheat/100)+ 1);
});
}
function drawGridChrome(srcCanvas, dstCanvas, params) {
// console.debug("drawGridChrome");
const srcCtx = srcCanvas.getContext("2d");
const dstCtx = dstCanvas.getContext("2d");
const width = srcCanvas.width, height = srcCanvas.height;
dstCanvas.width = width;
dstCanvas.height = height;
//
const srcImageData = srcCtx.getImageData(0, 0, width, height);
const dstImageData = dstCtx.createImageData(width, height);
for (let y = 0 ; y < height; y++) {
for (let x = 0 ; x < width; x++) {
const rgba = getRGBA(srcImageData, x, y);
setRGBA(dstImageData, x, y, gridChrome(x, y, rgba, params));
}
}
dstCtx.putImageData(dstImageData, 0, 0);
}