From 5cbab56509cc3b2a8603ca20e7ee7750b99c9c3e Mon Sep 17 00:00:00 2001 From: SimonDMC Date: Sat, 11 Jun 2022 19:31:30 +0200 Subject: [PATCH 1/2] Switch color comparison space --- index.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 362a5be..4e20264 100644 --- a/index.js +++ b/index.js @@ -178,7 +178,25 @@ function updateSteps() { } - +/** + * https://gist.github.com/ryancat/9972419b2a78f329ce3aebb7f1a09152 + * + * Compare color difference in RGB + * @param {Array} rgb1 First RGB color in array + * @param {Array} rgb2 Second RGB color in array + */ + function deltaRgb (rgb1, rgb2) { + const [ r1, g1, b1 ] = rgb1, + [ r2, g2, b2 ] = rgb2, + drp2 = Math.pow(r1 - r2, 2), + dgp2 = Math.pow(g1 - g2, 2), + dbp2 = Math.pow(b1 - b2, 2), + t = (r1 + r2) / 2 + + let result = (256*3) - Math.sqrt(2 * drp2 + 4 * dgp2 + 3 * dbp2 + t * (drp2 - dbp2) / 256); + console.log(result); + return result +} /* block generation */ @@ -200,14 +218,9 @@ function genBlocks() { for (let blockCount = blockData.length -1; blockCount >= 0; ) { let currentBlock = blockData[blockCount]; - /* count similarity for Red, Green and Blue */ - var calcR = ( 255 - Math.abs(currentStep[0] - currentBlock.rgb[0]) ) / 255, - calcG = ( 255 - Math.abs(currentStep[1] - currentBlock.rgb[1]) ) / 255, - calcB = ( 255 - Math.abs(currentStep[2] - currentBlock.rgb[2]) ) / 255; - /* 0.0 means 'completely opposite colour', 1.0 means 'same colour'; values <0.8 in 99% of cases are junk */ - var currentComparison = [currentBlock.id, (calcR + calcG + calcB) / 3]; + var currentComparison = [currentBlock.id, deltaRgb(currentStep, currentBlock.rgb)]; blockCount -= 1; From c04fd4eecb86fd5ca18a510b9fc84630e0b49dfc Mon Sep 17 00:00:00 2001 From: SimonDMC Date: Sat, 11 Jun 2022 21:13:58 +0200 Subject: [PATCH 2/2] Remove redundant logging --- index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/index.js b/index.js index 4e20264..d199d20 100644 --- a/index.js +++ b/index.js @@ -193,9 +193,7 @@ function updateSteps() { dbp2 = Math.pow(b1 - b2, 2), t = (r1 + r2) / 2 - let result = (256*3) - Math.sqrt(2 * drp2 + 4 * dgp2 + 3 * dbp2 + t * (drp2 - dbp2) / 256); - console.log(result); - return result + return (256*3) - Math.sqrt(2 * drp2 + 4 * dgp2 + 3 * dbp2 + t * (drp2 - dbp2) / 256); }