├── .github └── FUNDING.yml ├── .gitignore ├── README.md ├── karma.conf.js ├── package-lock.json ├── package.json ├── res ├── input.jpg ├── input2.png ├── output.png ├── output2.png ├── palette.png └── palette2.png ├── src └── sunglass.js └── webpack.config.js /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: http://zhangwenli.com/blog/tip/ 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | node_modules 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sunglass 😎 2 | 3 | Convert image into a given color palette. 4 | 5 | |Input Image|Input Palette|Output Image| 6 | |-|-|-| 7 | |![](./res/input.jpg)|![](./res/palette.png)|![](./res/output.png)| 8 | |![](./res/input2.png)|![](./res/palette2.png)|![](./res/output2.png)| 9 | 10 | This project was originally created for my [personal website](http://zhangwenli.com); 11 | 12 | ### API 13 | 14 | ``` 15 | /** 16 | * Convert image into a given color palette. 17 | * 18 | * @param {Image | HTMLCanvasElement} image input image 19 | * @param {string[]} [palette=['#fff','#999','#555','#222']] 20 | * colors of output image 21 | * @param {string} [colorSpace='rgb'] color space 22 | * @return {HTMLCanvasElement} output image on canvas 23 | */ 24 | sunglass(image[, palette[, colorSpace]]) 25 | ``` 26 | 27 | ### Example 28 | 29 | ```ts 30 | import sunglass from 'sunglass'; 31 | 32 | const img = new Image(); 33 | img.onload = () => { 34 | const palette = ['#d1c4af','#a39990','#363132']; 35 | const outputCanvas = sunglass(img, palette); 36 | console.log(outputCanvas.toDataURL()); // base64 string of output image 37 | }; 38 | img.src = '...'; 39 | ``` 40 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | module.exports = function (config) { 2 | config.set({ 3 | //root path location to resolve paths defined in files and exclude 4 | basePath: '', 5 | //files/patterns to exclude from loaded files 6 | exclude: [], 7 | //files/patterns to load in the browser 8 | files: [ 9 | { pattern: 'test/*.js', watched: true, served: true, included: true } 10 | /*parameters: 11 | watched: if autoWatch is true all files that have set watched to true will be watched for changes 12 | served: should the files be served by Karma's webserver? 13 | included: should the files be included in the browser using