javascript - Isolate alpha value from background colour, convert from string to number, then validate -
i'm looking check value of background colour on element, , if value rgba value want grab alpha channel , see if lower 1. return/exit if background colour isn't rgba value, i.e. if it's hex value, or rgb value.
i have feeling regex best approach this, after bit of head scratching i'm @ loss, , regex isn't strongest skill!
here's have already, , todo's things need little assistance with
var hasopacity = false; var backgroundcolor = $('.elemment-class').css('background-color'); // backgroundcolor return: // (string / bool) // - null - ignore // - transparent - ignore // - #000 - ignore // - #ffffff - ignore // - rgb(0,0,0) - ignore // - rgba(0, 0, 0, 0.4) // ^ need value if it's floating // - rgba(0, 0, 0, 1) // ^ or value if it's integer function checkifbackgroundhasalpha(backgroundcolor) { // @todo if background value of "ignore"s return because don't need // continue if value rgba // rgba lets process if (isaplpa === true) { // @todo first grab alpha section of rgba colour (regex perhaps?) // i.e rgba(255, 255, 255, 0.4) // ^ // or rgba(6, 39, 123, 1) // ^ // @todo convert value string number // run validation if (alphanumber < 1) { // know it's got opacity lets need plus // set our var true later use hasopacity = true; } } } // run check checkifbackgroundhasalpha(backgroundcolor); if (hasopacity) { // yay we're here console.log('yep have opacity on this'); }
thanks
you don't need regex @ all, in fact, can simplify approach down considerably given have specific set of input types.
function hasopacity(str) { if (str) { var tmp = str.split(',')[3]; if (tmp) { var val = parsefloat(tmp); return (!isnan(val) && val < 1); } } return false; } hasopacity("rgba(0, 0, 0, 0.4)"); //true hasopacity("rgba(0,0,0,1)"); //false hasopacity(null); //false hasopacity("#ffffff"); //false
Comments
Post a Comment