Coder Social home page Coder Social logo

Support hex codes without the # about colord HOT 4 CLOSED

omgovich avatar omgovich commented on May 19, 2024 1
Support hex codes without the #

from colord.

Comments (4)

omgovich avatar omgovich commented on May 19, 2024

Hi! Previously (in v1.x) Colord worked as you described, but this behavior causes a lot of weird errors in many projects, so since v2 Colord strictly follows CSS <color> specs. According to the specs, hexadecimal colors must have # prefix.

The easiest way for you is to use Colord v1.7.2.

Or you can define your own local plugin:

// utils/colord.js
import { colord, extend } from 'colord';
import namesPlugin from 'colord/plugins/names';

const lazyHexMatcher = /^([0-9a-f]{3,6})$/i;

const parseLazyHex = (value: string) => {
  const match = lazyHexMatcher.exec(value);

  if (!match) return null;

  const hex = match[1];

  if (hex.length === 3) {
    return {
      r: parseInt(hex[0] + hex[0], 16),
      g: parseInt(hex[1] + hex[1], 16),
      b: parseInt(hex[2] + hex[2], 16),
      a: 1,
    };
  }

  if (hex.length === 6) {
    return {
      r: parseInt(hex.substr(0, 2), 16),
      g: parseInt(hex.substr(2, 2), 16),
      b: parseInt(hex.substr(4, 2), 16),
      a: 1,
    };
  }

  return null;
};

const lazyHexPlugin = (ColordClass, parsers) => {
  parsers.string.push([parseLazyHex, "hex"]);
};

extend([lazyHexPlugin, namesPlugin])

export * from 'colord'
// anyOtherFile.js
import { colord } from "./utils/colord.js"

colord('fff').toHslString() // "hls(0, 0%, 100%)"

from colord.

marcofugaro avatar marcofugaro commented on May 19, 2024

Thanks! Any chance you can make this into an importable plugin as well?
It's a feature that was dropped, would be useful if it was at least optional.

from colord.

omgovich avatar omgovich commented on May 19, 2024

We're trying to be focused on the CSS specs, so providing any logic that doesn't follow those rules might mislead the developers using the library and cause errors in their projects. We already had pretty big issues in the past because of the lazy parsing, so I think we not having non-strict hex parsing in plans at the moment.

from colord.

marcofugaro avatar marcofugaro commented on May 19, 2024

Got it, thanks for the plugin!

from colord.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.