diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/Color.java | 82 |
1 files changed, 65 insertions, 17 deletions
diff --git a/src/main/java/Color.java b/src/main/java/Color.java index 9e05039..49a6618 100644 --- a/src/main/java/Color.java +++ b/src/main/java/Color.java | |||
@@ -4,6 +4,40 @@ | |||
4 | */ | 4 | */ |
5 | public final class Color { | 5 | public final class Color { |
6 | 6 | ||
7 | public static final int BYTE_MASK = 0b11111111; | ||
8 | public static final float DECIMAL_NORMALIZER = 255.0f; | ||
9 | |||
10 | public static final int RED_SHIFT = 16; | ||
11 | public static final int GREEN_SHIFT = 8; | ||
12 | public static final int BLUE_SHIFT = 0; | ||
13 | |||
14 | /** | ||
15 | * Returns shifted component from given packed color. | ||
16 | * | ||
17 | * @param rgb 32-bits RGB color | ||
18 | * @param shift binary shift | ||
19 | * @return a float between 0.0 and 1.0 | ||
20 | * @see #getRGBValue | ||
21 | */ | ||
22 | private static float getNormalizedRGBComponent(int rgb, int shift) { | ||
23 | return ((float) (rgb >> shift & BYTE_MASK)) / DECIMAL_NORMALIZER; | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * Returns RGB binary value from given component and color shift. | ||
28 | * | ||
29 | * @param component a float between 0.0 and 1.0 | ||
30 | * @param shift binary shift | ||
31 | * @return 32-bits RGB color | ||
32 | * @see #getNormalizedRGBComponent | ||
33 | */ | ||
34 | private static int getRGBValue(float component, int shift) { | ||
35 | if (component < 0.0f) component = 0.0f; | ||
36 | if (component > 1.0f) component = 1.0f; | ||
37 | |||
38 | return ((int) (component * DECIMAL_NORMALIZER) & BYTE_MASK) << shift; | ||
39 | } | ||
40 | |||
7 | /** | 41 | /** |
8 | * Returns red component from given packed color. | 42 | * Returns red component from given packed color. |
9 | * | 43 | * |
@@ -14,8 +48,7 @@ public final class Color { | |||
14 | * @see #getRGB(float, float, float) | 48 | * @see #getRGB(float, float, float) |
15 | */ | 49 | */ |
16 | public static float getRed(int rgb) { | 50 | public static float getRed(int rgb) { |
17 | // TODO getRed | 51 | return Color.getNormalizedRGBComponent(rgb, RED_SHIFT); |
18 | return 0.0f; | ||
19 | } | 52 | } |
20 | 53 | ||
21 | /** | 54 | /** |
@@ -28,8 +61,7 @@ public final class Color { | |||
28 | * @see #getRGB(float, float, float) | 61 | * @see #getRGB(float, float, float) |
29 | */ | 62 | */ |
30 | public static float getGreen(int rgb) { | 63 | public static float getGreen(int rgb) { |
31 | // TODO getGreen | 64 | return Color.getNormalizedRGBComponent(rgb, GREEN_SHIFT); |
32 | return 0.0f; | ||
33 | } | 65 | } |
34 | 66 | ||
35 | /** | 67 | /** |
@@ -42,8 +74,7 @@ public final class Color { | |||
42 | * @see #getRGB(float, float, float) | 74 | * @see #getRGB(float, float, float) |
43 | */ | 75 | */ |
44 | public static float getBlue(int rgb) { | 76 | public static float getBlue(int rgb) { |
45 | // TODO getBlue | 77 | return Color.getNormalizedRGBComponent(rgb, BLUE_SHIFT); |
46 | return 0.0f; | ||
47 | } | 78 | } |
48 | 79 | ||
49 | /** | 80 | /** |
@@ -57,8 +88,7 @@ public final class Color { | |||
57 | * @see #getRGB(float) | 88 | * @see #getRGB(float) |
58 | */ | 89 | */ |
59 | public static float getGray(int rgb) { | 90 | public static float getGray(int rgb) { |
60 | // TODO getGray | 91 | return (Color.getRed(rgb) + Color.getGreen(rgb) + Color.getBlue(rgb)) / 3.0f; |
61 | return 0.0f; | ||
62 | } | 92 | } |
63 | 93 | ||
64 | /** | 94 | /** |
@@ -73,20 +103,22 @@ public final class Color { | |||
73 | * @see #getBlue | 103 | * @see #getBlue |
74 | */ | 104 | */ |
75 | public static int getRGB(float red, float green, float blue) { | 105 | public static int getRGB(float red, float green, float blue) { |
76 | // TODO getRGB | 106 | int rgbValue = 0b0; |
77 | return 0; | 107 | rgbValue |= Color.getRGBValue(red, RED_SHIFT); |
108 | rgbValue |= Color.getRGBValue(green, GREEN_SHIFT); | ||
109 | rgbValue |= Color.getRGBValue(blue, BLUE_SHIFT); | ||
110 | return rgbValue; | ||
78 | } | 111 | } |
79 | 112 | ||
80 | /** | 113 | /** |
81 | * Returns packed RGB components from given grayscale value. | 114 | * Returns packed RGB components from given grayscale value. |
82 | * | 115 | * |
83 | * @param gray a float between 0.0 and 1.0 | 116 | * @param gray a float between 0.0 and 1.0 |
84 | * @return 32-bits RGB color | 117 | * @return 32-bits RGB color |
85 | * @see #getGray | 118 | * @see #getGray |
86 | */ | 119 | */ |
87 | public static int getRGB(float gray) { | 120 | public static int getRGB(float gray) { |
88 | // TODO getRGB | 121 | return Color.getRGB(gray, gray, gray); |
89 | return 0; | ||
90 | } | 122 | } |
91 | 123 | ||
92 | /** | 124 | /** |
@@ -98,8 +130,16 @@ public final class Color { | |||
98 | * @see #getGray | 130 | * @see #getGray |
99 | */ | 131 | */ |
100 | public static float[][] toGray(int[][] image) { | 132 | public static float[][] toGray(int[][] image) { |
101 | // TODO toGray | 133 | int width = image[0].length; |
102 | return null; | 134 | int height = image.length; |
135 | |||
136 | float[][] floatImage = new float[height][width]; | ||
137 | |||
138 | for (int row = 0; row < height; ++row) | ||
139 | for (int col = 0; col < width; ++col) | ||
140 | floatImage[row][col] = Color.getGray(image[row][col]); | ||
141 | |||
142 | return floatImage; | ||
103 | } | 143 | } |
104 | 144 | ||
105 | /** | 145 | /** |
@@ -111,8 +151,16 @@ public final class Color { | |||
111 | * @see #getRGB(float) | 151 | * @see #getRGB(float) |
112 | */ | 152 | */ |
113 | public static int[][] toRGB(float[][] gray) { | 153 | public static int[][] toRGB(float[][] gray) { |
114 | // TODO toRGB | 154 | int width = gray[0].length; |
115 | return null; | 155 | int height = gray.length; |
156 | |||
157 | int[][] rgbImage = new int[height][width]; | ||
158 | |||
159 | for (int row = 0; row < height; ++row) | ||
160 | for (int col = 0; col < width; ++col) | ||
161 | rgbImage[row][col] = Color.getRGB(gray[row][col]); | ||
162 | |||
163 | return rgbImage; | ||
116 | } | 164 | } |
117 | 165 | ||
118 | } | 166 | } |