If I have a Synchronet color value that's already been set (for example, I read the .attr of a character in a frame), how can I parse it to determine the foreground color and background color?
I imagine it requires using bitwise operators. I've never really understood that part.
switch (fg|b) {
var fgc = get_colour(fg|b);
The foreground colour is in the lower three bits of the byte.
var fg = attr&7; // 7 is essentially (1<<0)|(1<<1)|(1<<2)
Re: Color parsing
By: echicken to Kirkman on Sun Dec 03 2017 03:28 pm
The foreground colour is in the lower three bits of the byte.
var fg = attr&7; // 7 is essentially (1<<0)|(1<<1)|(1<<2)
Wow, that was fast! Thanks, echicken.
I was looking around online and experimenting with a bitwise calculator, and I ended up doing something like this:
// Use bitwise AND with a mask to get the four rightmost bits.
// dec 15 | bin 00001111 | hex 0xf
var fg = theAttr & 15;
// Use bitwise AND with a mask to get the four leftmost bits.
// dec 240 | bin 11110000 | hex 0xf0
var bg = theAttr & 240;
I think it's working, but I have other issues in my code to sort out before I can say 100%.
Then I'm doing fg = fg & 7 to change high colors into low equivalents.
Sysop: | Kurt Hamm |
---|---|
Location: | Columbia, SC |
Users: | 8 |
Nodes: | 20 (0 / 20) |
Uptime: | 134:27:46 |
Calls: | 2,804 |
Calls today: | 1 |
Files: | 64 |
Messages: | 854,503 |