6 kyu

Parse HTML/CSS Colors

154 of 2,771junthecoder

Description:

In this kata you parse RGB colors represented by strings. The formats are primarily used in HTML and CSS. Your task is to implement a function which takes a color as a string and returns the parsed color as a map (see Examples).

Input:

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 map PRESET_COLORS (JavaScript, Python, Ruby), presetColors (Java, C#, Haskell), PresetColors (Go) or preset-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:

parse_html_color('#80FFA0')   # => { r: 128, g: 255, b: 160 }
parse_html_color('#3B7')      # => { r: 51,  g: 187, b: 119 }
parse_html_color('LimeGreen') # => { r: 50,  g: 205, b: 50  }
parse_html_color('#80FFA0')   # => {'r': 128, 'g': 255, 'b': 160}
parse_html_color('#3B7')      # => {'r': 51,  'g': 187, 'b': 119}
parse_html_color('LimeGreen') # => {'r': 50,  'g': 205, 'b': 50 }
parseHTMLColor('#80FFA0');    // => { r: 128, g: 255, b: 160 }
parseHTMLColor('#3B7');       // => { r: 51,  g: 187, b: 119 }
parseHTMLColor('LimeGreen');  // => { r: 50,  g: 205, b: 50  }
(parse-html-color "#80FFA0")   ; => {:r 128 :g 255 :b 160}
(parse-html-color "#3B7")      ; => {:r 51  :g 187 :b 119}
(parse-html-color "LimeGreen") ; => {:r 50  :g 205 :b 50 }
-- You can get a value from the map like this:
presetColors ! "blue"
--
parseHtmlColor "#80FFA0"   === fromList [('r',128), ('g',255), ('b',160)]
parseHtmlColor "#3B7"      === fromList [('r',51), ('g',187), ('b',119)]
parseHtmlColor "LimeGreen" === fromList [('r',50), ('g',205), ('b',50)]
parse("#80FFA0")   === new RGB(128, 255, 160))
parse("#3B7")      === new RGB( 51, 187, 119))
parse("LimeGreen") === new RGB( 50, 205,  50))

// RGB class is defined as follows:
final class RGB {
    public int r, g, b;
    
    public RGB();
    public RGB(int r, int g, int b);
}
Parse("#80FFA0")   === new RGB(128, 255, 160))
Parse("#3B7")      === new RGB( 51, 187, 119))
Parse("LimeGreen") === new RGB( 50, 205,  50))

// RGB struct is defined as follows:
struct RGB
{
    public byte r, g, b;
    public RGB(byte r, byte g, byte b);
}
Parse("#80FFA0")   == Color{128, 255, 160}
Parse("#3B7")      == Color{51, 187, 119}
Parse("LimeGreen") == Color{50, 205, 50}

// Color struct is defined as follows:
type Color struct{ R, G, B byte }
Fundamentals
Algorithms
Strings
Parsing

Stats:

CreatedFeb 28, 2017
PublishedFeb 28, 2017
Warriors Trained8697
Total Skips2463
Total Code Submissions15666
Total Times Completed2771
Ruby Completions154
Python Completions838
JavaScript Completions1190
Clojure Completions72
Haskell Completions103
Java Completions303
C# Completions165
Go Completions19
Total Stars173
% of votes with a positive feedback rating93% of 581
Total "Very Satisfied" Votes504
Total "Somewhat Satisfied" Votes68
Total "Not Satisfied" Votes9
Total Rank Assessments10
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • junthecoder Avatar
  • matt c Avatar
  • zruF Avatar
  • Voile Avatar
  • hobovsky Avatar
  • Lixfeld Avatar
  • saudiGuy Avatar
Ad