From 3c52f056021e6842e802ae6ad5e0c102c9767a5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Rebelo?= Date: Fri, 11 Aug 2023 17:44:38 +0100 Subject: [PATCH] eval: white space fraction #56 --- .../src/metrics/WhiteSpaceFraction.mjs | 12 ++-- .../test/unit/WhiteSpaceFraction.test.js | 59 +++++++++++++++++++ src/public/app.js | 13 ++-- 3 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 src/@evoposter/evaluator/test/unit/WhiteSpaceFraction.test.js diff --git a/src/@evoposter/evaluator/src/metrics/WhiteSpaceFraction.mjs b/src/@evoposter/evaluator/src/metrics/WhiteSpaceFraction.mjs index 40c27d8..f8e3364 100644 --- a/src/@evoposter/evaluator/src/metrics/WhiteSpaceFraction.mjs +++ b/src/@evoposter/evaluator/src/metrics/WhiteSpaceFraction.mjs @@ -24,13 +24,11 @@ import {colorDistance, hexToRGB} from "../utils.js"; const OPTIMAL = .5; const MIN_DISTANCE = 10; -export const compute = (img, color, optimal = OPTIMAL) => { - - console.log ("img", img); - console.log ("color", color); - - color = hexToRGB(color); - const amount = percentTypographyColor(img, color, img.pixelDensity()); +export const compute = (img, color, amount = null, optimal = OPTIMAL) => { + if (amount === null) { + color = hexToRGB(color); + amount = percentTypographyColor(img, color, img.pixelDensity()); + } const res = 1-4*Math.pow((amount - optimal), 2); diff --git a/src/@evoposter/evaluator/test/unit/WhiteSpaceFraction.test.js b/src/@evoposter/evaluator/test/unit/WhiteSpaceFraction.test.js new file mode 100644 index 0000000..8e2c50a --- /dev/null +++ b/src/@evoposter/evaluator/test/unit/WhiteSpaceFraction.test.js @@ -0,0 +1,59 @@ +import {whiteSpaceFraction} from '../../src/index.mjs' + +const OPTIMAL = .5; +const TESTING_PARAMS = [ + { + amount: 0.046897163120567374, + res: 0.17879127684724105, + valid: true, + }, + { + amount: 0.5, + res: 1, + valid: true, + }, + { + amount: 0, + res: 0, + valid: true, + }, + { + amount: 0.19705673758865247, + res: 0.6329015190382778, + valid: true, + }, + { + amount: 0.1, + res: 1, + valid: false, + }, + { + amount: 0, + res: 1, + valid: false, + }, + { + amount: 0, + res: 1, + valid: false, + }, + { + amount: 0.21930851063829787, + res: 0.6848491511996379, + valid: true, + }, +]; + +describe(`Testing White Space Fraction metric`, () => { + for (let i in TESTING_PARAMS) { + let params = TESTING_PARAMS[i]; + test(`Test ${i}`, async () => { + const res = whiteSpaceFraction(null, null, params.amount, OPTIMAL); + if (params.valid) { + expect(res).toBe(params.res); + } else { + expect(res).not.toBe(params.res); + } + }); + } +}); \ No newline at end of file diff --git a/src/public/app.js b/src/public/app.js index d142b0e..70f794e 100644 --- a/src/public/app.js +++ b/src/public/app.js @@ -6080,13 +6080,18 @@ const compute$1 = ( const OPTIMAL = .5; const MIN_DISTANCE = 10; -const compute = (img, color, optimal = OPTIMAL) => { - color = hexToRGB(color); - const amount = percentTypographyColor(img, color, img.pixelDensity()); +const compute = (img, color, amount = null, optimal = OPTIMAL) => { + + console.group(); + console.log ("COLOR", color); + color = hexToRGB(color); + amount = amount === null ? percentTypographyColor(img, color, img.pixelDensity()) : amount; const res = 1-4*Math.pow((amount - optimal), 2); - console.log ("amount", amount, "res", res); + console.log ("AMOUNT", amount); + console.log ("RES", res); + console.groupEnd(); return res; };