From bca8d83f024b481a6d97a652e74d596718f4fc77 Mon Sep 17 00:00:00 2001 From: "John Molakvoæ (skjnldsv)" Date: Mon, 26 Feb 2018 11:59:34 +0100 Subject: New algorithm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ (skjnldsv) --- core/js/placeholder.js | 100 ++++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 46 deletions(-) (limited to 'core') diff --git a/core/js/placeholder.js b/core/js/placeholder.js index 29f91b6698d..0240fd4045d 100644 --- a/core/js/placeholder.js +++ b/core/js/placeholder.js @@ -61,8 +61,7 @@ (function ($) { - String.prototype.toHsl = function() { - + String.prototype.toRgb = function() { var hash = this.toLowerCase().replace(/[^0-9a-f]+/g, ''); // Already a md5 hash? @@ -70,65 +69,74 @@ hash = md5(hash); } - function rgbToHsl(r, g, b) { - r /= 255; g /= 255; b /= 255; - var max = Math.max(r, g, b), min = Math.min(r, g, b); - var h, s, l = (max + min) / 2; - if(max === min) { - h = s = 0; // achromatic - } else { - var d = max - min; - s = l > 0.5 ? d / (2 - max - min) : d / (max + min); - switch(max) { - case r: h = (g - b) / d + (g < b ? 6 : 0); break; - case g: h = (b - r) / d + 2; break; - case b: h = (r - g) / d + 4; break; - } - h /= 6; - } - return [h, s, l]; + function Color(r,g,b) { + this.r = r; + this.g = g; + this.b = b; } - // Init vars - var result = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; - var rgb = [0, 0, 0]; - var sat = 70; - var lum = 68; - var modulo = 16; - - // Splitting evenly the string - for(var i in hash) { - result[i%modulo] = result[i%modulo] + parseInt(hash.charAt(i), 16).toString(); + function stepCalc(steps, ends) { + var step = new Array(3); + step[0] = (ends[1].r - ends[0].r) / steps; + step[1] = (ends[1].g - ends[0].g) / steps; + step[2] = (ends[1].b - ends[0].b) / steps; + return step; } - // Converting our data into a usable rgb format - // Start at 1 because 16%3=1 but 15%3=0 and makes the repartition even - for(var count=1;count= 200) { - sat = 60; + // Convert a string to an integer evenly + function hashToInt(hash, maximum) { + var final = 0; + var result = Array(); + + // Splitting evenly the string + for (var i in hash) { + // chars in md5 goes up to f, hex:16 + result.push(parseInt(hash.charAt(i), 16) % 16); + } + // Adds up all results + for (var i in result) { + final += result[i]; + } + // chars in md5 goes up to f, hex:16 + return parseInt(final % maximum); } - return [parseInt(hsl[0] * 360), sat, lum]; + + return finalPalette[hashToInt(hash, steps * 3 )]; }; $.fn.imageplaceholder = function(seed, text, size) { text = text || seed; // Compute the hash - var hsl = seed.toHsl(); - this.css('background-color', 'hsl('+hsl[0]+', '+hsl[1]+'%, '+hsl[2]+'%)'); + var rgb = seed.toRgb(); + this.css('background-color', 'rgb('+rgb.r+', '+rgb.g+', '+rgb.b+')'); // Placeholders are square var height = this.height() || size || 32; -- cgit v1.2.3 From 275b9868b21c3b9b8ccc209c7c35a347ebf4ec29 Mon Sep 17 00:00:00 2001 From: "John Molakvoæ (skjnldsv)" Date: Tue, 27 Feb 2018 15:01:25 +0100 Subject: Fixed documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ (skjnldsv) --- core/js/placeholder.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'core') diff --git a/core/js/placeholder.js b/core/js/placeholder.js index 0240fd4045d..a73881344e3 100644 --- a/core/js/placeholder.js +++ b/core/js/placeholder.js @@ -2,7 +2,7 @@ * ownCloud * * @author John Molakvoæ - * @copyright 2016-2017 John Molakvoæ + * @copyright 2016-2018 John Molakvoæ * @author Morris Jobke * @copyright 2013 Morris Jobke * @@ -36,7 +36,7 @@ * * Which will result in: * - *
T
+ *
T
* * You may also call it like this, to have a different background, than the seed: * @@ -44,18 +44,18 @@ * * Resulting in: * - *
A
+ *
A
* */ /* - * Alternatively, you can use the prototype function to convert your string to hsl colors: + * Alternatively, you can use the prototype function to convert your string to rgb colors: * - * "a6741a86aded5611a8e46ce16f2ad646".toHsl() + * "a6741a86aded5611a8e46ce16f2ad646".toRgb() * - * Will return the hsl parameters within an array: + * Will return the rgb parameters within the following object: * - * [290, 60, 68] + * Color {r: 208, g: 158, b: 109} * */ -- cgit v1.2.3