javascript - get r, g, b component of a color in rgb() format -
i getting "rgb(18, 115, 224)" dom element. want assign color(whatever getting element) span element. need hexadecimal equivalent of color getting. can utilize
"#" + componenttohex(r) + componenttohex(g) + componenttohex(b)
but, question here's how can r, g,b component values "rgb(18, 115, 224)"
now want assign color(whatever getting element) span element.
no don't, can utilize rgb(18, 115, 224)
straight color value in css. (but see below how hex if needed it.) gratuitous example:
class="snippet-code-js lang-js prettyprint-override">$("#the-span").css("color", "rgb(18, 115, 224)");
class="snippet-code-html lang-html prettyprint-override"><span id="the-span">i'm span</span>
or without jquery, others find later:
class="snippet-code-js lang-js prettyprint-override">document.getelementbyid("the-span").style.color = "rgb(18, 115, 224)";
class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <span id="the-span">i'm span</span>
but let's assume do need hex reason:
class="snippet-code-js lang-js prettyprint-override">function getrgb(str) { var result = /rgb\((\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*\d+\s*)?\)/.exec(str); if (result) { homecoming "#" + tohex(+result[1], 2) + tohex(+result[2], 2) + tohex(+result[3], 2); } homecoming undefined; } // note simplistic tohex appropriate this, not negatives or fractionals function tohex(num, min) { var hex = num.tostring(16); while (hex.length < (min || 0)) { hex = "0" + hex; } homecoming hex; } function test(str) { display(str + " => " + getrgb(str)); } test("rgb(18, 115, 224)"); test("rgb(18, 115, 224, 50)"); function display(msg) { var p = document.createelement('p'); p.innerhtml = string(msg); document.body.appendchild(p); }
that allows possibility of 4th (alpha) argument, ignore.
javascript jquery html css colors
No comments:
Post a Comment