{"id":22828678,"url":"https://github.com/busterc/contrast-color","last_synced_at":"2025-04-23T16:24:14.091Z","repository":{"id":37672346,"uuid":"330204995","full_name":"busterc/contrast-color","owner":"busterc","description":":art: Determine the best contrast color to use. Give it a background color and it returns a foreground color (black or white by default but customizable).","archived":false,"fork":false,"pushed_at":"2023-02-02T11:17:52.000Z","size":6597,"stargazers_count":24,"open_issues_count":4,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-23T16:24:07.437Z","etag":null,"topics":["accessibility","background","chroma","color","colors","contrast","contrast-color","darken","font-color","foreground","foreground-background","foreground-color","hsl","hsv","luma","luminosity","wcag","yiq"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/busterc.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2021-01-16T16:25:29.000Z","updated_at":"2024-09-07T12:35:27.000Z","dependencies_parsed_at":"2023-02-17T19:01:13.822Z","dependency_job_id":null,"html_url":"https://github.com/busterc/contrast-color","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/busterc%2Fcontrast-color","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/busterc%2Fcontrast-color/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/busterc%2Fcontrast-color/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/busterc%2Fcontrast-color/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/busterc","download_url":"https://codeload.github.com/busterc/contrast-color/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250468597,"owners_count":21435513,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["accessibility","background","chroma","color","colors","contrast","contrast-color","darken","font-color","foreground","foreground-background","foreground-color","hsl","hsv","luma","luminosity","wcag","yiq"],"created_at":"2024-12-12T19:11:24.478Z","updated_at":"2025-04-23T16:24:14.073Z","avatar_url":"https://github.com/busterc.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# contrast-color [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url]\n\u003e Determines the best contrast color to use. Give it a background color and it returns a foreground color (black or white by default but customizable).\n\n## FYI\n\n- [YIQ](https://en.wikipedia.org/wiki/YIQ#:~:text=YIQ%20is%20the%20color%20space,used%20in%20quadrature%20amplitude%20modulation.)\n- [Calculating Color Contrast](https://24ways.org/2010/calculating-color-contrast/)\n\n## Demo ([try it for yourself](https://codepen.io/buster/pen/PoGXdze))\n![](contrastColor.gif)\n\n## Installation\n\n```sh\n$ npm install contrast-color\n```\n\n## Usage\n\n```js\n// Use via class instance\nconst ContrastColor = require('contrast-color');\nconst cc = new ContrastColor({\n  bgColor: \"navy\",\n  fgDarkColor: \"navy\",\n  fgLightColor: \"water\",\n  customNamedColors: {\n    water: \"#00D0FF\",\n  },\n});\n\nconst defaultFgColor = cc.contrastColor();\nconst hasAquaBgColor = cc.contrastColor({ bgColor: \"aqua\" });\n\n/* Results\n{\n  defaultFgColor: '#00D0FF', // water\n  hasAquaBgColor: '#000080'  // navy\n}\n*/\n\n// OR ...\n\n// Use via static method\nconst { contrastColor } = require('contrast-color');\n\nconst hasRedBg = contrastColor({ bgColor: '#f00' });\nconst hasRedBgWithBlackFg = contrastColor({ bgColor: '#f00', threshold: 76 });\nconst hasYellowBg = contrastColor({ bgColor: 'yellow' });\nconst hasWhiteBgWithGreenFg = contrastColor({ fgDarkColor: '#008000' });\nconst hasBlackBgWithLimeFg = contrastColor({ bgColor: '#000000', fgLightColor: 'lime' });\nconst hasKitchenSink = contrastColor({ \n  bgColor: '#808080', \n  fgDarkColor: 'dirty', \n  fgLightColor: 'clean', \n  threshold: 129, \n  customNamedColors: { \n    dirty: '#f90', \n    clean: '#ff99ff' \n  } \n});\n\n/* Results:\n{\n  hasRedBg: '#FFFFFF',              // white\n  hasRedBgWithBlackFg: '#000000',   // black\n  hasYellowBg: '#000000',           // black\n  hasWhiteBgWithGreenFg: '#008000', // green\n  hasBlackBgWithLimeFg: '#00FF00',  // lime\n  hasKitchenSink: '#ff99ff'         // clean\n}\n*/\n```\n\n## API\n\n### ContrastColor(defaults)\n\n- #### defaults \n\n  `Optional`: `Object` with parameters to use.\n\n  - ##### bgColor\n\n    - `Optional` : `String` hex or named (e.g.`#ff0000`, `red`)\n    - `Default`  : `#FFFFFF`\n\n  - ##### fgDarkColor\n\n    Is returned if `bgColor` is determined to be _light_\n\n    - `Optional` : `String` hex or named (e.g.`#ff0000`, `red`)\n    - `Default`  : `#000000`\n\n  - ##### fgLightColor\n\n    Is returned if `bgColor` is determined to be _dark_\n\n    - `Optional` : `String` hex or named (e.g.`#ff0000`, `red`)\n    - `Default`  : `#FFFFFF`\n\n  - ##### defaultColor\n\n    Is returned if `bgColor` is determined to be _invalid_\n\n    - `Optional` : `String` hex or named (e.g.`#ff0000`, `red`)\n    - `Default`  : `#000000`\n\n  - ##### threshold\n\n    - `Optional` : `Number` (0-255) used to adjust variance\n    - `Default`  : `128`\n\n  - ##### customNamedColors\n\n    - `Optional` : `Object` used to override or add named colors (e.g. `{ blue: \"#0074D9\" }`)\n\n\n### .contrastColor(options)\n\n- #### options \n\n  `Requrired`: `Object` with parameters to use.\n\n  - ##### bgColor\n\n    - `Required` : `String` hex or named (e.g.`#ff0000`, `red`)\n\n  - ##### fgDarkColor\n\n    Is returned if `bgColor` is determined to be _light_\n\n    - `Optional` : `String` hex or named (e.g.`#ff0000`, `red`)\n    - `Default`  : `#000000`\n\n  - ##### fgLightColor\n\n    Is returned if `bgColor` is determined to be _dark_\n\n    - `Optional` : `String` hex or named (e.g.`#ff0000`, `red`)\n    - `Default`  : `#FFFFFF`\n\n  - ##### defaultColor\n\n    Is returned if `bgColor` is determined to be _invalid_\n\n    - `Optional` : `String` hex or named (e.g.`#ff0000`, `red`)\n    - `Default`  : `#000000`\n\n  - ##### threshold\n\n    - `Optional` : `Number` (0-255) used to adjust variance\n    - `Default`  : `128`\n\n  - ##### customNamedColors\n\n    - `Optional` : `Object` used to override or add named colors (e.g. `{ blue: \"#0074D9\" }`)\n\n\n## License\n\nISC © [Buster Collings]()\n\n\n[npm-image]: https://badge.fury.io/js/contrast-color.svg\n[npm-url]: https://npmjs.org/package/contrast-color\n[travis-image]: https://travis-ci.com/busterc/contrast-color.svg?branch=master\n[travis-url]: https://travis-ci.com/busterc/contrast-color\n[daviddm-image]: https://david-dm.org/busterc/contrast-color.svg?theme=shields.io\n[daviddm-url]: https://david-dm.org/busterc/contrast-color\n[coveralls-image]: https://coveralls.io/repos/busterc/contrast-color/badge.svg\n[coveralls-url]: https://coveralls.io/r/busterc/contrast-color\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbusterc%2Fcontrast-color","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbusterc%2Fcontrast-color","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbusterc%2Fcontrast-color/lists"}