Merge pull request #20980 from nextcloud/techdebt/deprecate-torgb-monkey-patch

Deprecate String.prototype.toRgb
This commit is contained in:
Christoph Wurst 2020-05-14 20:32:26 +02:00 committed by GitHub
commit 7957a47e95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 19 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -63,9 +63,9 @@ import md5 from 'blueimp-md5'
* *
*/ */
String.prototype.toRgb = function() { const toRgb = (s) => {
// Normalize hash // Normalize hash
var hash = this.toLowerCase() var hash = s.toLowerCase()
// Already a md5 hash? // Already a md5 hash?
if (hash.match(/^([0-9a-f]{4}-?){8}$/) === null) { if (hash.match(/^([0-9a-f]{4}-?){8}$/) === null) {
@ -89,8 +89,7 @@ String.prototype.toRgb = function() {
} }
function mixPalette(steps, color1, color2) { function mixPalette(steps, color1, color2) {
var count = steps + 1 var palette = []
var palette = new Array()
palette.push(color1) palette.push(color1)
var step = stepCalc(steps, [color1, color2]) var step = stepCalc(steps, [color1, color2])
for (var i = 1; i < steps; i++) { for (var i = 1; i < steps; i++) {
@ -102,23 +101,23 @@ String.prototype.toRgb = function() {
return palette return palette
} }
var red = new Color(182, 70, 157) const red = new Color(182, 70, 157);
var yellow = new Color(221, 203, 85) const yellow = new Color(221, 203, 85);
var blue = new Color(0, 130, 201) // Nextcloud blue const blue = new Color(0, 130, 201); // Nextcloud blue
// Number of steps to go from a color to another // Number of steps to go from a color to another
// 3 colors * 6 will result in 18 generated colors // 3 colors * 6 will result in 18 generated colors
var steps = 6 const steps = 6;
var palette1 = mixPalette(steps, red, yellow) const palette1 = mixPalette(steps, red, yellow);
var palette2 = mixPalette(steps, yellow, blue) const palette2 = mixPalette(steps, yellow, blue);
var palette3 = mixPalette(steps, blue, red) const palette3 = mixPalette(steps, blue, red);
var finalPalette = palette1.concat(palette2).concat(palette3) const finalPalette = palette1.concat(palette2).concat(palette3);
// Convert a string to an integer evenly // Convert a string to an integer evenly
function hashToInt(hash, maximum) { function hashToInt(hash, maximum) {
var finalInt = 0 var finalInt = 0
var result = Array() var result = []
// Splitting evenly the string // Splitting evenly the string
for (var i = 0; i < hash.length; i++) { for (var i = 0; i < hash.length; i++) {
@ -137,11 +136,17 @@ String.prototype.toRgb = function() {
return finalPalette[hashToInt(hash, steps * 3)] return finalPalette[hashToInt(hash, steps * 3)]
} }
String.prototype.toRgb = function() {
console.warn('String.prototype.toRgb is deprecated! It will be removed in Nextcloud 22.')
return toRgb(this)
}
$.fn.imageplaceholder = function(seed, text, size) { $.fn.imageplaceholder = function(seed, text, size) {
text = text || seed text = text || seed
// Compute the hash // Compute the hash
var rgb = seed.toRgb() var rgb = toRgb(seed)
this.css('background-color', 'rgb(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ')') this.css('background-color', 'rgb(' + rgb.r + ', ' + rgb.g + ', ' + rgb.b + ')')
// Placeholders are square // Placeholders are square