Tuesday, October 20, 2015

RGBColor

Problem:

TopCoder Problem Statement - RGBColor
Single Round Match 176 Round 1 - Division II, Level One

Overview:

Compute the compliment of the given RGB color.

Java Source:
     1 import java.util.*;
     2 import java.math.*;
     3 import static java.lang.Math.*;
     4
     5 public class RGBColor {
     6
     7     private static final int RGB_MAX = 255;
     8     private static final int GREY_TOLERANCE = 32;
     9
    10  public int[] getComplement(int[] rgb) {
    11
    12   int[] compliment = new int[rgb.length];
    13
    14         boolean isGrey = true;
    15
    16         for (int i = 0; i < rgb.length; i++)  {
    17             compliment[i] = RGB_MAX - rgb[i];
    18             if (Math.abs(rgb[i] - compliment[i]) > GREY_TOLERANCE) isGrey = false;
    19         }
    20
    21         if (isGrey)  {
    22             for (int i = 0; i < rgb.length; i++)  {
    23                 compliment[i] = (rgb[i] >= 128) ? rgb[i] - 128 : rgb[i] + 128;
    24             }
    25         }
    26
    27         return compliment;
    28
    29  }
    30 }
Notes:

We need to deal with two separate cases here: the color may meet the given definition of grey, or it may not. The trouble is, we won't know if it's considered grey or not until we've examined all three colors. We'll start by assuming that the color is grey, and then flip the isGrey flag to false if we find a red, green, or blue value whose compliment exceeds GREY_TOLERANCE.

We start by creating the output array named compliment. Then for each value in rgb, we calculate the corresponding compliment by subtracting the color in the current position of rgb from RGB_MAX. If the difference between a color and its compliment exceeds GREY_TOLERANCE, then we'll set isGrey to false.

After red, green, and blue have all been evaluated, if isGrey is still true then we need to handle it according to the instructions for a grey color. For each position in rgb, if the value is greater than or equal to 128, we subtract 128, otherwise add 128. This value will overwrite the value in compliment[] that was stored earlier.

No comments:

Post a Comment