Created
November 12, 2023 18:08
-
-
Save peterhellberg/198498dddf0ba0147b4ea9de68041c5b to your computer and use it in GitHub Desktop.
Implementation of https://github.com/nesbox/TIC-80/wiki/code-examples-and-snippets#rainbow-color-function in Zig ⚡
This file contains 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
const tic = @import("tic80.zig"); | |
const Rainbow = struct { | |
red: u8 = 255, | |
grn: u8 = 0, | |
blu: u8 = 0, | |
cyRG: bool = true, | |
cyGB: bool = false, | |
cyBR: bool = false, | |
fn update(self: *Rainbow, slot: u32) void { | |
if (self.cyRG == true) { | |
self.grn +|= 1; | |
if (self.grn == 255) { | |
self.red -|= 1; | |
} | |
if (self.red == 0) { | |
self.cyRG = false; | |
self.cyGB = true; | |
} | |
} | |
if (self.cyGB == true) { | |
self.blu +|= 1; | |
if (self.blu == 255) { | |
self.grn -|= 1; | |
if (self.grn == 0) { | |
self.cyGB = false; | |
self.cyBR = true; | |
} | |
} | |
} | |
if (self.cyBR == true) { | |
self.red +|= 1; | |
if (self.red == 255) { | |
self.blu -|= 1; | |
if (self.blu == 0) { | |
self.cyBR = false; | |
self.cyRG = true; | |
} | |
} | |
} | |
tic.poke(0x3fc0 + (slot * 3) + 2, self.blu); | |
tic.poke(0x3fc0 + (slot * 3) + 1, self.grn); | |
tic.poke(0x3fc0 + (slot * 3), self.red); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updating the color in
BDR
:Updating the color in
TIC
: