Statistics

Problem Statement for "ColorMatch"

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 " " 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.
  • will contain only the letters A-Z and a-z.
  • will be between 1 and 20 characters, inclusive.
  • colorToMatch will be in the format " " 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"
  • , , and in colorToMatch and colorDatabase elements will be ints between 0 and 255, inclusive.
  • , , and 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

  1. {"BrightRed 255 0 0","BrightBlue 0 0 255","BrightGreen 0 255 0"}

    "200 200 200"

    Returns: "BrightGreen"

  2. {"Yellow 250 250 0","SkyBlue 30 80 250"}

    "140 165 125"

    Returns: "Yellow"

  3. {"SkyBlue 30 80 250","Yellow 250 250 0"}

    "140 165 125"

    Returns: "SkyBlue"

  4. {"SkyBlue 30 80 250","Yellow 250 250 30"}

    "140 165 125"

    Returns: "Yellow"

  5. {"lightgrey 200 200 200","red 240 0 0","white 255 255 255","lightblue 143 143 255","sulphur 255 200 50","green 0 255 0","orange 255 165 0","blue 0 0 255","green 34 139 34","brown 165 42 42","darkgrey 128 128 144","goldenrod 218 165 32","purple 160 32 240","firebrick 178 34 34","pink 255 192 203","deeppink 255 20 147","paleblue 96 128 255","white 255 255 255","YELLOW 255 200 0","redmagenta 255 0 128","beige 255 255 204","mauve 255 204 255","rosyflesh 255 204 204","orange 255 204 153","springreen 204 255 204","mediumgrey 170 170 170","brickRed 153 51 0","lime 153 255 0","paisley 153 102 102","burntsienna 153 153 102","aliceblue 240 248 255","crimson 227 38 54","antique 250 235 215","burntumber 138 51 36","cadmium 227 23 13","indianred 205 92 92","indigo 8 46 84","khaki 240 230 140","navy 0 0 128","olivedrab 107 142 35","teal 0 128 128","violet 143 94 153","Yellow 255 255 0","tan 210 180 140","royalblue 65 105 225","rose 227 54 56","white 0 0 0","Turquoise 64 224 208","YELLOW 255 255 0","salmon 250 128 114"}

    "255 167 100"

    Returns: "salmon"

  6. {"lightgrey 200 200 200","red 240 0 0","white 255 255 255","lightblue 143 143 255","sulphur 255 200 50","green 0 255 0","orange 255 165 0","blue 0 0 255","green 34 139 34","brown 165 42 42","darkgrey 128 128 144","goldenrod 218 165 32","purple 160 32 240","firebrick 178 34 34","pink 255 192 203","deeppink 255 20 147","paleblue 96 128 255","white 255 255 255","YELLOW 255 200 0","redmagenta 255 0 128","beige 255 255 204","mauve 255 204 255","rosyflesh 255 204 204","orange 255 204 153","springreen 204 255 204","mediumgrey 170 170 170","brickRed 153 51 0","lime 153 255 0","paisley 153 102 102","burntsienna 153 153 102","aliceblue 240 248 255","crimson 227 38 54","antique 250 235 215","burntumber 138 51 36","cadmium 227 23 13","indianred 205 92 92","indigo 8 46 84","khaki 240 230 140","navy 0 0 128","olivedrab 107 142 35","teal 0 128 128","violet 143 94 153","Yellow 255 255 0","tan 210 180 140","royalblue 65 105 225","rose 227 54 56","white 0 0 0","Turquoise 64 224 208","YELLOW 255 255 0","salmon 250 128 114"}

    "0 0 0"

    Returns: "white"

  7. {"lightgrey 200 200 200","red 240 0 0","white 255 255 255","lightblue 143 143 255","sulphur 255 200 50","green 0 255 0","orange 255 165 0","blue 0 0 255","green 34 139 34","brown 165 42 42","darkgrey 128 128 144","goldenrod 218 165 32","purple 160 32 240","firebrick 178 34 34","pink 255 192 203","deeppink 255 20 147","paleblue 96 128 255","white 255 255 255","YELLOW 255 200 0","redmagenta 255 0 128","beige 255 255 204","mauve 255 204 255","rosyflesh 255 204 204","orange 255 204 153","springreen 204 255 204","mediumgrey 170 170 170","brickRed 153 51 0","lime 153 255 0","paisley 153 102 102","burntsienna 153 153 102","aliceblue 240 248 255","crimson 227 38 54","antique 250 235 215","burntumber 138 51 36","cadmium 227 23 13","indianred 205 92 92","indigo 8 46 84","khaki 240 230 140","navy 0 0 128","olivedrab 107 142 35","teal 0 128 128","violet 143 94 153","Yellow 255 255 0","tan 210 180 140","royalblue 65 105 225","rose 227 54 56","white 0 0 0","Turquoise 64 224 208","YELLOW 255 255 0","salmon 250 128 114"}

    "64 64 64"

    Returns: "indigo"

  8. {"lightgrey 200 200 200","red 240 0 0","white 255 255 255","lightblue 143 143 255","sulphur 255 200 50","green 0 255 0","orange 255 165 0","blue 0 0 255","green 34 139 34","brown 165 42 42","darkgrey 128 128 144","goldenrod 218 165 32","purple 160 32 240","firebrick 178 34 34","pink 255 192 203","deeppink 255 20 147","paleblue 96 128 255","white 255 255 255","YELLOW 255 200 0","redmagenta 255 0 128","beige 255 255 204","mauve 255 204 255","rosyflesh 255 204 204","orange 255 204 153","springreen 204 255 204","mediumgrey 170 170 170","brickRed 153 51 0","lime 153 255 0","paisley 153 102 102","burntsienna 153 153 102","aliceblue 240 248 255","crimson 227 38 54","antique 250 235 215","burntumber 138 51 36","cadmium 227 23 13","indianred 205 92 92","indigo 8 46 84","khaki 240 230 140","navy 0 0 128","olivedrab 107 142 35","teal 0 128 128","violet 143 94 153","Yellow 255 255 0","tan 210 180 140","royalblue 65 105 225","rose 227 54 56","white 0 0 0","Turquoise 64 224 208","YELLOW 255 255 0","salmon 250 128 114"}

    "217 135 25"

    Returns: "goldenrod"

  9. {"lightgrey 200 200 200","red 240 0 0","white 255 255 255","lightblue 143 143 255","sulphur 255 200 50","green 0 255 0","orange 255 165 0","blue 0 0 255","green 34 139 34","brown 165 42 42","darkgrey 128 128 144","goldenrod 218 165 32","purple 160 32 240","firebrick 178 34 34","pink 255 192 203","deeppink 255 20 147","paleblue 96 128 255","white 255 255 255","YELLOW 255 200 0","redmagenta 255 0 128","beige 255 255 204","mauve 255 204 255","rosyflesh 255 204 204","orange 255 204 153","springreen 204 255 204","mediumgrey 170 170 170","brickRed 153 51 0","lime 153 255 0","paisley 153 102 102","burntsienna 153 153 102","aliceblue 240 248 255","crimson 227 38 54","antique 250 235 215","burntumber 138 51 36","cadmium 227 23 13","indianred 205 92 92","indigo 8 46 84","khaki 240 230 140","navy 0 0 128","olivedrab 107 142 35","teal 0 128 128","violet 143 94 153","Yellow 255 255 0","tan 210 180 140","royalblue 65 105 225","rose 227 54 56","white 0 0 0","Turquoise 64 224 208","YELLOW 255 255 0","salmon 250 128 114"}

    "0 64 128"

    Returns: "navy"

  10. {"lightgrey 200 200 200","red 240 0 0","white 255 255 255","lightblue 143 143 255","sulphur 255 200 50","green 0 255 0","orange 255 165 0","blue 0 0 255","green 34 139 34","brown 165 42 42","darkgrey 128 128 144","goldenrod 218 165 32","purple 160 32 240","firebrick 178 34 34","pink 255 192 203","deeppink 255 20 147","paleblue 96 128 255","white 255 255 255","YELLOW 255 200 0","redmagenta 255 0 128","beige 255 255 204","mauve 255 204 255","rosyflesh 255 204 204","orange 255 204 153","springreen 204 255 204","mediumgrey 170 170 170","brickRed 153 51 0","lime 153 255 0","paisley 153 102 102","burntsienna 153 153 102","aliceblue 240 248 255","crimson 227 38 54","antique 250 235 215","burntumber 138 51 36","cadmium 227 23 13","indianred 205 92 92","indigo 8 46 84","khaki 240 230 140","navy 0 0 128","olivedrab 107 142 35","teal 0 128 128","violet 143 94 153","Yellow 255 255 0","tan 210 180 140","royalblue 65 105 225","rose 227 54 56","white 0 0 0","Turquoise 64 224 208","YELLOW 255 255 0","salmon 250 128 114"}

    "0 65 128"

    Returns: "teal"

  11. {"Red 255 0 0","Reda 255 0 0","Redb 255 0 0","Redc 255 0 0","Redd 255 0 0","Rede 255 0 0","Redf 255 0 1"}

    "255 0 0"

    Returns: "Red"

  12. {"Red 255 0 0","Reda 255 0 0","Redb 255 0 0","Redc 255 0 0","Redd 255 0 0","Rede 255 0 0","Redf 255 1 1"}

    "255 255 0"

    Returns: "Redf"

  13. {"lightgrey 200 200 200","red 240 0 0","white 255 255 255","lightblue 143 143 255","sulphur 255 200 50","green 0 255 0","orange 255 165 0","blue 0 0 255","green 34 139 34","brown 165 42 42","darkgrey 128 128 144","goldenrod 218 165 32","purple 160 32 240","firebrick 178 34 34","pink 255 192 203","deeppink 255 20 147","paleblue 96 128 255","white 255 255 255","YELLOW 255 200 0","redmagenta 255 0 128","beige 255 255 204","mauve 255 204 255","rosyflesh 255 204 204","orange 255 204 153","springreen 204 255 204","mediumgrey 170 170 170","brickRed 153 51 0","lime 153 255 0","paisley 153 102 102","burntsienna 153 153 102","aliceblue 240 248 255","crimson 227 38 54","antique 250 235 215","burntumber 138 51 36","cadmium 227 23 13","indianred 205 92 92","indigo 8 46 84","khaki 240 230 140","navy 0 0 128","olivedrab 107 142 35","teal 0 128 128","violet 143 94 153","Yellow 255 255 0","tan 210 180 140","royalblue 65 105 225","rose 227 54 56","white 0 0 0","Turquoise 64 224 208","YELLOW 255 255 0","salmon 250 128 114"}

    "78 70 31"

    Returns: "burntumber"

  14. {"lightgrey 200 200 200","red 240 0 0","white 255 255 255","lightblue 143 143 255","sulphur 255 200 50","green 0 255 0","orange 255 165 0","blue 0 0 255","green 34 139 34","brown 165 42 42","darkgrey 128 128 144","goldenrod 218 165 32","purple 160 32 240","firebrick 178 34 34","pink 255 192 203","deeppink 255 20 147","paleblue 96 128 255","white 255 255 255","YELLOW 255 200 0","redmagenta 255 0 128","beige 255 255 204","mauve 255 204 255","rosyflesh 255 204 204","orange 255 204 153","springreen 204 255 204","mediumgrey 170 170 170","brickRed 153 51 0","lime 153 255 0","paisley 153 102 102","burntsienna 153 153 102","aliceblue 240 248 255","crimson 227 38 54","antique 250 235 215","burntumber 138 51 36","cadmium 227 23 13","indianred 205 92 92","indigo 8 46 84","khaki 240 230 140","navy 0 0 128","olivedrab 107 142 35","teal 0 128 128","violet 143 94 153","Yellow 255 255 0","tan 210 180 140","royalblue 65 105 225","rose 227 54 56","white 0 0 0","Turquoise 64 224 208","YELLOW 255 255 0","salmon 250 128 114"}

    "77 100 34"

    Returns: "olivedrab"

  15. {"BrightRed 255 0 0","BrightBlue 0 0 255","BrightGreen 0 255 0","Grey 127 127 127"}

    "200 200 200"

    Returns: "Grey"

  16. {"Red 255 255 255","Blue 255 255 255"}

    "255 255 255"

    Returns: "Red"

  17. {"Red 255 255 250","Red 250 250 250"}

    "50 50 50"

    Returns: "Red"

  18. {"veryveryveryveryvery 200 200 200"}

    "100 100 100"

    Returns: "veryveryveryveryvery"

  19. {"yellow 255 255 0","blue 200 200 0"}

    "255 255 0"

    Returns: "yellow"

  20. {"BrightRed 255 0 0","BrightGreen 0 255 0","BrightBlue 0 0 255","Grey 127 127 127"}

    "200 200 200"

    Returns: "Grey"

  21. {"BrightRed 255 0 0","BrightGreen 0 255 0","BrightBlue 0 0 255"}

    "200 200 200"

    Returns: "BrightGreen"

  22. {"rabidmongerel 20 20 20","tyrecius 250 250 0","twistedweasel 30 80 250","rlaustin 255 255 255"}

    "140 165 125"

    Returns: "tyrecius"

  23. {"yellow 255 255 0","yellow 200 200 0","blue 100 100 0"}

    "255 255 0"

    Returns: "yellow"

  24. {"white 255 255 255","blue 255 255 0","yellow 255 255 0","blue 255 255 0"}

    "200 200 0"

    Returns: "blue"

  25. {"BRIGHTRED 255 0 0","BRIGHTGREEN 25 0 0"}

    "0 0 0"

    Returns: "BRIGHTGREEN"

  26. {"rabmon 20 20 20","tyr 250 250 0","twister 30 80 250","zalah 140 165 125","alah 140 165 125","rlaustin 255 255 255"}

    "140 165 125"

    Returns: "zalah"

  27. {"one 50 100 0","two 50 0 100"}

    "0 0 0"

    Returns: "two"

  28. {"y 90 90 90","x 110 110 110"}

    "100 100 100"

    Returns: "y"

  29. {"red 255 0 0","green 0 255 0","blue 0 0 255"}

    "200 200 200"

    Returns: "green"

  30. {"ggggg 255 0 0","hhh 0 255 0","fff 0 0 0"}

    "200 200 200"

    Returns: "hhh"

  31. {"red 255 0 0","green 0 255 0","blue 0 0 255"}

    "200 200 200"

    Returns: "green"

  32. {"g 1 2 3"}

    "1 2 3"

    Returns: "g"

  33. {"g 200 180 200","b 200 200 220"}

    "200 200 200"

    Returns: "b"

  34. {"blue 255 255 0","yellow 255 255 0","green 255 255 0"}

    "240 240 0"

    Returns: "blue"

  35. {"RED 255 0 0","GREEN 0 255 0","BLUE 0 0 255"}

    "200 200 200"

    Returns: "GREEN"

  36. {"co 10 20 15","cb 90 90 90","cc 20 10 15"}

    "10 10 10"

    Returns: "cc"

  37. {"A 255 255 255","B 0 0 0"}

    "254 254 254"

    Returns: "A"

  38. {"red 255 0 0","blue 0 0 255","green 0 255 0","white 255 255 255","color 125 130 0","colorx 0 125 130"}

    "0 0 0"

    Returns: "red"

  39. {"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.

  40. {"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.

  41. {"yellow 255 255 0", "yellow 200 200 0", "blue 100 100 0"}

    "255 255 0"

    Returns: "yellow"

  42. {"white 255 255 255", "blue 255 255 0", "yellow 255 255 0", "blue 255 255 0"}

    "200 200 0"

    Returns: "blue"


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: