This repository was archived by the owner on Apr 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathcolorEffects.js
More file actions
109 lines (101 loc) · 3.35 KB
/
Copy pathcolorEffects.js
File metadata and controls
109 lines (101 loc) · 3.35 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
/*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var vec4 = require('gl-matrix').vec4;
var mat4 = require('gl-matrix').mat4;
/**
* Helper functions for color transformation {@link Effects}.
*
* References:
*
* - [ColorMatrix Guide](http://docs.rainmeter.net/tips/colormatrix-guide)
* - [Matrix Operations for Image Processing](http://www.graficaobscura.com/matrix/index.html)
* - [WebGLImageFilter](https://github.com/phoboslab/WebGLImageFilter)
* - [glfx.js](https://github.com/evanw/glfx.js)
*
* @namespace colorEffects
*/
/**
* A vector and matrix corresponding to an identity transformation.
*
* @param {Object} result Object to store result
* @param {vec4} result.colorOffset Array with zeroes.
* @param {mat4} result.colorMatrix Identity matrix.
*
* @memberof colorEffects
*/
function identity(resultArg) {
var result = resultArg || {};
result.colorOffset = result.colorOffset || vec4.create();
result.colorMatrix = result.colorMatrix || mat4.create();
return result;
}
/**
* Apply color effects to a single pixel
*
* @param {vec4} pixel Values in range [0,1]
* @param {Object} effect
* @param {vec4} effect.colorOffset
* @param {mat4} effect.colorMatrix
* @param {vec4} result Object to store result
*
* @memberof colorEffects
*/
function applyToPixel(pixel, effect, result) {
vec4TransformMat4Transposed(result, pixel, effect.colorMatrix);
vec4.add(result, result, effect.colorOffset);
}
// Oddly, the colorTransform matrix needs to be transposed to be used with
// vec4.transformMat4. It is strange that transformMat4 dosn't work the same
// way as multiplying on the shader.
// TODO: investigate this further
function vec4TransformMat4Transposed(out, a, m) {
var x = a[0], y = a[1], z = a[2], w = a[3];
out[0] = m[0] * x + m[1] * y + m[2] * z + m[3] * w;
out[1] = m[4] * x + m[5] * y + m[6] * z + m[7] * w;
out[2] = m[8] * x + m[9] * y + m[10] * z + m[11] * w;
out[3] = m[12] * x + m[13] * y + m[14] * z + m[15] * w;
return out;
}
/**
* Apply color effects to an ImageData
*
* @param {ImageData} imageData This object will be mutated
* @param {Object} effect
* @param {vec4} effect.colorOffset
* @param {mat4} effect.colorMatrix
*
* @memberof colorEffects
*/
var tmpPixel = vec4.create();
function applyToImageData(imageData, effect) {
var width = imageData.width;
var height = imageData.height;
var data = imageData.data;
for(var i = 0; i < width * height; i++) {
vec4.set(tmpPixel, data[i*4+0]/255, data[i*4+1]/255, data[i*4+2]/255, data[i*4+3]/255);
applyToPixel(tmpPixel, effect, tmpPixel);
data[i*4+0] = tmpPixel[0]*255;
data[i*4+1] = tmpPixel[1]*255;
data[i*4+2] = tmpPixel[2]*255;
data[i*4+3] = tmpPixel[3]*255;
}
}
module.exports = {
identity: identity,
applyToPixel: applyToPixel,
applyToImageData: applyToImageData
};