summaryrefslogtreecommitdiffstats
path: root/js
diff options
context:
space:
mode:
authorskjnldsv <fremulon@protonmail.com>2016-04-04 13:02:42 +0200
committerskjnldsv <fremulon@protonmail.com>2016-04-08 08:27:16 +0200
commitabf51cd48d0083414bb4d6aebf8623724b2b3216 (patch)
tree66be5ddeb437252a1e5fdd0be2d73bc5c7596445 /js
parente01603c4f58b5062425d7913882294919418594a (diff)
Updated color filter to use the core function
+ Retrocompatibility
Diffstat (limited to 'js')
-rw-r--r--js/filters/contactColor_filter.js48
1 files changed, 12 insertions, 36 deletions
diff --git a/js/filters/contactColor_filter.js b/js/filters/contactColor_filter.js
index d0f8466c..8c4645b9 100644
--- a/js/filters/contactColor_filter.js
+++ b/js/filters/contactColor_filter.js
@@ -1,41 +1,17 @@
angular.module('contactsApp')
.filter('contactColor', function() {
return function(input) {
- function hslToRgb(h, s, l) {
- var r, g, b;
- if (s === 0) {
- r = g = b = l;
- } else {
- var hue2rgb = function hue2rgb(p, q, t) {
- if(t < 0) t += 1;
- if(t > 1) t -= 1;
- if(t < 1/6) return p + (q - p) * 6 * t;
- if(t < 1/2) return q;
- if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
- return p;
- };
- var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
- var p = 2 * l - q;
- r = hue2rgb(p, q, h + 1/3);
- g = hue2rgb(p, q, h);
- b = hue2rgb(p, q, h - 1/3);
- }
- return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
+ // Check if core has the new color generator
+ if(typeof input.toHsl === 'function') {
+ var hsl = input.toHsl();
+ return 'hsl('+hsl[0]+', '+hsl[1]+'%, '+hsl[2]+'%)';
+ } else {
+ // If not, we use the old one
+ /* global md5 */
+ var hash = md5(input).substring(0, 4),
+ maxRange = parseInt('ffff', 16),
+ hue = parseInt(hash, 16) / maxRange * 256;
+ return 'hsl(' + hue + ', 90%, 65%)';
}
-
- var hash = input.split('-').join('');
- var result = 0;
- var sat = 80;
- var lum = 68;
- for(var i in hash) {
- result += parseInt(hash.charAt(i), 16)/16;
- }
- result = result * 360;
- var rgb = hslToRgb(result, sat, lum);
- var bright = Math.sqrt( 0.299 * Math.pow(rgb[0], 2) + 0.587 * Math.pow(rgb[1], 2) + 0.114 * Math.pow(rgb[2], 2) );
- if (bright >= 200) {
- sat = 60;
- }
- return 'hsl('+result+', '+sat+'%, '+lum+'%)';
};
-});
+}); \ No newline at end of file