|
| 1 | +const hexToRGBA = (hexColor,opacity) => { |
| 2 | + var input = document.getElementById("hex-color").value; |
| 3 | + var output = document.getElementById("rgb-color"); |
| 4 | + var hexRegx = /^#[a-fA-F0-9]{6}$|^#[a-fA-F0-9]{3}$/; |
| 5 | + |
| 6 | + if (input.match(hexRegx)) { |
| 7 | + var hexColor = input.replace("#", ""); |
| 8 | + var r, g, b; |
| 9 | + |
| 10 | + if (hexColor.length === 3) { |
| 11 | + r = parseInt(hexColor[0] + hexColor[0], 16); |
| 12 | + g = parseInt(hexColor[1] + hexColor[1], 16); |
| 13 | + b = parseInt(hexColor[2] + hexColor[2], 16); |
| 14 | + } else if (hexColor.length === 6) { |
| 15 | + r = parseInt(hexColor.substring(0, 2), 16); |
| 16 | + g = parseInt(hexColor.substring(2, 4), 16); |
| 17 | + b = parseInt(hexColor.substring(4, 6), 16); |
| 18 | + } |
| 19 | + if(!isNaN(opacity) && opacity >= 0 && opacity <= 1) |
| 20 | + { |
| 21 | + output.value = "rgba(" + r + "," + g + "," + b + "," + opacity + ")"; |
| 22 | + } |
| 23 | + else |
| 24 | + { |
| 25 | + output.value = "rgba(" + r + "," + g + "," + b + ")"; |
| 26 | + } |
| 27 | + } |
| 28 | + else { |
| 29 | + alert('Enter Correct Color code'); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +const convert = () =>{ |
| 34 | + var hexColor = document.getElementById("hex-color").value; |
| 35 | + var opacity = document.getElementById("opacity").value; |
| 36 | + |
| 37 | + if(!isNaN(opacity) && opacity>=0 && opacity<=1) |
| 38 | + { |
| 39 | + hexToRGBA(hexColor,opacity) |
| 40 | + } |
| 41 | + else |
| 42 | + { |
| 43 | + alert('Opacity value should be between [0 - 1]'); |
| 44 | + } |
| 45 | +} |
0 commit comments