Skip to content

Instantly share code, notes, and snippets.

@xmeta
Last active April 3, 2018 06:44
Show Gist options
  • Save xmeta/68646303f3ae7dd12da6e05d0c5def84 to your computer and use it in GitHub Desktop.
Save xmeta/68646303f3ae7dd12da6e05d0c5def84 to your computer and use it in GitHub Desktop.
abstract class Color() of Red | Green | Blue | Rgb {}
class Red() extends Color() {}
class Green() extends Color() {}
class Blue() extends Color() {}
class Rgb(r, g, b) extends Color() {
shared Integer r;
shared Integer g;
shared Integer b;
}
String intToHex(Integer num, Integer min) {
value s = formatInteger(num, 16);
return s.padLeading(min, '0');
}
String toHex(Color root) {
switch (root)
case (is Red) { return "#FF0000"; }
case (is Green) { return "#00FF00"; }
case (is Blue) { return "#0000FF"; }
case (is Rgb) {
value hex = "#" + intToHex(root.r, 2) + intToHex(root.g, 2) + intToHex(root.b, 2);
return hex.uppercased;
}
}
void run() {
value color = Rgb(100, 1, 255);
print(toHex(color));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment