Skip to content

Instantly share code, notes, and snippets.

@spreadLink
Created July 8, 2018 20:53
Show Gist options
  • Save spreadLink/dc9a5541df73aa289ab6ca5373dfa601 to your computer and use it in GitHub Desktop.
Save spreadLink/dc9a5541df73aa289ab6ca5373dfa601 to your computer and use it in GitHub Desktop.
Structs and Primitives in Pony
use "lib:tcod"
// declare c-stuff:
use @TCOD_color_RGB[Color](r: U8, g: U8, b: U8)
use @TCOD_color_HSV[Color](h: F32, s: F32, v: F32)
use @TCOD_color_equals[Bool](c1: Color, c2: Color)
use @TCOD_color_add[Color](c1: Color, c2: Color)
use @TCOD_color_subtract[Color](c1: Color, c2: Color)
use @TCOD_color_multiply[Color](c1: Color, c2: Color)
// [snip], there are a lot more functions but that's not important here
// colour:
primitive Color
new create(red: U8, green: U8, blue: U8) =>
@TCOD_color_RGB(red, green, blue)
new from_HSV(hue: F32, saturation: F32, value: F32) =>
@TCOD_color_HSV(hue, saturation, value)
fun equals(other: Color): Bool =>
@TCOD_color_equals(this, other)
fun eq(other: Color): Bool =>
this.equals(other)
fun ne(other: Color): Bool =>
not this.equals(other)
fun add(other: Color): Color =>
@TCOD_color_add(this, other)
fun subtract(other: Color): Color =>
@TCOD_color_subtract(this, other)
fun sub(other: Color): Color =>
this.subtract(other)
fun multiply(other: Color): Color =>
@TCOD_color_multiply(this, other)
fun multiply_scalar(value: F32): Color =>
@TCOD_color_multiply_scalar(this, value)
fun mul(that: (Color | F32)): Color =>
match that
| let other: Color => this.multiply(other)
| let value: F32 => this.multiply_scalar(value)
end
//[snip], it just goes on like this for a while
actor Main
new create(env: Env) =>
c = Color(30, 30, 15)
d = Color(15, 15, 30)
env.out.print((c + d).get_hue().string()) // prints 263.489, which is correct as far as i can tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment