Last active
August 29, 2015 14:27
-
-
Save jaytaph/c28e6fd38eea50e2b8ee 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, | |
"cyan" : 0x00FFFF, | |
]]; | |
// 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() & 0xFFFFFF; | |
return; | |
} | |
// Use custom color name (if any) | |
self.value = self.names[name]; | |
if (self.value) { | |
return; | |
} | |
// Nothing found, throw exception | |
throw argumentException("Cannot find color name: '" + name + "'"); | |
} | |
// Coerce method to cast "something" to a color (if possible) | |
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(); | |
v = (a | self.value) & 0xFFFFFF; | |
return Color("#" + (v).hex()); | |
} | |
// overloading for the == comparison | |
public method __cmp_eq(Color c) { | |
return self.getHexValue() == c.getHexValue(); | |
} | |
// Return hex value as a string | |
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("#FF0002"); | |
io.println(c5.getHexValue()); | |
io.println(c5.getName()); | |
io.println(); | |
io.println("Here be dragons..."); | |
// Works, because c3 and c5 are "equal" types | |
c6 = c3 + c5; | |
io.println(c6.getHexValue()); | |
io.println(c6.getName()); | |
io.println(); | |
c7 = c3 + "white"; | |
io.println(c7.getHexValue()); | |
io.println(c7.getName()); | |
io.println(); | |
c8 = c3 + 0x0000FF; | |
io.println(c8.getHexValue()); | |
io.println(c8.getName()); | |
io.println(); | |
c8_1 = c3 + c3; | |
io.println(c8_1.getHexValue()); | |
io.println(c8_1.getName()); | |
io.println(); | |
c8_1 = c3 + 0xFFFFFF; | |
io.println(c8_1.getHexValue()); | |
io.println(c8_1.getName()); | |
io.println(); | |
io.println("Moar dragons..."); | |
if (Color("#FFFFFF") == "white") { | |
io.println("Color is white."); | |
} else { | |
io.println("Nope. Not white."); | |
} | |
if (Color("#FFFF00") == "white") { | |
io.println("Color is white."); | |
} else { | |
io.println("Nope. Not white."); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment