How to Parse HTML/CSS Colors in Java
The challenge
In this challenge, you parse RGB colors represented by strings. The formats are primarily used in HTML and CSS. Your task is to implement a function that takes a color as a string and returns the parsed color as a map (see Examples).
Inputs:
The input string represents one of the following:
- 6-digit hexadecimal – “#RRGGBB”
e.g. “#012345”, “#789abc”, “#FFA077”
Each pair of digits represents a value of the channel in hexadecimal: 00 to FF - 3-digit hexadecimal – “#RGB”
e.g. “#012”, “#aaa”, “#F5A”
Each digit represents a value 0 to F which translates to 2-digit hexadecimal: 0->00, 1->11, 2->22, and so on. - Preset color name
e.g. “red”, “BLUE”, “LimeGreen”
You have to use the predefined mapPRESET_COLORS
(JavaScript, Python, Ruby),presetColors
(Java, C#, Haskell), orpreset-colors
(Clojure). The keys are the names of preset colors in lower-case and the values are the corresponding colors in 6-digit hexadecimal (same as 1. “#RRGGBB”).
Examples:
The solution in Java code
Option 1:
|
|
Option 2:
|
|
Option 3:
|
|
Test cases to validate our solution
|
|