Question

Task In this challenge you parse RGB colors represented by strings. The formats are primarily used in HTML and CSS. Your task
Specification Challenge.parseHtml Colo r(colo r) Takes a String that represents a color and returns the parsed color as a map
import java.util . Map; public class HtmlColorParser private final Map<String, String> presetColors ; public HtmlColorParser
Task 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 which takes a color as a string and returns the parsed color as a map (see Examples). Input represents one of the following: color The input string 1.6-digit hexadecimal "#RRGGBB" - Each pair of digits represents a value of the channel in hexadecimal: 00 to FF 2.3-digit hexadecimal "#RGB" - Each digit represents a value 0 to F which translates to 2- digit hexadecimal: 0>00, 1 11, 2->22, and so on. 3. Preset color name You have to use the predefined map PRESET_COLORS (Ruby, Python, JavaScript), presetColorsHaskell), 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" ).
Specification Challenge.parseHtml Colo r(colo r) Takes a String that represents a color and returns the parsed color as a map. Parameters color: String-A color represented by color name, 3- digit hexadecimal or 6-digit hexadecimal Return Value HashMap-Aset of numerical RGB values Examples color Return Value "#80FFA0" {"r":128,"g":255," b":160) "# 3B7" {"r":51,"g":187,"b ":119) "LimeGreen" ("r":50,"g":205 , "b ":50)
import java.util . Map; public class HtmlColorParser private final Map presetColors ; public HtmlColorParser ( Map presetColors) presetColors; this.presetColors } public RGB parse (String color) { return new RGB (0, 128, 255); }
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.HashMap;
import java.util.Map;

public class HtmlColorParser {
    private final Map<String, String> presetColors;

    //Initializing preset color in map
    public HtmlColorParser(Map<String, String> presetColors) {
        this.presetColors = presetColors;
    }

    //parsing color string
    public RGB parse(String colorString) throws Exception {
        //when hex code used
        if (colorString.startsWith("#")) {
            String colorValue = colorString.substring(1);
            String reformattedValue = reformatColorValue(colorValue);
            return getRgb(reformattedValue);
        }
        //when preset colors used
        if (presetColors.containsKey(colorString.toLowerCase())) {
            return getRgb(this.presetColors.get(colorString.toLowerCase()).substring(1));
        }

        throw new Exception("Invalid color");
    }


    private String reformatColorValue(String colorValue) {
        if (colorValue.length() == 6) {
            return colorValue;
        }
        return getDoubleString(colorValue.substring(0, 1))
                + getDoubleString(colorValue.substring(1, 2))
                + getDoubleString(colorValue.substring(2, 3));
    }

    private String getDoubleString(String str) {
        return str + str;
    }


    //parsing logic
    private RGB getRgb(String colorString) {
        int red = Integer.parseInt(colorString.substring(0, 2), 16);
        int green = Integer.parseInt(colorString.substring(2, 4), 16);
        int blue = Integer.parseInt(colorString.substring(4, 6), 16);
        return new RGB(red, green, blue);
    }


    public static void main(String args[]) {
        Map<String, String> presetColor = new HashMap<>();
        presetColor.put("limegreen", "#32CD32");
        presetColor.put("green", "#008000");
        presetColor.put("blue", "#0000FF");
        presetColor.put("red", "#FF0000");
        presetColor.put("black", "#000000");
        presetColor.put("white", "#FFFFFF");

        try {
            HtmlColorParser htmlColorParser = new HtmlColorParser(presetColor);
            System.out.println("#80FFA0: " + htmlColorParser.parse("#80FFA0"));
            System.out.println("#3B7: " + htmlColorParser.parse("#3B7"));
            System.out.println("LimeGreen: " + htmlColorParser.parse("LimeGreen"));
            System.out.println("Blue: " + htmlColorParser.parse("Blue"));
            System.out.println("Red: " + htmlColorParser.parse("red"));
        } catch (Exception e) {
            System.out.println("One of the color is invalid");
        }
    }
}

class RGB {
    private final int r;
    private final int b;
    private final int g;


    RGB(int r, int b, int g) {
        this.r = r;
        this.b = b;
        this.g = g;
    }

    @Override
    public String toString() {
        return "{" +
                "r:" + r +
                ", b:" + b +
                ", g:" + g +
                '}';
    }
}
/Library/Java/JavaVirtualMachines/openjdk-12.0.1.jdk/Contents/H #80FFA0: {r:128, b:255, g:160} #3B7: {r:51, b:187, g:119} Lim
Add a comment
Know the answer?
Add Answer to:
Task In this challenge you parse RGB colors represented by strings. The formats are primarily used...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT