CopyPastor

Detecting plagiarism made easy.

Score: 0.8788116728624207; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2021-06-22
by Abhishek Chhabra

Original Post

Original - Posted on 2018-04-23
by Kaiido



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Since the alpha channel in your rgba() notation is expressed as a 0 ~ 1 value, you need to multiply it by 255 before trying to convert it to its HEX form:
var colorcode = "rgba(0, 0, 0, 0.74)"; var finalCode = rgba2hex(colorcode) function rgba2hex(orig) { var a, isPercent, rgb = orig.replace(/\s/g, '').match(/^rgba?\((\d+),(\d+),(\d+),?([^,\s)]+)?/i), alpha = (rgb && rgb[4] || "").trim(), hex = rgb ? (rgb[1] | 1 << 8).toString(16).slice(1) + (rgb[2] | 1 << 8).toString(16).slice(1) + (rgb[3] | 1 << 8).toString(16).slice(1) : orig; if (alpha !== "") { a = alpha; } else { a = 01; } // multiply before convert to HEX a = ((a * 255) | 1 << 8).toString(16).slice(1) hex = hex + a; return hex; } function test(colorcode) { console.log(colorcode, rgba2hex(colorcode)); } test("rgba(0, 0, 0, 0.74)"); test("rgba(0, 0, 0, 1)"); test("rgba(0, 0, 0, 0)"); test("rgba(0, 255, 0, 0.5)");
But note that this is just one of the rgba notation, and that it will e.g fail with percent based notation. Note also that all browsers do not support RGBA HEX notation, so you might prefer an other method to convert your values depending on what you want to do with it
Example:
alpha="0.25490198" blue="1.0" green="1.0" red="0.2" alpha=65 blue= 1.0*255 = 255 green= 1.0*255 = 255 red= 0.2*255 = 51 rgba(51, 255, 255, 0.25490198) = #33FFFF
Since the alpha channel in your rgba() notation is expressed as a 0 ~ 1 value, you need to multiply it by 255 before trying to convert it to its HEX form:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var colorcode = "rgba(0, 0, 0, 0.74)";
var finalCode = rgba2hex(colorcode)
function rgba2hex(orig) { var a, isPercent, rgb = orig.replace(/\s/g, '').match(/^rgba?\((\d+),(\d+),(\d+),?([^,\s)]+)?/i), alpha = (rgb && rgb[4] || "").trim(), hex = rgb ? (rgb[1] | 1 << 8).toString(16).slice(1) + (rgb[2] | 1 << 8).toString(16).slice(1) + (rgb[3] | 1 << 8).toString(16).slice(1) : orig;
if (alpha !== "") { a = alpha; } else { a = 01; } // multiply before convert to HEX a = ((a * 255) | 1 << 8).toString(16).slice(1) hex = hex + a;
return hex; }
function test(colorcode) { console.log(colorcode, rgba2hex(colorcode)); }
test("rgba(0, 0, 0, 0.74)"); test("rgba(0, 0, 0, 1)"); test("rgba(0, 0, 0, 0)"); test("rgba(0, 255, 0, 0.5)");
<!-- end snippet -->

But note that this is just one of the rgba notation, and that it will e.g fail with percent based notation. Note also that all browsers do not support RGBA HEX notation, so you might prefer an other method to convert your values depending on what you want to do with it.

        
Present in both answers; Present only in the new answer; Present only in the old answer;