From fea69cf97c79ef74776e8e5abfb5e33676173262 Mon Sep 17 00:00:00 2001 From: davidmarkclements Date: Mon, 4 Apr 2016 23:49:50 +0100 Subject: [PATCH] submodule issue fix --- .../source/writing-module-code/hsl-to-hex | 1 - .../writing-module-code/hsl-to-hex/index.js | 59 +++++++++++++++++++ .../hsl-to-hex/package.json | 26 ++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) delete mode 160000 1-Writing-Modules/source/writing-module-code/hsl-to-hex create mode 100644 1-Writing-Modules/source/writing-module-code/hsl-to-hex/index.js create mode 100644 1-Writing-Modules/source/writing-module-code/hsl-to-hex/package.json diff --git a/1-Writing-Modules/source/writing-module-code/hsl-to-hex b/1-Writing-Modules/source/writing-module-code/hsl-to-hex deleted file mode 160000 index bc1b5d0..0000000 --- a/1-Writing-Modules/source/writing-module-code/hsl-to-hex +++ /dev/null @@ -1 +0,0 @@ -Subproject commit bc1b5d01130ed2d141b3c65010326f908fd42592 diff --git a/1-Writing-Modules/source/writing-module-code/hsl-to-hex/index.js b/1-Writing-Modules/source/writing-module-code/hsl-to-hex/index.js new file mode 100644 index 0000000..6a623e7 --- /dev/null +++ b/1-Writing-Modules/source/writing-module-code/hsl-to-hex/index.js @@ -0,0 +1,59 @@ +// In our case, there's only one dependency + +var toRgb = require('hsl-to-rgb-for-reals') + +// Typically all dependencies should be declared at the top of the file. + +// Now let's define an API for our module, we're taking hue, saturation and luminosity values and outputting a CSS compatible hex string. +// Hue is in degrees, between 0 and 359. Since degrees a cyclical in nature, we'll support numbers greater than 359 or less than 0 by "spinning" them around until they fall within the 0 to 359 range. +// Saturation and luminosity are both percentages, we'll represent these percentages with whole numbers between 0 and 100. For these numbers we'll need to enforce a maximum and a minimum, anything below 0 will become 0, anything above 100 will become 100. +// Let's write some utility functions to handle this logic: + +function max (val, n) { + return (val > n) ? n : val +} + +function min (val, n) { + return (val < n) ? n : val +} + +function cycle (val) { + // for safety: + val = max(val, 1e7) + val = min(val, -1e7) + // cycle value: + while (val < 0) { val += 360 } + while (val > 359) { val -= 360 } + return val +} + +// Now for the main piece, the `hsl` function: + +function hsl (hue, saturation, luminosity) { + // resolve degrees to 0 - 359 range + hue = cycle(hue) + + // enforce constraints + saturation = min(max(saturation, 100), 0) + luminosity = min(max(luminosity, 100), 0) + + // convert to 0 to 1 range used by hsl-to-rgb-for-reals + saturation /= 100 + luminosity /= 100 + + // let hsl-to-rgb-for-reals do the hard work + var rgb = toRgb(hue, saturation, luminosity) + + // convert each value in the returned RGB array + // to a 2 character hex value, join the array into + // a string, prefixed with a hash + return '#' + rgb + .map(function (n) { + return (256 + n).toString(16).substr(-2) + }) + .join('') +} + +// In order to make our code into a bona fide module we have to export it: + +module.exports = hsl diff --git a/1-Writing-Modules/source/writing-module-code/hsl-to-hex/package.json b/1-Writing-Modules/source/writing-module-code/hsl-to-hex/package.json new file mode 100644 index 0000000..b1d558c --- /dev/null +++ b/1-Writing-Modules/source/writing-module-code/hsl-to-hex/package.json @@ -0,0 +1,26 @@ +{ + "name": "hsl-to-hex", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "test": "npm run lint", + "lint": "standard" + }, + "author": "David Mark Clements", + "license": "MIT", + "repository": { + "type": "git", + "url": "git+ssh://git@github.com/davidmarkclements/hsl-to-hex.git" + }, + "bugs": { + "url": "https://github.com/davidmarkclements/hsl-to-hex/issues" + }, + "homepage": "https://github.com/davidmarkclements/hsl-to-hex#readme", + "description": "", + "dependencies": { + "hsl-to-rgb-for-reals": "^1.1.0" + }, + "devDependencies": { + "standard": "^6.0.8" + } +}