Extend hex value

JavaScript, String · Sep 15, 2020

Extends a 3-digit color code to a 6-digit color code.

const extendHex = shortHex =>
  '#' +
  shortHex
    .slice(shortHex.startsWith('#') ? 1 : 0)
    .split('')
    .map(x => x + x)
    .join('');
extendHex('#03f'); // '#0033ff'
extendHex('05a'); // '#0055aa'

More like this

  • Colors in JavaScript

    Working with color in JavaScript requires some understanding of color formats and conversions. Luckily, this snippet collection's got you covered.

    Collection · 13 snippets

  • Hex to RGB

    Converts a color code to an rgb() or rgba() string if alpha value is provided.

    JavaScript, String · Oct 19, 2020

  • RGB to hex

    Converts the values of RGB components to a hexadecimal color code.

    JavaScript, String · Nov 3, 2020

  • Change color lightness

    Changes the lightness value of an hsl() color string.

    JavaScript, String · Oct 31, 2020