Created
August 10, 2015 21:23
-
-
Save jaytaph/9cd3b2c8bd395acb7633 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
import io; | |
class Color { | |
// Keeps numerical RGB value of the color | |
protected property value; | |
// Internal list of color names and RGB pairs. | |
protected property names = hash[[ | |
"white" : 0xFFFFFF, | |
"black" : 0x000000, | |
"red" : 0xFF0000, | |
"green" : 0x00FF00, | |
"blue" : 0x0000FF, | |
"yellow" : 0xFFFF00, | |
]]; | |
// Constructor (should be method overloading with __ctor(Numerical) for hex numbers, but | |
// that is not functional yet). | |
public method __ctor(string name) { | |
// Parse hex value RGB if string starts with # | |
if (name[0] == '#') { | |
self.value = ("0x" + name[1..]).__numerical(); | |
return self; | |
} | |
// Use custom color name (if any) | |
self.value = self.names[name]; | |
if (self.value) { | |
return self; | |
} | |
// Nothing found, throw exception | |
throw ArgumentException("Cannot find color name"); | |
} | |
// Coerce method (not yet implemented) | |
public method __coerce(obj) { | |
if (obj.__instanceOf(String)) { | |
return tuple[[color(obj), self]]; | |
} | |
if (obj.__instanceOf(Numerical)) { | |
return tuple[[color("#" + obj.hex()), self]]; | |
} | |
throw TypeError("Cannot coerce " + obj.__name() + " into a Color object"); | |
} | |
// Operator overloading for the + operator | |
public method __opr_add(Color c) { | |
a = c.getValue(); | |
io.println("__opr_add 1: ", a.hex()); | |
io.println("__opr_add 2: ", self.value.hex()); | |
return Color("#" + (a + self.value).hex()); | |
} | |
// Return hex value | |
public method getHexValue() { | |
return self.getValue().hex(); | |
} | |
// Return numerical value | |
public method getValue() { | |
return self.value; | |
} | |
// Return name of color (if any available) | |
public method getName() { | |
// We should have something like: k = hash.searchKey(v) | |
foreach (self.names as k, v) { | |
if (self.value == v) { | |
return k; | |
} | |
} | |
return "unknown"; | |
} | |
} | |
c1 = Color("white"); | |
io.println(c1.getHexValue()); | |
io.println(c1.getName()); | |
io.println(); | |
c2 = Color("red"); | |
io.println(c2.getHexValue()); | |
io.println(c2.getName()); | |
io.println(); | |
c3 = Color("#00FF00"); | |
io.println(c3.getHexValue()); | |
io.println(c3.getName()); | |
io.println(); | |
c4 = Color("#123456"); | |
io.println(c4.getHexValue()); | |
io.println(c4.getName()); | |
io.println(); | |
c5 = Color("#FF0000"); | |
io.println(c5.getHexValue()); | |
io.println(c5.getName()); | |
io.println(); | |
io.println("Here be dragons...\n"); | |
// Works, because c3 and c5 are "equal" types | |
c6 = c3 + c5; | |
io.println(c6.getHexValue()); | |
io.println(c6.getName()); | |
io.println(); | |
/* | |
c7 = c3 + "white"; | |
c8 = c3 + 0x0000FF; | |
c9 = 0x0000FF + c3; | |
c10 = "white" + c3; | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment