Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ npm i invert-color --save
```js
const invert = require('invert-color');
```
or
```js
import invert from 'invert-color';
```


### `invert(color[, bw])`

Expand Down
91 changes: 91 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(['module'], factory);
} else if (typeof exports !== "undefined") {
factory(module);
} else {
var mod = {
exports: {}
};
factory(mod);
global.index = mod.exports;
}
})(this, function (module) {
'use strict';

var BW_THRESHOLD = Math.sqrt(1.05 * 0.05) - 0.05;
var RE_HEX = /^(?:[0-9a-f]{3}){1,2}$/i;
var DEFAULT_BW_COLORS = {
black: '#000000',
white: '#ffffff'
};

function padz(str, len) {
len = len || 2;
return (new Array(len).join('0') + str).slice(-len);
}

function toObj(c) {
return { r: c[0], g: c[1], b: c[2] };
}

function hexToRGB(hex) {
if (hex.slice(0, 1) === '#') hex = hex.slice(1);
if (!RE_HEX.test(hex)) throw new Error('Invalid HEX color.');
// normalize / convert 3-chars hex to 6-chars.
if (hex.length === 3) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
}
return [parseInt(hex.slice(0, 2), 16), // r
parseInt(hex.slice(2, 4), 16), // g
parseInt(hex.slice(4, 6), 16) // b
];
}

// c = String (hex) | Array [r, g, b] | Object {r, g, b}
function toRGB(c) {
if (Array.isArray(c)) return c;
return typeof c === 'string' ? hexToRGB(c) : [c.r, c.g, c.b];
}

// http://stackoverflow.com/a/3943023/112731
function getLuminance(c) {
var i = void 0,
x = void 0;
var a = []; // so we don't mutate
for (i = 0; i < c.length; i++) {
x = c[i] / 255;
a[i] = x <= 0.03928 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4);
}
return 0.2126 * a[0] + 0.7152 * a[1] + 0.0722 * a[2];
}

function invertToBW(color, bw, asArr) {
var colors = bw === true ? DEFAULT_BW_COLORS : Object.assign({}, DEFAULT_BW_COLORS, bw);
return getLuminance(color) > BW_THRESHOLD ? asArr ? hexToRGB(colors.black) : colors.black : asArr ? hexToRGB(colors.white) : colors.white;
}

function invert(color, bw) {
color = toRGB(color);
if (bw) return invertToBW(color, bw);
return '#' + color.map(function (c) {
return padz((255 - c).toString(16));
}).join('');
}

invert.asRgbArray = function (color, bw) {
color = toRGB(color);
return bw ? invertToBW(color, bw, true) : color.map(function (c) {
return 255 - c;
});
};

invert.asRgbObject = function (color, bw) {
color = toRGB(color);
return toObj(bw ? invertToBW(color, bw, true) : color.map(function (c) {
return 255 - c;
}));
};

module.exports = invert;
});
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
"name": "invert-color",
"version": "1.1.0",
"description": "Generates inverted (opposite) version of the given color.",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"test": "node test"
"test": "node test",
"build": "babel src --presets es2015 --plugins transform-es2015-modules-umd --out-dir dist",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way in DOM, it's exposed as window.index, not window.invert.

"watch": "babel -w src --presets es2015 --plugins transform-es2015-modules-umd --out-dir dist"
},
"repository": {
"type": "git",
Expand All @@ -30,6 +32,9 @@
"homepage": "https://github.com/onury/invert-color#readme",
"devDependencies": {
"jasmine": "^2.7.0",
"jasmine-console-reporter": "^2.0.1"
"jasmine-console-reporter": "^2.0.1",
"babel-cli": "^6.26.0",
"babel-plugin-transform-es2015-modules-umd": "^6.24.1",
"babel-preset-es2015": "^6.24.1"
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion test/unit.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const invert = require('../index');
const invert = require('../src/index');

/**
* Test Suite
Expand Down