Last active
March 30, 2024 03:41
-
-
Save suhodolskiy/d14b74e183607bbb34eae3313bce7c80 to your computer and use it in GitHub Desktop.
Contrasting color [Colord]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Plugin } from 'colord' | |
declare module 'colord/colord' { | |
interface Colord { | |
contrasting(): Colord | |
} | |
} | |
const plugin: Plugin = (ColordClass) => { | |
ColordClass.prototype.contrasting = function () { | |
const isLight = this.isLight() | |
let color = this | |
while(color.contrast(this) < 4.5) color = isLight ? color.darken(0.4) : color.lighten(0.4) | |
return color | |
} | |
} | |
export default plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Colord, colord, extend } from 'colord' | |
import a11yPlugin from 'colord/plugins/a11y' | |
import contrastingPlugin from './contrasting' | |
extend([a11yPlugin, contrastingPlugin]) | |
colord('#ebfde8').contrasting().toHex() |
artalar
commented
Feb 22, 2022
@artalar Thanks. I have updated
Infinite loop when passing red color 🤔
My page got broken using this plugin. Maybe it needs a way to break the infinite loop? 😅
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment