-
Notifications
You must be signed in to change notification settings - Fork 25
/
yuv.js
48 lines (37 loc) · 899 Bytes
/
yuv.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
/**
* YUV https://en.wikipedia.org/?title=YUV
*
* @module color-space/yuv
*/
import rgb from './rgb.js';
var yuv = {
name: 'yuv',
min: [0,-0.5,-0.5],
max: [1, 0.5, 0.5],
channel: ['Y','U','V'],
alias: ['YUV', 'EBU'],
};
yuv.rgb = function(yuv) {
var y = yuv[0],
u = yuv[1],
v = yuv[2],
r, g, b;
r = (y * 1) + (u * 0) + (v * 1.13983);
g = (y * 1) + (u * -0.39465) + (v * -0.58060);
b = (y * 1) + (u * 2.02311) + (v * 0);
r = Math.min(Math.max(0, r), 1);
g = Math.min(Math.max(0, g), 1);
b = Math.min(Math.max(0, b), 1);
return [r * 255, g * 255, b * 255];
}
//extend rgb
rgb.yuv = function(rgb) {
var r = rgb[0] / 255,
g = rgb[1] / 255,
b = rgb[2] / 255;
var y = (r * 0.299) + (g * 0.587) + (b * 0.114);
var u = (r * -0.14713) + (g * -0.28886) + (b * 0.436);
var v = (r * 0.615) + (g * -0.51499) + (b * -0.10001);
return [y, u, v];
};
export default yuv;