Skip to content

rgb转hex #98

@hushicai

Description

@hushicai

最近看了一道面试题:

实现颜色转换 'rgb(255, 255, 255)' -> '#FFFFFF' 的多种思路。

掘金上看到其中有一个解法挺新颖的:

((1 << 24) + (Number(r) << 16) + (Number(g) << 8) + Number(b)).toString(16).slice(1)

有点不明觉厉,google了一番,终于找到了源头。

https://ryanclark.me/rgb-to-hex-via-binary-shifting/

有空可以翻译一下。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions