-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding npm scripts for compiling ES5 dist through babel #6
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6a6a937
adding npm scripts for compiling dist through babel
74cd0a4
updating README
81a37b2
updating src path in unit.spec.js
68666fb
altering the build script to output UMD and correct package.json to p…
08fa00e
Update package.json
onury da2465f
Update package.json
onury File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,91 @@ | ||
'use strict'; | ||
(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' | ||
}; | ||
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 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 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]; | ||
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 | ||
]; | ||
} | ||
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]; | ||
} | ||
// 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); | ||
// 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]; | ||
} | ||
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 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(''); | ||
} | ||
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.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; | ||
})); | ||
}; | ||
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; | ||
module.exports = invert; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
, notwindow.invert
.