Last active
April 3, 2018 06:44
-
-
Save xmeta/68646303f3ae7dd12da6e05d0c5def84 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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