Re: Color parsing
By: Kirkman to echicken on Sun Dec 03 2017 02:36 pm
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.
It's a little odd to be using decimal numbers in bit-wise operations (e.g. 0xf0 makes more sense than 240, given the context).
But I would usually use macros or constants for these magic numbers anyway. Like so (in JS):
const BG_COLOR = 0x70;
const FG_COLOR = 0x07;
The BLINK and HIGH bit values are already defined in sbbsdefs.js (along with all the color bit values).
Anyway, so parsing the foreground color with the high/bright flag:
var fg_color = attr & (FG_COLOR | HIGH);
var bg_color = attr & BG_COLOR; // exclude the blink flag
To convert the background color to a foreground color, just shift-right 4-bits.
var color = bg_color >> 4; // move the upper nibble to the lower
Then you could compare 'color' against the color values defined in sbbsdefs.js (e.g. RED, BLUE, etc.).
To convert a foreground color to a background color, shift-left:
var bg_color = (color & FG_COLOR) << 4;
If you know that "color" won't include the HIGH flag or any other values than 0-7, then you don't need the "& FG_COLOR" part.
And the reason for all this madness?
https://en.wikipedia.org/wiki/Color_Graphics_Adapter
:-)
digital man
Synchronet "Real Fact" #64:
Synchronet PCMS (introduced w/v2.0) is Programmable Command and Menu Structure. Norco, CA WX: 67.9øF, 56.0% humidity, 0 mph ESE wind, 0.00 inches rain/24hrs
---
þ Synchronet þ Vertrauen þ Home of Synchronet þ [vert/cvs/bbs].synchro.net