Skip to content

Commit

Permalink
feat: add support for minified version that can be loaded in HTML scr…
Browse files Browse the repository at this point in the history
…ipt tag (#19)

Signed-off-by: Derek Smith <drsmith.phys@gmail.com>
  • Loading branch information
clok committed Jun 3, 2022
1 parent c55a4e1 commit 45a237f
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 13 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Color gradient generator and picker

![image](https://clokwork.net/images/color-select-sample.png)
![image](https://clok.sh/assets/img/color-select-sample.png)

## Why?

Expand Down Expand Up @@ -71,6 +71,13 @@ const pix = picker.genMultiHexArray(
4. Commit with a [Conventional Commit](https://www.conventionalcommits.org/en/v1.0.0/)
5. Open a PR

### Local Demo

1. Clone the repo
2. `npm install`
3. `npm build`
4. `open ./demo.html`

#### Why is it named `halli`?

According to [Google translate](https://www.google.com/search?q=gradient+in+icelandic) "gradient" in English translated to Icelandic results in "halli".
104 changes: 104 additions & 0 deletions demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="./dist/halli.dev.min.js"></script>
</head>
<body>
<canvas id="canvas" width="400" height="400">
Your browser does not support the canvas element.
</canvas>

<canvas id="canvas2" width="400" height="400">
Your browser does not support the canvas element.
</canvas>

<canvas id="canvas3" width="400" height="400">
Your browser does not support the canvas element.
</canvas>

<canvas id="canvas4" width="400" height="400">
Your browser does not support the canvas element.
</canvas>

<script type="text/javascript">
// Initialize ColorPicker object
var picker = new Halli;

// Quick and dirty render of Array colors
function drawBoxes(ctx, colors, str){
var x = 0;
var y = 0;
var j = 0;
for (var i = 0; i < colors.length; i++) {
ctx.fillStyle = `#${colors[i]}`;
ctx.fillRect( x, y, 20, 20 );
x += 20;
if (x + 20 > canvas.width){
j++;
x = 0;
y = j*20;
}
}
ctx.font = "bold 18px monospace";
ctx.textAlign = 'center';
ctx.fillText(str, 200, 40);
};

// Use the hexToRGBA converter to use the createImageData method

// Use the hexToRGBA converter to use the createImageData method
// Allows for fast generation of pixels
function drawPixels(ctx, colors, picker, str){
var x = 0;
var y = 0;
var id = ctx.createImageData( 1, 1 );
var d = id.data;
for (var i = 0; i < colors.length; i++) {
var rgba = Halli.hexToRGBA( colors[i] );
d[0] = rgba.r;
d[1] = rgba.g;
d[2] = rgba.b;
d[3] = rgba.a;
ctx.putImageData( id, x, y );
x++;
if (x > canvas.width){
x = 0;
y++;
}
}
ctx.font = "bold 18px monospace";
ctx.textAlign = 'center';
ctx.fillText(str, 200, 40);
};

// Generate and draw fade between White and Black with 400 steps
var fade = picker.genHexArray(0xFFFFFF, 0x000000, 400);
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
drawBoxes(ctx, fade, "Two Color Fade");

// Generate the Matlab Jet colormap with 80 steps between each color
// then render
var jet = picker.genMultiHexArray([0x0000FF, 0x00FFFF, 0x00FF00, 0xFFFF00, 0xFF0000, 0x000000], 80);
var c2 = document.getElementById("canvas2");
var ctx2 = c2.getContext("2d");
drawBoxes(ctx2, jet, "Matlab Jet");

// Generate the Matlab HSV colormap with 70 steps between each color
// then render
var hsv = picker.genMultiHexArray([0xFF0000, 0xFFFF00, 0x00FF00, 0x00FFFF, 0x0000FF, 0xFF00FF, 0x000000], 70);
var c3 = document.getElementById("canvas3");
var ctx3 = c3.getContext("2d");
drawBoxes(ctx3, hsv, "Matlab HSV");

// Regenerate the Matlab HSV colormap in a higher resolution with
// 27,000 steps between each color.
// Use the createImageData method to quickly render the colormap
// with each entry in the array represneted by a single pixel.
var pix = picker.genMultiHexArray([0xFF0000, 0xFFFF00, 0x00FF00, 0x00FFFF, 0x0000FF, 0xFF00FF, 0x000000], 27000);
var c4 = document.getElementById("canvas4");
var ctx4 = c4.getContext("2d");
drawPixels(ctx4, pix, picker, "Matlab HSV HighRes");
</script>
</body>
</html>
19 changes: 9 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"build": "npm run clean && tsc --declaration && npm run minify",
"build:docs": "typedoc && cp CHANGELOG.md docs/ && cp CONTRIBUTING.md docs/ && cp README.md docs/ && cp LICENSE docs/",
"changelog": "git-chglog -o CHANGELOG.md --next-tag v$(grep '\\\"version\\\":' package.json | grep -v git-chglog | awk -F '\\\"' '{print $4}')",
"clean": "rm -rf dist coverage",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"minify:release": "uglifyjs ./dist/halli.js ./dist/min.js --compress --mangle --wrap halli --output \"./dist/halli.$(git describe --abbrev=0).min.js\"",
"minify": "uglifyjs ./dist/halli.js ./dist/min.js --compress --mangle --wrap halli --output \"./dist/halli.dev.min.js\"",
"prepare": "husky install",
"test": "jest --coverage",
"test:watch": "jest --watch --coverageReporters=\"lcov\" --coverage",
Expand Down Expand Up @@ -41,7 +44,8 @@
"typedoc": "0.22.14",
"typedoc-plugin-markdown": "3.11.14",
"typedoc-plugin-missing-exports": "0.22.6",
"typescript": "4.6.3"
"typescript": "4.6.3",
"uglify-js": "^3.15.5"
},
"jest": {
"clearMocks": true,
Expand All @@ -55,6 +59,9 @@
"collectCoverageFrom": [
"src/**/*.{ts,js,jsx,mjs}"
],
"coveragePathIgnorePatterns": [
"src/min.ts"
],
"setupFiles": [],
"testMatch": [
"<rootDir>/src/**/*.spec.ts"
Expand Down
1 change: 1 addition & 0 deletions release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ h1 "Preparing release of $VERSION"

h2 "Updating distribution"
npm run build
npm run minify:release

h2 "Updating docs"
npm run build:docs
Expand Down
32 changes: 32 additions & 0 deletions src/min.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
;(function () {
// CommonJS module
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = Halli
}
exports.Halli = Halli
}

// Register as an anonymous AMD module
if (typeof define === 'function' && define.amd) {
define([], function () {
return Halli
})
}

// if there is a importsScrips object define chance for worker
// allows worker to use full Halli functionality with seed
if (typeof importScripts !== 'undefined') {
halli = new Halli()
self.Halli = Halli
}

// If there is a window object, that at least has a document property,
// instantiate and define chance on the window
if (typeof window === 'object' && typeof window.document === 'object') {
window.Halli = Halli
window.halli = new Halli()
}
})()

0 comments on commit 45a237f

Please sign in to comment.