RGB to hex and hex to RGB

前端 未结 30 3342
遥遥无期
遥遥无期 2020-11-21 06:56

How to convert colors in RGB format to hex format and vice versa?

For example, convert \'#0080C0\' to (0, 128, 192).

30条回答
  •  庸人自扰
    2020-11-21 07:17

    While this answer is unlikely to fit the question perfectly it may be very useful none the less.

    1. Create any random element

    var toRgb = document.createElement('div');

    1. Set any valid style to the color you want to convert

    toRg.style.color = "hsl(120, 60%, 70%)";

    1. Call the style property again

    > toRgb.style.color;

    < "rgb(133, 225, 133)" Your color has been converted to Rgb

    Works for: Hsl, Hex

    Does not work for: Named colors

提交回复
热议问题