From 3f081efd76b492f3ccd1d53022103f1b8cdfe6ba Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 10 Oct 2015 17:04:33 +0200 Subject: Implement color coding functions --- src/main/java/Color.java | 82 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 17 deletions(-) (limited to 'src') 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 @@ */ public final class Color { + public static final int BYTE_MASK = 0b11111111; + public static final float DECIMAL_NORMALIZER = 255.0f; + + public static final int RED_SHIFT = 16; + public static final int GREEN_SHIFT = 8; + public static final int BLUE_SHIFT = 0; + + /** + * Returns shifted component from given packed color. + * + * @param rgb 32-bits RGB color + * @param shift binary shift + * @return a float between 0.0 and 1.0 + * @see #getRGBValue + */ + private static float getNormalizedRGBComponent(int rgb, int shift) { + return ((float) (rgb >> shift & BYTE_MASK)) / DECIMAL_NORMALIZER; + } + + /** + * Returns RGB binary value from given component and color shift. + * + * @param component a float between 0.0 and 1.0 + * @param shift binary shift + * @return 32-bits RGB color + * @see #getNormalizedRGBComponent + */ + private static int getRGBValue(float component, int shift) { + if (component < 0.0f) component = 0.0f; + if (component > 1.0f) component = 1.0f; + + return ((int) (component * DECIMAL_NORMALIZER) & BYTE_MASK) << shift; + } + /** * Returns red component from given packed color. * @@ -14,8 +48,7 @@ public final class Color { * @see #getRGB(float, float, float) */ public static float getRed(int rgb) { - // TODO getRed - return 0.0f; + return Color.getNormalizedRGBComponent(rgb, RED_SHIFT); } /** @@ -28,8 +61,7 @@ public final class Color { * @see #getRGB(float, float, float) */ public static float getGreen(int rgb) { - // TODO getGreen - return 0.0f; + return Color.getNormalizedRGBComponent(rgb, GREEN_SHIFT); } /** @@ -42,8 +74,7 @@ public final class Color { * @see #getRGB(float, float, float) */ public static float getBlue(int rgb) { - // TODO getBlue - return 0.0f; + return Color.getNormalizedRGBComponent(rgb, BLUE_SHIFT); } /** @@ -57,8 +88,7 @@ public final class Color { * @see #getRGB(float) */ public static float getGray(int rgb) { - // TODO getGray - return 0.0f; + return (Color.getRed(rgb) + Color.getGreen(rgb) + Color.getBlue(rgb)) / 3.0f; } /** @@ -73,20 +103,22 @@ public final class Color { * @see #getBlue */ public static int getRGB(float red, float green, float blue) { - // TODO getRGB - return 0; + int rgbValue = 0b0; + rgbValue |= Color.getRGBValue(red, RED_SHIFT); + rgbValue |= Color.getRGBValue(green, GREEN_SHIFT); + rgbValue |= Color.getRGBValue(blue, BLUE_SHIFT); + return rgbValue; } /** * Returns packed RGB components from given grayscale value. * - * @param gray a float between 0.0 and 1.0 + * @param gray a float between 0.0 and 1.0 * @return 32-bits RGB color * @see #getGray */ public static int getRGB(float gray) { - // TODO getRGB - return 0; + return Color.getRGB(gray, gray, gray); } /** @@ -98,8 +130,16 @@ public final class Color { * @see #getGray */ public static float[][] toGray(int[][] image) { - // TODO toGray - return null; + int width = image[0].length; + int height = image.length; + + float[][] floatImage = new float[height][width]; + + for (int row = 0; row < height; ++row) + for (int col = 0; col < width; ++col) + floatImage[row][col] = Color.getGray(image[row][col]); + + return floatImage; } /** @@ -111,8 +151,16 @@ public final class Color { * @see #getRGB(float) */ public static int[][] toRGB(float[][] gray) { - // TODO toRGB - return null; + int width = gray[0].length; + int height = gray.length; + + int[][] rgbImage = new int[height][width]; + + for (int row = 0; row < height; ++row) + for (int col = 0; col < width; ++col) + rgbImage[row][col] = Color.getRGB(gray[row][col]); + + return rgbImage; } } -- cgit v1.2.3