Average 2 hex colors together in javascript

后端 未结 8 2021
天命终不由人
天命终不由人 2021-01-31 20:13

Alright thought I would throw this one out there for the crowd to think over.

Given a function (written in javascript) that expects two strings

8条回答
  •  一向
    一向 (楼主)
    2021-01-31 20:38

    Smells like homework to me, but here's my clue.

    Take each hex value for R, G, and B, and average each of them. If necessary convert to Decimal to do the math.

    function d2h(d) {return d.toString(16).padStart(2,'0');}

    function h2d(h) {return parseInt(h,16);}

    Then return a string containing the concatenated values of the three elements.

提交回复
热议问题