{"id":25407680,"url":"https://github.com/calico32/postcss-color-contrast","last_synced_at":"2026-04-05T23:33:04.339Z","repository":{"id":59145006,"uuid":"523400001","full_name":"calico32/postcss-color-contrast","owner":"calico32","description":"PostCSS `color-contrast()` polyfill","archived":false,"fork":false,"pushed_at":"2022-08-30T21:54:19.000Z","size":1252,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-03T19:05:29.273Z","etag":null,"topics":["color-contrast","css","polyfill","postcss"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/calico32.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-08-10T15:35:24.000Z","updated_at":"2024-02-01T15:32:15.000Z","dependencies_parsed_at":"2022-09-13T01:25:42.299Z","dependency_job_id":null,"html_url":"https://github.com/calico32/postcss-color-contrast","commit_stats":null,"previous_names":["wiisportsresort/postcss-color-contrast"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/calico32/postcss-color-contrast","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calico32%2Fpostcss-color-contrast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calico32%2Fpostcss-color-contrast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calico32%2Fpostcss-color-contrast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calico32%2Fpostcss-color-contrast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/calico32","download_url":"https://codeload.github.com/calico32/postcss-color-contrast/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calico32%2Fpostcss-color-contrast/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31454197,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-05T21:22:52.476Z","status":"ssl_error","status_checked_at":"2026-04-05T21:22:51.943Z","response_time":75,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["color-contrast","css","polyfill","postcss"],"created_at":"2025-02-16T07:17:54.912Z","updated_at":"2026-04-05T23:33:04.325Z","avatar_url":"https://github.com/calico32.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# postcss-color-contrast\n\n\u003cp\u003e\n  \u003ca href=\"https://www.npmjs.com/package/postcss-color-contrast\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/v/postcss-color-contrast\"\u003e\n    \u003cimg src=\"https://img.shields.io/bundlephobia/min/postcss-color-contrast\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\nThis PostCSS plugin implements a polyfill for the `color-contrast()` function, part of the extremely new [CSS Color Module Level 6](https://drafts.csswg.org/css-color-6) spec.\n\nBecause the color is computed at compile-time, this plugin only supports literal colors (i.e. hex, `rgb()`, `hsl()`) and does not support `var()` or `calc()` expressions, `currentColor`, etc., and therefore must be placed near the end of the PostCSS chain.\n\nNo guarantees are made about the spec-compliance of this plugin, but I tried my best.\n\n## TL;DR Spec behavior\n\n### Syntax\n\n```css\nselector {\n  color: color-contrast(\u003ccolor\u003e vs \u003ccolor\u003e, \u003ccolor\u003e[, ...] [to \u003cnumber\u003e | AA | AA-large | AAA | AAA-large]);\n}\n```\n\nI'll call the first color the \"background\", the list of colors the \"foreground\" colors, and the number/keyword the \"target ratio\".\n\n### Semantics\n\n**No target ratio: `color-contrast(\u003ccolor\u003e vs \u003ccolor\u003e, \u003ccolor\u003e[, ...])`**\n\n- The foreground color that has the highest contrast ratio with the background color is used.\n\n**Target ratio: `color-contrast(\u003ccolor\u003e vs \u003ccolor\u003e, \u003ccolor\u003e[, ...] to \u003cnumber\u003e | AA | AA-large | AAA | AAA-large)`**\n\n- The first foreground color in the list that has a contrast ratio greater than or equal to the target ratio is used.\n- AA-large is the same as `3`.\n- AA is the same as `4.5`.\n- AAA-large is the same as `4.5`.\n- AAA is the same as `7`.\n\n## Usage\n\n```sh\nnpm install --save-dev postcss-color-contrast\nyarn add -D postcss-color-contrast\n```\n\nIn your `postcss.config.js` file:\n\n```js\nmodule.exports = {\n  plugins: [\n    require('postcss-color-contrast'),\n  ],\n};\n```\n\nOr programmatically:\n\n```js\nconst postcss = require('postcss')\n\npostcss([\n  // ... other plugins here\n  require('postcss-color-contrast'),\n  // ... maybe more plugins here\n]).process(css)\n```\n\nAdditionally, use the `colorContrast()` function from JavaScript:\n\n```js\nimport colorContrast from 'postcss-color-contrast/js'\nconst colorContrast = require('postcss-color-contrast/js')\n// -\u003e colorContrast(backgroundColor, foregroundColors, targetRatio, outputFormat)\n\n// provide colors in any valid CSS color format, or a [r, g, b] array\ncolorContrast('#0f172a', ['#e5e5e5', '#171717']) // -\u003e '#e5e5e5'\ncolorContrast('rgb(194, 65, 12)', ['hsl(0, 0%, 90%)', [23, 23, 23]], 'aa') // -\u003e '#ffffff'\n\n// provide an output format (defaults to 'hex')\nconst bg = '#6ee7b7'\nconst fg = ['#ecfdf5', '#d1fae5', '#a7f3d0', '#6ee7b7', '#34d399', '#10b981', '#059669', '#047857', '#065f46', '#064e3b']\nconst target = 'aa'\ncolorContrast(bg, fg, target)   // -\u003e '#065f46'\ncolorContrast(..., 'hex')       // -\u003e '#065f46'\ncolorContrast(..., 'rgb')       // -\u003e 'rgb(6, 95, 70)'\ncolorContrast(..., 'rgb-array') // -\u003e [6, 95, 70]\ncolorContrast(..., 'hsl')       // -\u003e 'hsl(163, 88%, 20%)' (rounded)\ncolorContrast(..., 'hsl-array') // -\u003e [163.14606741573033, 0.8811881188118811, 0.19803921568627453] (not rounded)\n```\n\n\n## Examples\n\n*Arguments to `color-contrast()` wrapped onto multiple lines for clarity.*\n\n\u003ctable\u003e\n\u003ctr\u003e\u003cth\u003eInput\u003c/th\u003e\u003cth\u003eOutput\u003c/th\u003e\u003cth\u003eImage\u003c/th\u003e\u003c/tr\u003e\n\u003ctr\u003e\n\u003ctd\u003e\n\n```css\n.container {\n  background-color: #0f172a;\n  color: color-contrast(\n    #0f172a vs \n    #e5e5e5, #171717\n  );\n}\n```\n\n\u003c/td\u003e\n\u003ctd\u003e\n\n```css\n.container {\n  background-color: #0f172a;\n  /*\n    picks the color in the list \n    (after \"vs\") that has the\n    highest contrast ratio with\n    the first color\n  */\n  color: #e5e5e5;\n}\n```\n\n\u003c/td\u003e\n\u003ctd\u003e\n\n![](./img/black.png)\n\n\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n\u003ctd\u003e\n\n```css\n.container {\n  background-color: #6ee7b7;\n  color: color-contrast(\n    #6ee7b7 vs \n    #ecfdf5, #d1fae5,\n    #a7f3d0, #6ee7b7,\n    #34d399, #10b981,\n    #059669, #047857,\n    #065f46, #064e3b \n    to AA\n  );\n}\n```\n\n\u003c/td\u003e\n\u003ctd\u003e\n\n```css\n.container {\n  background-color: #6ee7b7;\n  /*\n    with a target contrast:\n    picks the first color\n    that meets or exceeds \n    the target contrast ratio\n  */\n  color: #065f46; /* 5.41:1 */\n}\n```\n\n\u003c/td\u003e\n\u003ctd\u003e\n\n![](./img/green.png)\n\n\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n\u003ctd\u003e\n\n```css\n.container {\n  background-color: #c2410c;\n  color: color-contrast(\n    #c2410c vs \n    #e5e5e5, #171717 \n    to AA\n  );\n}\n```\n\n\u003c/td\u003e\n\n\u003ctd\u003e\n\n```css\n.container {\n  background-color: #c2410c;\n  /* \n    defaults to black or white \n    if the target contrast cannot \n    be met with the provided colors \n  */\n  color: #fff;\n}\n```\n\n\u003c/td\u003e\n\n\u003ctd\u003e\n\n![](./img/orange.png)\n\n\u003c/td\u003e\n\u003c/tr\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalico32%2Fpostcss-color-contrast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcalico32%2Fpostcss-color-contrast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalico32%2Fpostcss-color-contrast/lists"}