{"_id":"k-colors","_rev":"7-5ff31c43ee0f8d9bf3a7653c216c5714","name":"k-colors","dist-tags":{"latest":"0.3.0"},"versions":{"0.0.0":{"name":"k-colors","version":"0.0.0","_id":"k-colors@0.0.0","maintainers":[{"name":"ppzreboot","email":"ppzreboot@qq.com"}],"dist":{"shasum":"00fe117af6fd5a88bbad7170a914142875f8865a","tarball":"https://registry.npmjs.org/k-colors/-/k-colors-0.0.0.tgz","fileCount":6,"integrity":"sha512-5bIC3cD0OOyIO7FH9HAKYInPn5VRlt1/5l6vOj874QkM+S9uKQShiW5QaWgtwyW92uCsq3XRqBLVaC7CYn2Bng==","signatures":[{"sig":"MEUCIQDZLbn0W2KJ+dg6QvF7DutwDUh45gLUIIftGDjr7Wjm3AIgOgw5f8nklimfj1RYFmQ3VtbIU0LnVNkqcfa1CVtyYug=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":2335},"main":"kcpp.js","types":"./kcpp.d.ts","gitHead":"1cc6126bf5b12190957d875213634416fb35710e","_npmUser":{"name":"ppzreboot","email":"ppzreboot@qq.com"},"_npmVersion":"10.7.0","description":"","directories":{},"_nodeVersion":"22.2.0","_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/k-colors_0.0.0_1725888986373_0.4358405677412136","host":"s3://npm-registry-packages"}},"0.0.1":{"name":"k-colors","version":"0.0.1","keywords":["color","k-means++","k-means","kmeans"],"author":{"name":"ppz"},"license":"Unlicense","_id":"k-colors@0.0.1","maintainers":[{"name":"ppzreboot","email":"ppzreboot@qq.com"}],"homepage":"https://github.com/ppzreboot/k-colors.js#readme","bugs":{"url":"https://github.com/ppzreboot/k-colors.js/issues"},"dist":{"shasum":"131e714080a2670ac96b611339d3791f4f279cec","tarball":"https://registry.npmjs.org/k-colors/-/k-colors-0.0.1.tgz","fileCount":14,"integrity":"sha512-MYUgHFG4PPMBOosPtnQHj+xtEP8j3n+swLokg/DGzCfGOfRoKwPt/tUDUJ1MfqRNcRrJOdqdBl7yFIBFy/8K1g==","signatures":[{"sig":"MEYCIQCDarJVQfkCeaoV88wnjXkTSj598ufaZK+A2zQI4dx73AIhAPNVh7VhsJ3W+0ZYejsrWaRIBUsxpGEija3KnxYwVzDb","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":5082},"main":"kcpp.js","types":"./kcpp.d.ts","gitHead":"1cc6126bf5b12190957d875213634416fb35710e","_npmUser":{"name":"ppzreboot","email":"ppzreboot@qq.com"},"repository":{"url":"git+https://github.com/ppzreboot/k-colors.js.git","type":"git"},"_npmVersion":"10.7.0","description":"","directories":{},"_nodeVersion":"22.2.0","_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/k-colors_0.0.1_1725940553553_0.31602379893552746","host":"s3://npm-registry-packages"}},"0.0.2":{"name":"k-colors","version":"0.0.2","keywords":["color","k-means++","k-means","kmeans"],"author":{"name":"ppz"},"license":"Unlicense","_id":"k-colors@0.0.2","maintainers":[{"name":"ppzreboot","email":"ppzreboot@qq.com"}],"homepage":"https://github.com/ppzreboot/k-colors.js#readme","bugs":{"url":"https://github.com/ppzreboot/k-colors.js/issues"},"dist":{"shasum":"ee747df1ff3cf2d6c28a060f2b91b64639cca19c","tarball":"https://registry.npmjs.org/k-colors/-/k-colors-0.0.2.tgz","fileCount":14,"integrity":"sha512-AQBrp6XBkH4VYHaEyfLSPsAhWSqrjmA1ozXvCU+/eoly1hMS0XXBUiTUOMaMgOwDkmpqa00lBSu/Lu/UyYXzBA==","signatures":[{"sig":"MEUCIQDglv+qGG6qWn2h9b+bQ4zzkBCAM3/I08uzq5iZtCAEqwIgN5ea6MlZgEU2s5E68WqbH7N15A2swP6kalQx2eorFz8=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":6081},"main":"kcpp.js","types":"./kcpp.d.ts","gitHead":"e82510655e3ae988e600eeadc9adb7fdba738e0d","_npmUser":{"name":"ppzreboot","email":"ppzreboot@qq.com"},"repository":{"url":"git+https://github.com/ppzreboot/k-colors.js.git","type":"git"},"_npmVersion":"10.7.0","description":"[demo](https://ppzreboot.github.io/k-colors.js/) | [github](https://github.com/ppzreboot/k-colors.js) | [npm](https://www.npmjs.com/package/k-colors)","directories":{},"_nodeVersion":"22.2.0","dependencies":{"k-means-pp":">=2.1.0"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/k-colors_0.0.2_1725943056103_0.274871539548198","host":"s3://npm-registry-packages"}},"0.0.3":{"name":"k-colors","version":"0.0.3","keywords":["color","k-means++","k-means","kmeans"],"author":{"name":"ppz"},"license":"Unlicense","_id":"k-colors@0.0.3","maintainers":[{"name":"ppzreboot","email":"ppzreboot@qq.com"}],"homepage":"https://github.com/ppzreboot/k-colors.js#readme","bugs":{"url":"https://github.com/ppzreboot/k-colors.js/issues"},"dist":{"shasum":"228c921986f5de3619dfb726e5b5e7a38d5cda8e","tarball":"https://registry.npmjs.org/k-colors/-/k-colors-0.0.3.tgz","fileCount":14,"integrity":"sha512-PtoQoyyTm7oTOiw+smLPjE49liizQ2RUa0afGD+w1/pmzxGZ5pJvTTOOhiO0xcuQ66Zc14n5P6n4t99woznUXQ==","signatures":[{"sig":"MEUCIQD8m1EHvT1SYG0uv/AavBbkYxVK8GQO2CZ+hW+L9HI+yAIgRhG20ceNiC/tGugPpegbNNbjmEIUGIYZngxRL30PpHw=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":9049},"main":"kcpp.js","types":"./kcpp.d.ts","gitHead":"867f33bf546898fe993f49b5efbc6d8d60c9bb31","_npmUser":{"name":"ppzreboot","email":"ppzreboot@qq.com"},"repository":{"url":"git+https://github.com/ppzreboot/k-colors.js.git","type":"git"},"_npmVersion":"10.7.0","description":"[demo](https://ppzreboot.github.io/k-colors.js/) | [github](https://github.com/ppzreboot/k-colors.js) | [npm](https://www.npmjs.com/package/k-colors)","directories":{},"_nodeVersion":"22.2.0","dependencies":{"k-means-pp":">=2.2.0"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/k-colors_0.0.3_1726134103631_0.6013817799806511","host":"s3://npm-registry-packages"}},"0.0.4":{"name":"k-colors","version":"0.0.4","keywords":["color","k-means++","k-means","kmeans"],"author":{"name":"ppz"},"license":"Unlicense","_id":"k-colors@0.0.4","maintainers":[{"name":"ppzreboot","email":"ppzreboot@qq.com"}],"homepage":"https://github.com/ppzreboot/k-colors.js#readme","bugs":{"url":"https://github.com/ppzreboot/k-colors.js/issues"},"dist":{"shasum":"8872e07fe6fbcce7e8d9d251c8afe5e831b3de18","tarball":"https://registry.npmjs.org/k-colors/-/k-colors-0.0.4.tgz","fileCount":14,"integrity":"sha512-HiA6FNAOT4NpVp1ZlIVWEDZZZynlBv8rqx/y5LU9qGtR1s3WltIPwcIuKM7oZkpxgagBTAjX69I9r74LMRIeuw==","signatures":[{"sig":"MEUCIAFClBQwhcp8kE4ihXDaU7iV2Io5zYmf2rBrTMdt6ImOAiEAl/ANoYBsIi0+4xIQ0PzwCY6IqvCS+d5+O64sAEaV/wA=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":9049},"main":"kcpp.js","types":"./kcpp.d.ts","gitHead":"74af8d3d8a23badddec197dc19d3e6a6e91536aa","_npmUser":{"name":"ppzreboot","email":"ppzreboot@qq.com"},"repository":{"url":"git+https://github.com/ppzreboot/k-colors.js.git","type":"git"},"_npmVersion":"10.7.0","description":"[demo](https://ppzreboot.github.io/k-colors.js/) | [github](https://github.com/ppzreboot/k-colors.js) | [npm](https://www.npmjs.com/package/k-colors)","directories":{},"_nodeVersion":"22.2.0","dependencies":{"k-means-pp":">=2.2.1"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/k-colors_0.0.4_1726222172111_0.2333682877730805","host":"s3://npm-registry-packages"}},"0.1.0":{"name":"k-colors","version":"0.1.0","keywords":["color","k-means++","k-means","kmeans"],"author":{"name":"ppz"},"license":"Unlicense","_id":"k-colors@0.1.0","maintainers":[{"name":"ppzreboot","email":"ppzreboot@qq.com"}],"homepage":"https://github.com/ppzreboot/k-colors.js#readme","bugs":{"url":"https://github.com/ppzreboot/k-colors.js/issues"},"dist":{"shasum":"0e088ce51ecc0776aef0280e1441995f38f4dad8","tarball":"https://registry.npmjs.org/k-colors/-/k-colors-0.1.0.tgz","fileCount":14,"integrity":"sha512-DvJuAKx6HW9r9tCXlffxBEui126YzGaxE9RDlLKdX0CtwIIUE+xbdvpW9mR++JoxBufHZeJ/SOsV5dj75RrqDw==","signatures":[{"sig":"MEUCIQCKpETR7kAOcNvalhlhiWAvKebd9DiLNtgjZ1bRcPAGNQIgU+/jCZ8ggM+ZocUNFscRaT8/ew0gWXEL2W9UAwvbqQM=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":8574},"main":"kcpp.js","types":"./kcpp.d.ts","gitHead":"1eb454c246d41e7d3af7ddd01b73fe07b6bedbc2","_npmUser":{"name":"ppzreboot","email":"ppzreboot@qq.com"},"repository":{"url":"git+https://github.com/ppzreboot/k-colors.js.git","type":"git"},"_npmVersion":"10.7.0","description":"[demo](https://ppzreboot.github.io/k-colors.js/) | [github](https://github.com/ppzreboot/k-colors.js) | [npm](https://www.npmjs.com/package/k-colors)","directories":{},"_nodeVersion":"22.2.0","dependencies":{"k-means-pp":"^2.3.1"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/k-colors_0.1.0_1726719005447_0.16689686759682565","host":"s3://npm-registry-packages"}},"0.2.0":{"name":"k-colors","version":"0.2.0","author":{"name":"PPz"},"license":"UNLICENSED","_id":"k-colors@0.2.0","maintainers":[{"name":"ppzreboot","email":"ppzreboot@qq.com"}],"homepage":"https://github.com/ppzreboot/k-colors.js#readme","bugs":{"url":"https://github.com/ppzreboot/k-colors.js/issues"},"dist":{"shasum":"499eaf06d0e804d0b21c6300276a1c9eb0b7acd0","tarball":"https://registry.npmjs.org/k-colors/-/k-colors-0.2.0.tgz","fileCount":18,"integrity":"sha512-ixxYZfTJUTFSgHA/i56kGwt0EYCMQ0upSL98CqH8fSj9QCnmOkDbRD9fHxPZ7buddJmROh7rzwGcxfSZ2SCYnA==","signatures":[{"sig":"MEUCIBYKom8mSF0YYDFI75Dhdj4vrehuz4ZPQNqzItv55XESAiEAvb2n7xjAiWGcjsWtDYuPM0oFVSAv4f7/KqG0Z/vzHGE=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":6484},"type":"module","exports":{".":"./mod.js","./worker":"./worker/index.js","./worker/helper":"./worker/helper.js"},"gitHead":"ddd18bd20349133b3f77ad551de63cf751a0a20d","_npmUser":{"name":"ppzreboot","email":"ppzreboot@qq.com"},"repository":{"url":"git+https://github.com/ppzreboot/k-colors.js.git","type":"git"},"_npmVersion":"10.7.0","description":"[demo](https://ppzreboot.github.io/k-colors.js/) | [github](https://github.com/ppzreboot/k-colors.js) | [npm](https://www.npmjs.com/package/k-colors)","directories":{},"_nodeVersion":"22.2.0","dependencies":{"k-means-pp":"^3.1.0"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/k-colors_0.2.0_1740017396362_0.8770865709189841","host":"s3://npm-registry-packages-npm-production"}},"0.3.0":{"name":"k-colors","version":"0.3.0","author":{"name":"PPz"},"type":"module","repository":{"type":"git","url":"git+https://github.com/ppzreboot/k-colors.js.git"},"license":"UNLICENSED","exports":{".":"./mod.js","./worker":"./worker/index.js","./worker/helper":"./worker/helper.js"},"dependencies":{"k-means-pp":"^3.2.0"},"_id":"k-colors@0.3.0","gitHead":"a0679fd53ad60a0d9b317450f5f12d8456d72edb","description":"[demo](https://ppzreboot.github.io/k-colors.js/) | [github](https://github.com/ppzreboot/k-colors.js) | [npm](https://www.npmjs.com/package/k-colors)","bugs":{"url":"https://github.com/ppzreboot/k-colors.js/issues"},"homepage":"https://github.com/ppzreboot/k-colors.js#readme","_nodeVersion":"22.2.0","_npmVersion":"10.7.0","dist":{"integrity":"sha512-UbXTBetj3K4t996ga/NIiR0OdO2C1Kr2AbxYYmymq4im1YAmueBmfvvSBkCEBu3wXnjnIDkoH+RFnKLn4vwpJA==","shasum":"87e46eb6355f6f36d7ac5ee4f7d4f6056f4e26d7","tarball":"https://registry.npmjs.org/k-colors/-/k-colors-0.3.0.tgz","fileCount":18,"unpackedSize":7605,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIFGsadVZkZtizKgZ6TPaah7jMogB1PFzSzvo4btPKIXsAiAkZhMjYOSsVC9FYWbdthFqxG2rzoWSzqhIMzHF0yg3YQ=="}]},"_npmUser":{"name":"ppzreboot","email":"ppzreboot@qq.com"},"directories":{},"maintainers":[{"name":"ppzreboot","email":"ppzreboot@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/k-colors_0.3.0_1740465717164_0.6751263266278293"},"_hasShrinkwrap":false}},"time":{"created":"2024-09-09T13:36:26.217Z","modified":"2025-02-25T06:41:57.521Z","0.0.0":"2024-09-09T13:36:26.564Z","0.0.1":"2024-09-10T03:55:53.733Z","0.0.2":"2024-09-10T04:37:36.276Z","0.0.3":"2024-09-12T09:41:43.774Z","0.0.4":"2024-09-13T10:09:32.243Z","0.1.0":"2024-09-19T04:10:05.628Z","0.2.0":"2025-02-20T02:09:56.568Z","0.3.0":"2025-02-25T06:41:57.344Z"},"bugs":{"url":"https://github.com/ppzreboot/k-colors.js/issues"},"author":{"name":"PPz"},"license":"UNLICENSED","homepage":"https://github.com/ppzreboot/k-colors.js#readme","repository":{"type":"git","url":"git+https://github.com/ppzreboot/k-colors.js.git"},"description":"[demo](https://ppzreboot.github.io/k-colors.js/) | [github](https://github.com/ppzreboot/k-colors.js) | [npm](https://www.npmjs.com/package/k-colors)","maintainers":[{"name":"ppzreboot","email":"ppzreboot@qq.com"}],"readme":"# k-colors.js\n[demo](https://ppzreboot.github.io/k-colors.js/) | [github](https://github.com/ppzreboot/k-colors.js) | [npm](https://www.npmjs.com/package/k-colors)\n\nA JavaScript library for extracting dominant colors from images using the k-means/k-means++ algorithm.\n\nThis lib is based on [k-means-pp.js](https://github.com/ppzreboot/k-means-pp.js).\n\n## Usage\n#### Install\n``` bash\nnpm install k-colors\n```\n\n#### Cases\n\n##### web worker (vite)\n\n``` ts\nimport KC_workder from 'k-colors/worker?worker'\nimport { KC_worker_helper } from 'k-colors/worker/helper'\n\nconst worker = new KC_workder()\nconst kwh = new KC_worker_helper(worker)\nconst colors = [\n  [255, 255, 0, 255],\n  [0, 255, 255, 255],\n  // ...\n]\nconst range = {\n  min: [0, 0, 0, 0],\n  max: [255, 255, 255, 255],\n}\nconst clusters = await kwh(colors, 6, range)\nconsole.log('clustered colors:',\n  clusters.map(c => c.mean)\n)\n```\n\n##### basic\n\n``` ts\nimport { k_color, has_enough_unique_colors } from 'k-colors'\n\nasync function less_color (source: HTMLImageElement, k: number, opts: ImageEncodeOptions): HTMLImageElement {\n  const { width, height } = source\n  const img_data = img_2_img_data(source)\n  const all_colors = img_data_2_colors(img_data)\n  \n  if (!has_enough_unique_colors(all_colors, k))\n    return source\n\n  const clusters = k_colors(all_colors, k)\n  const new_img_data = clusters_2_img_data(clusters, width, height)\n  const blob = await img_data_2_img_blob(new_img_data, opts)\n  const img = new Image()\n  return await new Promise<HTMLImageElement>((res, rej) => {\n    img.onload = () => res(img)\n    img.onerror = rej\n    img.src = URL.createObjectURL(blob)\n  })\n}\n```\n\n### DEV\n+ For simplicity, don't use workspace or other monorepo tool chain. Just `esbuild` and `node:fs`.\n+ Open another vscode window for the demo app.\n\n##### pack for demo\n``` bash\nnpm run build\ncd dist\nnpm pack\ncd ../demo\nnpm install ../dist/k-colors-xxx.tgz\n```\n\n##### publish demo\n\n```bash\ncd demo\nnpm run build\ncd ..\nnpm run publish-demo\n```\n","readmeFilename":"readme.md"}