• Color parsing

    From Kirkman@VERT/GUARDIAN to All on Sun Dec 3 13:21:29 2017
    Quick question for DM or echicken or whoever:ãã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.ãã--Joshãã////--------------------------------------------------ãBiC -=- http://breakintochat.com -=- bbs wiki and blogãã---ã þ Synchronetã
  • From echicken@VERT/ECBBS to Kirkman on Sun Dec 3 15:28:23 2017
    Re: Color parsingã By: Kirkman to All on Sun Dec 03 2017 13:21:29ãã Ki> If I have a Synchronet color value that's already been set (for example, Iã Ki> read the .attr of a character in a frame), how can I parse it to determineã Ki> the foreground color and background color?ãã Ki> I imagine it requires using bitwise operators. I've never reallyã Ki> understood that part.ããA colour attribute byte has a bunch of information packed into it, so you needãto mask out various subfields to see what values they hold.ãã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)ããThe 'high' (or 'bold' or 'bright') status of the foreground colour is in bit 3:ããvar b = attr&HIGH; // Bright if > 0; HIGH is 8 or (1<<3) or 0x08ããThe background colour is bits 4 through 6:ããvar bg = attr&(7<<4);ããLastly there's the BLINK attribute, 128 or (1<<7) or 0x80:ããvar blink = attr&BLINK; // Blinking if > 0, else this is inoffensive text.ããWhen you want to parse it there are a few options:ããswitch (fg|b) {ã case BLUE:ã break;ã case LIGHTBLUE:ã break;ã // and so onã default:ã break; ã}ããswitch (bg) {ã case BG_BLUE:ã break;ã // and so onã default:ã break;ã}ããYou could get away with using the same switch () for either one, but you wouldãhave to shift the bg value over. The background colours are the same as theãforeground colours, just shifted 4 bits to the left:ããfunction get_colour(c) {ã switch (c) {ã case BLUE:ã break;ã case LIGHTBLUE:ã break;ã // and so onã default:ã break;ã }ã}ããvar fgc = get_colour(fg|b);ãvar bgc = get_colour(bg>>4);ããBitwise stuff takes some getting used to, but is easy enough once you get theãhang of it.ãã(I may have made several mistakes in the above, I'm just going from memory.)ãã---ãechickenãelectronic chicken bbs - bbs.electronicchicken.com - 416-273-7230ã þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.comã
  • From echicken@VERT/ECBBS to Kirkman on Sun Dec 3 15:32:27 2017
    Re: Color parsingã By: echicken to Kirkman on Sun Dec 03 2017 15:28:23ãã ec> switch (fg|b) {ããThat was meant to be 'switch (fg | b)'. I forgot about pipe codes in messages.ãã ec> var fgc = get_colour(fg|b);ããLikewise, 'get_colour(fg | b)'.ãã---ãechickenãelectronic chicken bbs - bbs.electronicchicken.com - 416-273-7230ã þ Synchronet þ electronic chicken bbs - bbs.electronicchicken.comã
  • From Kirkman@VERT/GUARDIAN to echicken on Sun Dec 3 14:36:33 2017
    Re: Color parsingã By: echicken to Kirkman on Sun Dec 03 2017 03:28 pmãã ec> The foreground colour is in the lower three bits of the byte.ã ec> 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.ãã--Joshãã////--------------------------------------------------ãBiC -=- http://breakintochat.com -=- bbs wiki and blogãã---ã þ Synchronetã
  • From Digital Man@VERT to Kirkman on Sun Dec 3 13:12:23 2017
    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ã >ã > ec> The foreground colour is in the lower three bits of the byte.ã > ec> 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ã