Sunday, April 14, 2024
HomeSoftware EngineeringThe way to Parse HTML/CSS Colours in Java

The way to Parse HTML/CSS Colours in Java


The problem

On this problem, you parse RGB colours represented by strings. The codecs are primarily utilized in HTML and CSS. Your job is to implement a operate that takes a coloration as a string and returns the parsed coloration as a map (see Examples).

Inputs:

The enter string represents one of many following:

  • 6-digit hexadecimal – “#RRGGBB”
    e.g. “#012345”, “#789abc”, “#FFA077”
    Every pair of digits represents a worth of the channel in hexadecimal: 00 to FF
  • 3-digit hexadecimal – “#RGB”
    e.g. “#012”, “#aaa”, “#F5A”
    Every digit represents a worth 0 to F which interprets to 2-digit hexadecimal: 0->00, 1->11, 2->22, and so forth.
  • Preset coloration identify
    e.g. “crimson”, “BLUE”, “LimeGreen”
    You must use the predefined map PRESET_COLORS (JavaScript, Python, Ruby), presetColors (Java, C#, Haskell), or preset-colors (Clojure). The keys are the names of preset colours in lower-case and the values are the corresponding colours in 6-digit hexadecimal (similar as 1. “#RRGGBB”).

Examples:

parse("#80FFA0") === new RGB(128, 255, 160))
parse("#3B7") === new RGB( 51, 187, 119))
parse("LimeGreen") === new RGB( 50, 205,  50))

// RGB class is outlined as follows:
ultimate class RGB {
    public int r, g, b;
    
    public RGB();
    public RGB(int r, int g, int b);
}

The answer in Java code

Choice 1:

import java.util.Map;
import java.awt.Colour;

public class HtmlColorParser {
    personal ultimate Map<String, String> presetColors;
    public HtmlColorParser(Map<String, String> presetColors) {
        this.presetColors = presetColors;
    }
    public RGB parse(String coloration) {
        if (coloration.charAt(0) != '#') coloration = nameFixer(coloration);
        if (coloration.size() < 7) coloration = stringFixer(coloration);
        Colour decoded = (Colour.decode(coloration));
        return new RGB(decoded.getRed(), decoded.getGreen(), decoded.getBlue());
    }
    public String stringFixer(String s) {
        return "#" + s.charAt(1) + s.charAt(1) + s.charAt(2) + s.charAt(2) + s.charAt(3) + s.charAt(3);
    }
    public String nameFixer(String s) {
        return presetColors.get(s.toLowerCase());
    }
}

Choice 2:

import java.util.Map;

class HtmlColorParser {
  personal ultimate Map<String, String> presetColors;
  HtmlColorParser(Map<String, String> presetColors) {
    this.presetColors = presetColors;
  }
  RGB parse(String coloration) {
    if ((coloration = presetColors.getOrDefault(coloration.toLowerCase(), coloration)).size() < 7)
      coloration = coloration.replaceAll("((?i)[da-f])", "$1$1");
    return new RGB(Integer.valueOf(coloration.substring(1, 3), 16), Integer.valueOf(coloration.substring(3, 5), 16), Integer.valueOf(coloration.substring(5), 16));
  }
}

Choice 3:

import java.util.Map;

public class HtmlColorParser {
    personal ultimate Map<String, String> presetColors;
    public HtmlColorParser(Map<String, String> presetColors) {
        this.presetColors = presetColors;
    }
    public RGB parse(String coloration) {
            String lc = coloration.toLowerCase();
            String rgb = presetColors.getOrDefault(lc, lc);
            if(rgb.size() == 4)
                rgb = rgb.replaceAll("([0-9a-f])", "$1$1");
            return new RGB(Integer.valueOf(rgb.substring(1, 3), 16), 
                           Integer.valueOf(rgb.substring(3, 5), 16),
                           Integer.valueOf(rgb.substring(5), 16));
    }
}

Check circumstances to validate our resolution

import java.util.Locale;
import org.junit.Earlier than;
import org.junit.Check;
import static org.junit.Assert.assertEquals;

public class ExampleTests {
    personal HtmlColorParser parser;
    @Earlier than
    public void setup() {
        parser = new HtmlColorParser(PresetColors.getMap());
    }
    @Check
    public void testExamples() {
        shouldParse("#80FFA0", new RGB(128, 255, 160));
        shouldParse("#3B7", new RGB( 51, 187, 119));
        shouldParse("LimeGreen", new RGB( 50, 205,  50));
    }
    personal void shouldParse(String coloration, RGB anticipated) {
        assertRgbEquals(coloration, anticipated, parser.parse(coloration));
    }
    personal static void assertRgbEquals(String enter, RGB anticipated, RGB precise) throws AssertionError {
        strive {
            System.out.printf("enter: "%s"", enter);
            assertEquals(anticipated, precise);
            System.out.println(" => move!");
        } catch (AssertionError e) {
            String message = String.format(Locale.ENGLISH,
                "anticipated: %snactual  : %s", anticipated, precise);
            throw new AssertionError(message, e);
        }
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments