Comments (4)
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.
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.
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.
Got it, thanks for the plugin!
from colord.
Related Issues (20)
- Large memory consumption HOT 2
- Types not compatible with moduleResolution Node16
- Colour grouping
- I get wrong color output when input color is LCH HOT 2
- Output in 0xRRGGBB format? HOT 2
- Setting absolute values for LCH
- colord returns orange-ish color when mixing red HOT 2
- Return A11y score as text?
- Create SECURITY.md HOT 1
- err Error: Default condition should be last one
- `HslaColor` parsed to zero in hue value
- "Nearest color" functionality
- Convert HSV object to string
- Auto-generating palettes using LCH
- Colord is unmaintained. What is the best small typescript color library alternative? HOT 1
- ICC vary from CPU-type HOT 1
- a11y does not support opacity HOT 2
- TypeScript 4.7 - Add missing ESM `types` subpath export condition HOT 1
- Incorrectly parse hexa to rgba to hexa
- Add support for modern color syntax HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from colord.