What would be the preferred way to convert opacity (0 – 1) to hex (00 – ff) in Javascript?
My thoughts are to use an if statement to check if opacity is between 1 and 0.95 then use ff. Work my way down to 0.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
At the most basic level, you’re just converting a decimal to hex: How to convert decimal to hex in JavaScript?:
The 0.0 – 1.0 range is just a percentage format of the 0-255 range. So multiply your value (e.g. 0.5 * 255) then convert to hex, you’ll get the correct value.