universe@127: package de.uapcore.lightpit.types; universe@127: universe@127: public final class WebColor { universe@127: universe@127: private final String hex; universe@127: universe@127: /** universe@127: * Constructs a color object from a hex string. universe@127: * @param hex the 6 digits hex string optionally preceded by a hash symbol universe@127: * @throws IllegalArgumentException if the given string does not specify a color universe@127: */ universe@127: public WebColor(String hex) throws IllegalArgumentException { universe@127: this.hex = (hex.startsWith("#") ? hex : ("#"+hex)).toUpperCase(); universe@127: if (!this.hex.matches("#[0-9A-F]{6}")) universe@127: throw new IllegalArgumentException(hex+" is not a color"); universe@127: } universe@127: universe@127: /** universe@127: * Returns the hex representation without th leading hash symbol. universe@127: * @return the hex representation of this color (e.g. FF0000 for red) universe@127: */ universe@127: public String getHex() { universe@127: return hex.substring(1); universe@127: } universe@127: universe@127: @Override universe@127: public String toString() { universe@127: return hex; universe@127: } universe@127: }