Improved IntelliJ startup script.
[imagesqueeze.git] / src / main / java / eu / svjatoslav / imagesqueeze / codec / Color.java
1 /*
2  * Imagesqueeze - Image codec. Copyright ©2012-2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 3 of the GNU Lesser General Public License
6  * or later as published by the Free Software Foundation.
7  */
8
9 package eu.svjatoslav.imagesqueeze.codec;
10
11 /**
12  * Helper class to convert between RGB and YUV
13  */
14 class Color {
15
16     int r;
17     int g;
18     int b;
19
20     int y;
21     int u;
22     int v;
23
24     public void RGB2YUV() {
25
26         y = (int) ((r * 0.299000) + (g * 0.587000) + (b * 0.114000));
27         u = (int) ((r * -0.168736) + (g * -0.331264) + (b * 0.500000) + 128);
28         v = (int) ((r * 0.500000) + (g * -0.418688) + (b * -0.081312) + 128);
29
30         if (y < 0)
31             y = 0;
32         if (u < 0)
33             u = 0;
34         if (v < 0)
35             v = 0;
36
37         if (y > 255)
38             y = 255;
39         if (u > 255)
40             u = 255;
41         if (v > 255)
42             v = 255;
43
44     }
45
46     public void YUV2RGB() {
47
48         b = (int) (y + (1.4075 * (v - 128)));
49         g = (int) (y - (0.3455 * (u - 128)) - (0.7169 * (v - 128)));
50         r = (int) (y + (1.7790 * (u - 128)));
51
52         if (r < 0)
53             r = 0;
54         if (g < 0)
55             g = 0;
56         if (b < 0)
57             b = 0;
58
59         if (r > 255)
60             r = 255;
61         if (g > 255)
62             g = 255;
63         if (b > 255)
64             b = 255;
65
66     }
67
68 }