From 2229d548b5595c36a5e175413d822f39610a3f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20Schl=C3=BCter?= Date: Sun, 8 Feb 2015 09:58:07 +0100 Subject: [PATCH] Refactor the color module --- static/js/colors.js | 37 ++++++++++++++++++++++++++++--------- static/js/filter.js | 2 +- static/js/main.js | 4 ++-- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/static/js/colors.js b/static/js/colors.js index f3bd76a..f3c5e0a 100644 --- a/static/js/colors.js +++ b/static/js/colors.js @@ -1,18 +1,37 @@ -export default function mixColors (colors) { - var rgb = colors.map(function (color) { +var colorUtils = { + + /** + * Converts a hex string to RGB + * @param {String} color A string in six digit hex format + * @return {Array[Number]} An array of 3 integers which are the red, + * green and blue values + */ + hexToRGB: function toRGB (color) { var r = color.substr(1, 2) , g = color.substr(3, 2) , b = color.substr(5, 2); return [parseInt(r, 16), parseInt(g, 16), parseInt(b, 16)]; - }); + }, - var result = [ 0, 0, 0 ]; - for (var i = 0, l = rgb.length; i < l; i++) { - result[0] += rgb[i][0] / l; - result[1] += rgb[i][1] / l; - result[2] += rgb[i][2] / l; + /** + * Mixes an arbitrary amount of colors linearly + * @param {Array[String]} colors Different colors as hex string + * @return {String} The mixed color as hex string + */ + mix: function mixColors (colors) { + var rgb = colors.map(colorUtils.hexToRGB); + + var result = [ 0, 0, 0 ]; + for (var i = 0, l = rgb.length; i < l; i++) { + result[0] += rgb[i][0] / l; + result[1] += rgb[i][1] / l; + result[2] += rgb[i][2] / l; + } + + return '#' + result[0].toString(16) + result[1].toString(16) + result[2].toString(16); } - return '#' + result[0].toString(16) + result[1].toString(16) + result[2].toString(16); } + +export default colorUtils; diff --git a/static/js/filter.js b/static/js/filter.js index 19e2324..640bea1 100644 --- a/static/js/filter.js +++ b/static/js/filter.js @@ -16,4 +16,4 @@ export default { }); } -} +}; diff --git a/static/js/main.js b/static/js/main.js index 2b200a5..7f104b9 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -1,7 +1,7 @@ 'use strict'; import $ from 'jquery'; import filter from './filter'; -import {mixColors} from './colors'; +import colorUtils from './colors'; // http://www.colourlovers.com/palette/1811244/1001_Stories var colors = [ '#F8B195', '#F67280', '#C06C84', '#6C5B7B', '#355C7D' ]; @@ -73,7 +73,7 @@ function pickColor (incident) { return categories.indexOf(index) !== -1; }); - return incident.categories.length ? mixColors(categoryColors) : colors[4]; + return incident.categories.length ? colorUtils.mix(categoryColors) : colors[4]; } /**