Software Engineering

Easy methods to Parse HTML/CSS Colours in Java

Easy methods to Parse HTML/CSS Colours in Java
Written by admin


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 colour as a string and returns the parsed colour 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 colour title
    e.g. “crimson”, “BLUE”, “LimeGreen”
    It’s a must to 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:
remaining 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.Coloration;

public class HtmlColorParser {
    non-public remaining Map<String, String> presetColors;
    public HtmlColorParser(Map<String, String> presetColors) {
        this.presetColors = presetColors;
    }
    public RGB parse(String colour) {
        if (colour.charAt(0) != '#') colour = nameFixer(colour);
        if (colour.size() < 7) colour = stringFixer(colour);
        Coloration decoded = (Coloration.decode(colour));
        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 {
  non-public remaining Map<String, String> presetColors;
  HtmlColorParser(Map<String, String> presetColors) {
    this.presetColors = presetColors;
  }
  RGB parse(String colour) {
    if ((colour = presetColors.getOrDefault(colour.toLowerCase(), colour)).size() < 7)
      colour = colour.replaceAll("((?i)[da-f])", "$1$1");
    return new RGB(Integer.valueOf(colour.substring(1, 3), 16), Integer.valueOf(colour.substring(3, 5), 16), Integer.valueOf(colour.substring(5), 16));
  }
}

Choice 3:

import java.util.Map;

public class HtmlColorParser {
    non-public remaining Map<String, String> presetColors;
    public HtmlColorParser(Map<String, String> presetColors) {
        this.presetColors = presetColors;
    }
    public RGB parse(String colour) {
            String lc = colour.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 {
    non-public 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));
    }
    non-public void shouldParse(String colour, RGB anticipated) {
        assertRgbEquals(colour, anticipated, parser.parse(colour));
    }
    non-public static void assertRgbEquals(String enter, RGB anticipated, RGB precise) throws AssertionError {
        attempt {
            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);
        }
    }
}

About the author

admin

Leave a Comment