{"id":21885575,"url":"https://github.com/sheraff/palette","last_synced_at":"2025-06-23T11:37:18.225Z","repository":{"id":257283612,"uuid":"857815788","full_name":"Sheraff/palette","owner":"Sheraff","description":null,"archived":false,"fork":false,"pushed_at":"2024-12-08T19:48:38.000Z","size":5733,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-11T08:08:21.344Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Sheraff.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-09-15T17:21:35.000Z","updated_at":"2024-12-08T19:48:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"fd12186a-52a4-4a8e-89c8-1ed4c2fb1ce0","html_url":"https://github.com/Sheraff/palette","commit_stats":null,"previous_names":["sheraff/palette"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Sheraff/palette","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sheraff%2Fpalette","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sheraff%2Fpalette/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sheraff%2Fpalette/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sheraff%2Fpalette/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sheraff","download_url":"https://codeload.github.com/Sheraff/palette/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sheraff%2Fpalette/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261470086,"owners_count":23163213,"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":[],"created_at":"2024-11-28T10:28:04.571Z","updated_at":"2025-06-23T11:37:13.216Z","avatar_url":"https://github.com/Sheraff.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Color palette extraction\n\n\u003e [!WARNING]\n\u003e This library is still in development and the API is subject to change.\n\u003e - The entry point is `extractColors.ts`\n\u003e - use `pnpm test` to launch the tests\n\u003e - use `pnpm serve` to get a visual preview of what is happening on `localhost:3000`\n\nWith an emphasis on \n- **Accuracy**, using a clustering algorithm and saliency feature detection to find the most representative colors.\n  *Other libraries use a simple histogram and manual thresholding / quantization*\n- **Usability**, by returning colors that are visually distinct and can be used in a design.\n  *Other libraries return \"just the main colors\", we also look for contrast and accents*\n- **Speed**, by using worker threads and array buffers.\n  *Other libraries are synchronous and require a lot of memory to run*\n\n\n```ts\nconst colors = await extractColors(buffer, 3, {\n\tworkers: true,\n\tcolorSpace: oklabSpace,\n\tstrategy: gapStatisticKmeans({ max: 10 }),\n\tclamp: 0.005,\n})\n```\n\n## How does it work\n\n| image | step |\n|-----|-----|\n| ![maroon5-scrambled](https://github.com/user-attachments/assets/cfebd938-680a-4a1c-99ca-c78c695c1c83) | original image (*here the original image is scrambled to avoid infringing on copyrights*) |\n| \u003cimg width=\"298\" alt=\"Screenshot 2024-09-29 at 14 52 30\" src=\"https://github.com/user-attachments/assets/d492f285-b65b-488b-b007-41ea189738fa\"\u003e | main colors are extracted by [k-means](https://en.wikipedia.org/wiki/K-means_clustering) clustering |\n| ![maroon5-masked](https://github.com/user-attachments/assets/23166d7a-9dbf-45d6-9461-9eeff1112c66) | the **background** is picked from extracted colors by looking at the most prevalent colors outside of the masked center |\n| ![maroon5-saliency](https://github.com/user-attachments/assets/eafe7d71-6987-4bd8-8c19-7dce327d9722) | the **foreground** is picked from the most salient features by [Itti-Koch filtering](https://en.wikipedia.org/wiki/Laurent_Itti) of the image |\n| \u003cimg width=\"598\" alt=\"Screenshot 2024-09-29 at 14 54 39\" src=\"https://github.com/user-attachments/assets/21d409e2-be9c-464e-8143-d1ea135b2893\"\u003e | main colors are split into 2 pools depending on their proximity with either the background or the foreground |\n| \u003cimg width=\"296\" alt=\"Screenshot 2024-09-29 at 14 55 01\" src=\"https://github.com/user-attachments/assets/84831c11-2103-42d0-8541-4fa2d800cbdb\"\u003e | \u003cul\u003e\u003cli\u003ethe **accent** is picked from the foreground pool based on its *chroma*, *prevalence*, and *distance* to the already picked colors. \u003c/li\u003e\u003cli\u003e the **alternate background** is picked from the background pool based on its *prevalence* and *contrast* with the foreground colors.\u003c/li\u003e\u003c/ul\u003e |\n\n\n## Sample of results\n\n![localhost_3000_ (2)](https://github.com/user-attachments/assets/b3afea74-fd35-4838-a7db-4871d6b512ce)\n\n\n## Multi-threading\n\nA lot of the work done by this library is CPU intensive, so if performance is *at all* a concern, you should enable multithreading by using the `workers: true` option. If [`piscina`](https://www.npmjs.com/package/piscina) is installed, it will be used to manage the worker pool, and the pool can be provided (`worker: pool`) to integrate with the rest of your application\n\n## Unit-testing colors\n\nBecause changes on \"color manipulation\" algorithms can be chaotic, we need to be able to test our resulting colors with some form of *fuzzy matching*. Additionally, color codes don't immediately mentally map to actual colors, making tests hard to read.\n\nFor this we base our tests on \"named colors\" (taken from the CSS list):\n```ts\nt.diagnostic(`accent: ${hex(accent)} \u003e\u003e ${nameColor(accent)} (${simpleColor(accent)})`)\nassert.strictEqual(nameColor(accent), 'lightcoral')\n// will output: ℹ accent: #f85963 \u003e\u003e lightcoral (salmon)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsheraff%2Fpalette","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsheraff%2Fpalette","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsheraff%2Fpalette/lists"}