From f3c068b83177205cb312fe3f62ee8755d6c5950b Mon Sep 17 00:00:00 2001 From: MohammadMir Date: Tue, 15 Dec 2020 14:20:34 +0330 Subject: [PATCH 1/4] isDarker/Lighter(), isWarm(er), isCool(er), isHarmonious() added. --- tinycolor.js | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/tinycolor.js b/tinycolor.js index 9ce79d8..0fd3d09 100644 --- a/tinycolor.js +++ b/tinycolor.js @@ -55,6 +55,108 @@ tinycolor.prototype = { isLight: function() { return !this.isDark(); }, + /** + * The color with less brightness value is considered as Darker. + * @param {tinycolor} secondColor + */ + isDarker: function (secondColor) { + return this.getBrightness() < secondColor.getBrightness(); + }, + /** + * Return the opposite value of isDarker(). + * @param {tinycolor} secondColor + */ + isLighter: function (secondColor) { + return !this.isDarker(secondColor); + }, + isWarm: function () { + return this._r > this._b; + }, + isCool: function () { + return !this.isWarm(); + }, + /** + * Returns true if the color is warmer i.e. have more red value. + * If both colors have the same red value the one with less green and blue values is considered warmer. + * @param {tinycolor} secondColor + */ + isWarmer: function (secondColor) { + if (this._r > secondColor._r) + return true; + if (this._r == secondColor._r) { + return (this._b + this._g) < (secondColor._b + secondColor._g); + } + return false; + }, + /** + * Return the opposite value of isWarmer(). + * @param {tinycolor} secondColor + */ + isCooler: function (secondColor) { + return !this.isWarmer(secondColor); + }, + /** + * Returns true if colors are harmonious with the selected harmony. + * + * @param {String} harmony + * Should be a value from ['complement', 'analogous', 'monochromatic', 'splitcomplement', 'triad', 'tetrad']. + * @param {tinycolor[]} otherColors + * you have to pass + * 1 color for 'complement', 'monochromatic', + * 2 colors for 'analogous', 'splitcomplement', 'triad', and + * 3 colors for 'tetrad'. + * @src https://www.sessions.edu/color-calculator/ + */ + isHarmonious: function (harmony, ...otherColors) { + var len = otherColors.length; + switch (harmony) { + case 'complement': { + var compColor = this.complement(); + return compColor.toRgbString() == otherColors[0].toRgbString(); + } + case 'analogous': { + var anaList = this.analogous(); + for (const color of anaList) { + if (!(otherColors.includes(color))) { + return false; + } + } + return true; + } + case 'monochromatic': { + var monoList = this.monochromatic(); + for (const color of monoList) { + if (!(otherColors.includes(color))) + return false; + } + return true; + } + case 'splitcomplement': { + var splitList = this.splitcomplement(); + for (const color of splitList) { + if (!(otherColors.includes(color))) + return false; + } + return true; + } + case 'triad': { + var triList = this.triad(); + for (const color of triList) { + if (!(otherColors.includes(color))) + return false; + } + return true; + } + case 'tetrad': { + var tetList = this.tetrad(); + for (const color of tetList) { + if (!(otherColors.includes(color))) + return false; + } + return true; + } + } + }, isValid: function() { return this._ok; }, From 37f11d7a16eb49647a6c843f9c7106c450242a8a Mon Sep 17 00:00:00 2001 From: MohammadMir Date: Sun, 20 Dec 2020 06:26:39 +0330 Subject: [PATCH 2/4] isHarmonious() fixed. --- tinycolor.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/tinycolor.js b/tinycolor.js index 0fd3d09..8966548 100644 --- a/tinycolor.js +++ b/tinycolor.js @@ -101,13 +101,14 @@ tinycolor.prototype = { * @param {String} harmony * Should be a value from ['complement', 'analogous', 'monochromatic', 'splitcomplement', 'triad', 'tetrad']. * @param {tinycolor[]} otherColors + * List of the colors to be checked. * you have to pass * 1 color for 'complement', 'monochromatic', * 2 colors for 'analogous', 'splitcomplement', 'triad', and * 3 colors for 'tetrad'. * @src https://www.sessions.edu/color-calculator/ */ - isHarmonious: function (harmony, ...otherColors) { + isHarmonious: function (harmony, otherColors) { var len = otherColors.length; switch (harmony) { case 'complement': { @@ -116,8 +117,8 @@ tinycolor.prototype = { } case 'analogous': { var anaList = this.analogous(); - for (const color of anaList) { - if (!(otherColors.includes(color))) { + for (let i = 0; i < anaList.length; i++) { + if (!(otherColors.includes(anaList[i]))) { return false; } } @@ -125,32 +126,32 @@ tinycolor.prototype = { } case 'monochromatic': { var monoList = this.monochromatic(); - for (const color of monoList) { - if (!(otherColors.includes(color))) + for (let i = 0; i < monoList.length; i++) { + if (!(otherColors.includes(monoList[i]))) return false; } return true; } case 'splitcomplement': { var splitList = this.splitcomplement(); - for (const color of splitList) { - if (!(otherColors.includes(color))) + for (let i = 0; i < splitList.length; i++) { + if (!(otherColors.includes(splitList[i]))) return false; } return true; } case 'triad': { var triList = this.triad(); - for (const color of triList) { - if (!(otherColors.includes(color))) + for (let i = 0; i < triList.length; i++) { + if (!(otherColors.includes(triList[i]))) return false; } return true; } case 'tetrad': { var tetList = this.tetrad(); - for (const color of tetList) { - if (!(otherColors.includes(color))) + for (let i = 0; i < tetList.length; i++) { + if (!(otherColors.includes(tetList[i]))) return false; } return true; From b8ebfe8551ce3ce4acde303087173e6c2e08a92a Mon Sep 17 00:00:00 2001 From: MohammadMir Date: Sun, 20 Dec 2020 06:52:25 +0330 Subject: [PATCH 3/4] lint fixes. --- tinycolor.js | 50 +++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/tinycolor.js b/tinycolor.js index 8966548..554118b 100644 --- a/tinycolor.js +++ b/tinycolor.js @@ -110,53 +110,61 @@ tinycolor.prototype = { */ isHarmonious: function (harmony, otherColors) { var len = otherColors.length; + var result = false; switch (harmony) { case 'complement': { var compColor = this.complement(); - return compColor.toRgbString() == otherColors[0].toRgbString(); + restult = compColor.toRgbString() == otherColors[0].toRgbString(); + break; } case 'analogous': { var anaList = this.analogous(); - for (let i = 0; i < anaList.length; i++) { - if (!(otherColors.includes(anaList[i]))) { - return false; + for (var aIndex = 0; aIndex < anaList.length; aIndex++) { + if (!(otherColors.includes(anaList[aIndex]))) { + result = false; } } - return true; + result = true; + break; } case 'monochromatic': { var monoList = this.monochromatic(); - for (let i = 0; i < monoList.length; i++) { - if (!(otherColors.includes(monoList[i]))) - return false; + for (var mIndex = 0; mIndex < monoList.length; mIndex++) { + if (!(otherColors.includes(monoList[mIndex]))) + result = false; } - return true; + result = true; + break; } case 'splitcomplement': { var splitList = this.splitcomplement(); - for (let i = 0; i < splitList.length; i++) { - if (!(otherColors.includes(splitList[i]))) - return false; + for (var sIndex = 0; sIndex < splitList.length; sIndex++) { + if (!(otherColors.includes(splitList[sIndex]))) + result = false; } - return true; + result = true; + break; } case 'triad': { var triList = this.triad(); - for (let i = 0; i < triList.length; i++) { - if (!(otherColors.includes(triList[i]))) - return false; + for (var tIndex = 0; tIndex < triList.length; tIndex++) { + if (!(otherColors.includes(triList[tIndex]))) + result = false; } - return true; + result = true; + break; } case 'tetrad': { var tetList = this.tetrad(); - for (let i = 0; i < tetList.length; i++) { - if (!(otherColors.includes(tetList[i]))) - return false; + for (var tetIndex = 0; tetIndex < tetList.length; tetIndex++) { + if (!(otherColors.includes(tetList[tetIndex]))) + result = false; } - return true; + result = true; + break; } } + return result; }, isValid: function() { return this._ok; From 60b2a231362af899222b11d6b9e5ec51a9289db1 Mon Sep 17 00:00:00 2001 From: MohammadMir Date: Sun, 20 Dec 2020 06:59:55 +0330 Subject: [PATCH 4/4] lint fixes. --- tinycolor.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tinycolor.js b/tinycolor.js index 554118b..2594fdd 100644 --- a/tinycolor.js +++ b/tinycolor.js @@ -115,8 +115,8 @@ tinycolor.prototype = { case 'complement': { var compColor = this.complement(); restult = compColor.toRgbString() == otherColors[0].toRgbString(); - break; } + break; case 'analogous': { var anaList = this.analogous(); for (var aIndex = 0; aIndex < anaList.length; aIndex++) { @@ -125,8 +125,8 @@ tinycolor.prototype = { } } result = true; - break; } + break; case 'monochromatic': { var monoList = this.monochromatic(); for (var mIndex = 0; mIndex < monoList.length; mIndex++) { @@ -134,17 +134,17 @@ tinycolor.prototype = { result = false; } result = true; - break; } + break; case 'splitcomplement': { var splitList = this.splitcomplement(); for (var sIndex = 0; sIndex < splitList.length; sIndex++) { if (!(otherColors.includes(splitList[sIndex]))) result = false; } - result = true; - break; + result = true; } + break; case 'triad': { var triList = this.triad(); for (var tIndex = 0; tIndex < triList.length; tIndex++) { @@ -152,8 +152,9 @@ tinycolor.prototype = { result = false; } result = true; - break; + } + break; case 'tetrad': { var tetList = this.tetrad(); for (var tetIndex = 0; tetIndex < tetList.length; tetIndex++) { @@ -161,7 +162,6 @@ tinycolor.prototype = { result = false; } result = true; - break; } } return result;