Problem Statement
Colors are commonly represented by the intensity of the red, green, and blue elements used to represent them on a TV screen or monitor. The three numbers together are referred to as the 'RGB values' of the color.
Given a String[] database of colors with their RGB values, and the RGB values of a sample, return the color closest to the sample. The closest color will be the one with the smallest difference from the sample. Here difference is defined as the sum of the differences between each corresponding RGB component. If multiple colors have the same total difference, return the one among them with the minimum green difference. (The eye distinguishes green better than red or blue). If multiple colors have the same minimum total difference and the same minimum green difference, you should return the color among them with the lowest index in the database (see examples).
The total difference between two colors is defined as | red1 - red2 | + | green1 - green2 | + | blue1 - blue2 |. The green difference between two colors is defined as | green1 - green2 |.
Create a class ColorMatch that contains the method bestMatch, which takes a String[] as a color database and a String representation of the RGB values of a sample color to match, and returns the name of the closest color in the database.
Definition
- Class:
- ColorMatch
- Method:
- bestMatch
- Parameters:
- String[], String
- Returns:
- String
- Method signature:
- String bestMatch(String[] colorDatabase, String colorToMatch)
- (be sure your method is public)
Notes
- Color names may appear more than once in the database, even with different RGB values; use the tie breaker rules outlined above if there is a conflict. (see example 2)
- The same RGB values may appear more than once in the database, even with different names; use the tie breaker rules outlined above if there is a conflict. (see example 3)
- The return value should be the color name from the database with no leading or trailing spaces or RGB values, and the same upper/lower casing as the matching element in the database.
Constraints
- Each element of colorDatabase will be in the format "<color_name> <red> <green> <blue>" exactly, with no extra spaces (quotes and brackets are for illustration only).
- there will be exactly one space between each component (red, green, and blue) of each element of colorDatabase, with no leading or trailing spaces.
- <color_name> will contain only the letters A-Z and a-z.
- <color_name> will be between 1 and 20 characters, inclusive.
- colorToMatch will be in the format "<red> <green> <blue>" exactly.
- there will be exactly one space between each component (red, green, and blue) of colorToMatch, with no leading or trailing spaces.
- colorToMatch will be between 5 and 11 characters, inclusive. "0 0 0" - "255 255 255"
- <red>, <green>, and <blue> in colorToMatch and colorDatabase elements will be ints between 0 and 255, inclusive.
- <red>, <green>, and <blue> will have no leading 0s. "0" and "123" are valid, but "0123" and "00" are not.
- colorDatabase will have between 1 and 50 elements, inclusive.
Examples
{"BrightRed 255 0 0", "BrightGreen 0 255 0", "BrightBlue 0 0 255", "Grey 127 127 127"}"200 200 200"
Returns: "Grey"
BrightRed total difference: |255-200|+|0-200|+|0-200| = 455 BrightGreen total difference: |0-200|+|255-200|+|0-200| = 455 BrightBlue total difference: |0-200|+|0-200|+|255-200| = 455 Grey total difference: |127-200|+|127-200|+|127-200| = 219 The method returns "Grey" because 219 is the smallest total difference.{"BrightRed 255 0 0", "BrightGreen 0 255 0", "BrightBlue 0 0 255"}"200 200 200"
Returns: "BrightGreen"
BrightRed total difference: |255-200|+|0-200|+|0-200| = 455 BrightGreen total difference: |0-200|+|255-200|+|0-200| = 455 BrightBlue total difference: |0-200|+|0-200|+|255-200| = 455 BrightRed green difference: |0-200| = 200 BrightGreen green difference: |255-200| = 55 BrightBlue green difference: |0-200| = 200 There is a tie between all of the total differences, so the minimum green difference would be used.{"yellow 255 255 0", "yellow 200 200 0", "blue 100 100 0"}"255 255 0"
Returns: "yellow"
{"white 255 255 255", "blue 255 255 0", "yellow 255 255 0", "blue 255 255 0"}"200 200 0"
Returns: "blue"