1// Copyright 2011 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package color
6
7// RGBToYCbCr converts an RGB triple to a Y'CbCr triple.
8func RGBToYCbCr(r, g, b uint8) (uint8, uint8, uint8) {
9	// The JFIF specification says:
10	//	Y' =  0.2990*R + 0.5870*G + 0.1140*B
11	//	Cb = -0.1687*R - 0.3313*G + 0.5000*B + 128
12	//	Cr =  0.5000*R - 0.4187*G - 0.0813*B + 128
13	// http://www.w3.org/Graphics/JPEG/jfif3.pdf says Y but means Y'.
14
15	r1 := int32(r)
16	g1 := int32(g)
17	b1 := int32(b)
18	yy := (19595*r1 + 38470*g1 + 7471*b1 + 1<<15) >> 16
19	cb := (-11056*r1 - 21712*g1 + 32768*b1 + 257<<15) >> 16
20	cr := (32768*r1 - 27440*g1 - 5328*b1 + 257<<15) >> 16
21	if yy < 0 {
22		yy = 0
23	} else if yy > 0xff {
24		yy = 0xff
25	}
26	if cb < 0 {
27		cb = 0
28	} else if cb > 0xff {
29		cb = 0xff
30	}
31	if cr < 0 {
32		cr = 0
33	} else if cr > 0xff {
34		cr = 0xff
35	}
36	return uint8(yy), uint8(cb), uint8(cr)
37}
38
39// YCbCrToRGB converts a Y'CbCr triple to an RGB triple.
40func YCbCrToRGB(y, cb, cr uint8) (uint8, uint8, uint8) {
41	// The JFIF specification says:
42	//	R = Y' + 1.40200*(Cr-128)
43	//	G = Y' - 0.34414*(Cb-128) - 0.71414*(Cr-128)
44	//	B = Y' + 1.77200*(Cb-128)
45	// http://www.w3.org/Graphics/JPEG/jfif3.pdf says Y but means Y'.
46
47	yy1 := int32(y) * 0x10100 // Convert 0x12 to 0x121200.
48	cb1 := int32(cb) - 128
49	cr1 := int32(cr) - 128
50	r := (yy1 + 91881*cr1) >> 16
51	g := (yy1 - 22554*cb1 - 46802*cr1) >> 16
52	b := (yy1 + 116130*cb1) >> 16
53	if r < 0 {
54		r = 0
55	} else if r > 0xff {
56		r = 0xff
57	}
58	if g < 0 {
59		g = 0
60	} else if g > 0xff {
61		g = 0xff
62	}
63	if b < 0 {
64		b = 0
65	} else if b > 0xff {
66		b = 0xff
67	}
68	return uint8(r), uint8(g), uint8(b)
69}
70
71// YCbCr represents a fully opaque 24-bit Y'CbCr color, having 8 bits each for
72// one luma and two chroma components.
73//
74// JPEG, VP8, the MPEG family and other codecs use this color model. Such
75// codecs often use the terms YUV and Y'CbCr interchangeably, but strictly
76// speaking, the term YUV applies only to analog video signals, and Y' (luma)
77// is Y (luminance) after applying gamma correction.
78//
79// Conversion between RGB and Y'CbCr is lossy and there are multiple, slightly
80// different formulae for converting between the two. This package follows
81// the JFIF specification at http://www.w3.org/Graphics/JPEG/jfif3.pdf.
82type YCbCr struct {
83	Y, Cb, Cr uint8
84}
85
86func (c YCbCr) RGBA() (uint32, uint32, uint32, uint32) {
87	// This code is a copy of the YCbCrToRGB function above, except that it
88	// returns values in the range [0, 0xffff] instead of [0, 0xff]. There is a
89	// subtle difference between doing this and having YCbCr satisfy the Color
90	// interface by first converting to an RGBA. The latter loses some
91	// information by going to and from 8 bits per channel.
92	//
93	// For example, this code:
94	//	const y, cb, cr = 0x7f, 0x7f, 0x7f
95	//	r, g, b := color.YCbCrToRGB(y, cb, cr)
96	//	r0, g0, b0, _ := color.YCbCr{y, cb, cr}.RGBA()
97	//	r1, g1, b1, _ := color.RGBA{r, g, b, 0xff}.RGBA()
98	//	fmt.Printf("0x%04x 0x%04x 0x%04x\n", r0, g0, b0)
99	//	fmt.Printf("0x%04x 0x%04x 0x%04x\n", r1, g1, b1)
100	// prints:
101	//	0x7e18 0x808d 0x7db9
102	//	0x7e7e 0x8080 0x7d7d
103
104	yy1 := int32(c.Y) * 0x10100 // Convert 0x12 to 0x121200.
105	cb1 := int32(c.Cb) - 128
106	cr1 := int32(c.Cr) - 128
107	r := (yy1 + 91881*cr1) >> 8
108	g := (yy1 - 22554*cb1 - 46802*cr1) >> 8
109	b := (yy1 + 116130*cb1) >> 8
110	if r < 0 {
111		r = 0
112	} else if r > 0xffff {
113		r = 0xffff
114	}
115	if g < 0 {
116		g = 0
117	} else if g > 0xffff {
118		g = 0xffff
119	}
120	if b < 0 {
121		b = 0
122	} else if b > 0xffff {
123		b = 0xffff
124	}
125	return uint32(r), uint32(g), uint32(b), 0xffff
126}
127
128// YCbCrModel is the Model for Y'CbCr colors.
129var YCbCrModel Model = ModelFunc(yCbCrModel)
130
131func yCbCrModel(c Color) Color {
132	if _, ok := c.(YCbCr); ok {
133		return c
134	}
135	r, g, b, _ := c.RGBA()
136	y, u, v := RGBToYCbCr(uint8(r>>8), uint8(g>>8), uint8(b>>8))
137	return YCbCr{y, u, v}
138}
139
140// NYCbCrA represents a non-alpha-premultiplied Y'CbCr-with-alpha color, having
141// 8 bits each for one luma, two chroma and one alpha component.
142type NYCbCrA struct {
143	YCbCr
144	A uint8
145}
146
147func (c NYCbCrA) RGBA() (uint32, uint32, uint32, uint32) {
148	// The first part of this method is the same as YCbCr.RGBA.
149	yy1 := int32(c.Y) * 0x10100 // Convert 0x12 to 0x121200.
150	cb1 := int32(c.Cb) - 128
151	cr1 := int32(c.Cr) - 128
152	r := (yy1 + 91881*cr1) >> 8
153	g := (yy1 - 22554*cb1 - 46802*cr1) >> 8
154	b := (yy1 + 116130*cb1) >> 8
155	if r < 0 {
156		r = 0
157	} else if r > 0xffff {
158		r = 0xffff
159	}
160	if g < 0 {
161		g = 0
162	} else if g > 0xffff {
163		g = 0xffff
164	}
165	if b < 0 {
166		b = 0
167	} else if b > 0xffff {
168		b = 0xffff
169	}
170
171	// The second part of this method applies the alpha.
172	a := uint32(c.A) * 0x101
173	return uint32(r) * a / 0xffff, uint32(g) * a / 0xffff, uint32(b) * a / 0xffff, a
174}
175
176// NYCbCrAModel is the Model for non-alpha-premultiplied Y'CbCr-with-alpha
177// colors.
178var NYCbCrAModel Model = ModelFunc(nYCbCrAModel)
179
180func nYCbCrAModel(c Color) Color {
181	switch c := c.(type) {
182	case NYCbCrA:
183		return c
184	case YCbCr:
185		return NYCbCrA{c, 0xff}
186	}
187	r, g, b, a := c.RGBA()
188
189	// Convert from alpha-premultiplied to non-alpha-premultiplied.
190	if a != 0 {
191		r = (r * 0xffff) / a
192		g = (g * 0xffff) / a
193		b = (b * 0xffff) / a
194	}
195
196	y, u, v := RGBToYCbCr(uint8(r>>8), uint8(g>>8), uint8(b>>8))
197	return NYCbCrA{YCbCr{Y: y, Cb: u, Cr: v}, uint8(a >> 8)}
198}
199
200// RGBToCMYK converts an RGB triple to a CMYK quadruple.
201func RGBToCMYK(r, g, b uint8) (uint8, uint8, uint8, uint8) {
202	rr := uint32(r)
203	gg := uint32(g)
204	bb := uint32(b)
205	w := rr
206	if w < gg {
207		w = gg
208	}
209	if w < bb {
210		w = bb
211	}
212	if w == 0 {
213		return 0, 0, 0, 0xff
214	}
215	c := (w - rr) * 0xff / w
216	m := (w - gg) * 0xff / w
217	y := (w - bb) * 0xff / w
218	return uint8(c), uint8(m), uint8(y), uint8(0xff - w)
219}
220
221// CMYKToRGB converts a CMYK quadruple to an RGB triple.
222func CMYKToRGB(c, m, y, k uint8) (uint8, uint8, uint8) {
223	w := uint32(0xffff - uint32(k)*0x101)
224	r := uint32(0xffff-uint32(c)*0x101) * w / 0xffff
225	g := uint32(0xffff-uint32(m)*0x101) * w / 0xffff
226	b := uint32(0xffff-uint32(y)*0x101) * w / 0xffff
227	return uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)
228}
229
230// CMYK represents a fully opaque CMYK color, having 8 bits for each of cyan,
231// magenta, yellow and black.
232//
233// It is not associated with any particular color profile.
234type CMYK struct {
235	C, M, Y, K uint8
236}
237
238func (c CMYK) RGBA() (uint32, uint32, uint32, uint32) {
239	// This code is a copy of the CMYKToRGB function above, except that it
240	// returns values in the range [0, 0xffff] instead of [0, 0xff].
241
242	w := uint32(0xffff - uint32(c.K)*0x101)
243	r := uint32(0xffff-uint32(c.C)*0x101) * w / 0xffff
244	g := uint32(0xffff-uint32(c.M)*0x101) * w / 0xffff
245	b := uint32(0xffff-uint32(c.Y)*0x101) * w / 0xffff
246	return uint32(r), uint32(g), uint32(b), 0xffff
247}
248
249// CMYKModel is the Model for CMYK colors.
250var CMYKModel Model = ModelFunc(cmykModel)
251
252func cmykModel(c Color) Color {
253	if _, ok := c.(CMYK); ok {
254		return c
255	}
256	r, g, b, _ := c.RGBA()
257	cc, mm, yy, kk := RGBToCMYK(uint8(r>>8), uint8(g>>8), uint8(b>>8))
258	return CMYK{cc, mm, yy, kk}
259}
260