+ 2

Hexadecimal in Javascript

Hello, is it possible to use hexadecimal calculations in Javascript? My specific intention is to change a color slowly, like this theoretical code: var color = #000000; for (i=0; i<256; i++) color.red++; After one repeat the color will be #010000 After 10 repeats the color will be #0a0000 Can I do something like this?

8th Nov 2018, 6:15 PM
Yoni Simian
3 odpowiedzi
+ 2
I think you can solve your problem with the following hint: color = '#' + Math.floor(Math.random() * 16777215).toString(16);
8th Nov 2018, 6:40 PM
Mirko Klotzsche
Mirko Klotzsche - avatar
+ 2
You can work with hexadecimal in javascript, but you can't work with color. Fortunately you can make it yourself easily, check this: https://code.sololearn.com/WlN70Fu1pYcF/?ref=app To convert a number to a hexadecimal string, you can use `number.toString(16)`. The `.padStart` makes sure that for values smaller than 16 we insert a zero (For example "a" -> "0a") because hex color codes are exactly two digits per channel. Be aware that the code breaks if you go above 255 :) EDIT: To change a color slowly on a website, you should also take a look at CSS transitions!
8th Nov 2018, 7:09 PM
Schindlabua
Schindlabua - avatar
0
This is color='#' + Math.floor(Math.random()*16777215).toString(16);
9th Nov 2018, 6:16 AM
gingercross123