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: | digital man |
---|---|
Location: | Riverside County, California |
Users: | 1,067 |
Nodes: | 17 (0 / 17) |
Uptime: | 07:18:35 |
Calls: | 501,266 |
Calls today: | 13 |
Files: | 109,409 |
D/L today: |
12,962 files (9,088M bytes) |
Messages: | 302,255 |
Posted today: | 10 |