-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathypbpr.js
66 lines (53 loc) · 1.24 KB
/
ypbpr.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
/**
* https://en.wikipedia.org/?title=YPbPr
*
* YPbPr is analog form of YCbCr
* hence limits are [0..1]
*
* Default conversion is ITU-R BT.709
*
* @module color-space/ypbpr
*/
import rgb from './rgb.js';
var ypbpr = {
name: 'ypbpr',
min: [0,-0.5,-0.5],
max: [1, 0.5, 0.5],
channel: ['Y','Pb','Pr'],
alias: ['YPbPr', 'Y/PB/PR', 'YPRPB', 'PRPBY', 'PBPRY', 'Y/Pb/Pr', 'YPrPb', 'PrPbY', 'PbPrY', 'Y/R-Y/B-Y', 'Y(R-Y)(B-Y)', 'R-Y', 'B-Y']
};
/**
* YPbPr to RGB
*
* @param {Array} ypbpr RGB values
*
* @return {Array} YPbPr values
*/
ypbpr.rgb = function(ypbpr, kb, kr) {
var y = ypbpr[0], pb = ypbpr[1], pr = ypbpr[2];
//default conversion is ITU-R BT.709
kb = kb || 0.0722;
kr = kr || 0.2126;
var r = y + 2 * pr * (1 - kr);
var b = y + 2 * pb * (1 - kb);
var g = (y - kr * r - kb * b) / (1 - kr - kb);
return [r*255,g*255,b*255];
};
/**
* RGB to YPbPr
*
* @param {Array} ypbpr YPbPr values
*
* @return {Array} RGB values
*/
rgb.ypbpr = function(rgb, kb, kr) {
var r = rgb[0]/255, g = rgb[1]/255, b = rgb[2]/255;
//ITU-R BT.709
kb = kb || 0.0722;
kr = kr || 0.2126;
var y = kr*r + (1 - kr - kb)*g + kb*b;
var pb = 0.5 * (b - y) / (1 - kb);
var pr = 0.5 * (r - y) / (1 - kr);
return [y, pb, pr];
};
export default ypbpr;