From 38203e85ae7093fecb200188317bf95fbd62c5fc Mon Sep 17 00:00:00 2001 From: Ziga Cernigoj Date: Mon, 18 May 2020 21:52:15 +0200 Subject: [PATCH] separate function for changing all 3 (R,G,B) mapping arrays --- .../mapDataManipulationUtils.ts | 16 ++++++++++++---- .../predefinedEffectFunctions.ts | 15 +++++---------- .../runPredefinedEffectFunctions.ts | 9 ++++++--- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/mapDataManipulation/mapDataManipulationUtils.ts b/src/mapDataManipulation/mapDataManipulationUtils.ts index b41c110c..5a0161a0 100644 --- a/src/mapDataManipulation/mapDataManipulationUtils.ts +++ b/src/mapDataManipulation/mapDataManipulationUtils.ts @@ -8,6 +8,16 @@ export function prepareRgbMappingArrays(): RgbMappingArrays { }; } +export function changeRgbMappingArraysWithFunction( + rgbMappingArrays: RgbMappingArrays, + transfrormationFunction: Function, +) { + rgbMappingArrays.red = rgbMappingArrays.red.map(x => transfrormationFunction(x)); + rgbMappingArrays.green = rgbMappingArrays.green.map(x => transfrormationFunction(x)); + rgbMappingArrays.blue = rgbMappingArrays.blue.map(x => transfrormationFunction(x)); + return rgbMappingArrays; +} + // from one interval to another // f(x) = c + ((d - c) / (b - a)) * (x - a) // a = oldMin, b = oldMax; c = newMin, d = newMax @@ -31,13 +41,11 @@ export function changeRgbMappingArraysInterval( return newX; }; - rgbMappingArrays.red = rgbMappingArrays.red.map(x => transformValueToInterval(x)); - rgbMappingArrays.green = rgbMappingArrays.green.map(x => transformValueToInterval(x)); - rgbMappingArrays.blue = rgbMappingArrays.blue.map(x => transformValueToInterval(x)); + rgbMappingArrays = changeRgbMappingArraysWithFunction(rgbMappingArrays, transformValueToInterval); return rgbMappingArrays; } -export function prepareManipulatePixel(rgbMappingArrays: RgbMappingArrays): any { +export function prepareManipulatePixel(rgbMappingArrays: RgbMappingArrays): Function { return function(r: number, g: number, b: number, a: number): object { return { r: rgbMappingArrays.red[r], g: rgbMappingArrays.green[g], b: rgbMappingArrays.blue[b], a }; }; diff --git a/src/mapDataManipulation/predefinedEffectFunctions.ts b/src/mapDataManipulation/predefinedEffectFunctions.ts index b11ddedb..b238c7a8 100644 --- a/src/mapDataManipulation/predefinedEffectFunctions.ts +++ b/src/mapDataManipulation/predefinedEffectFunctions.ts @@ -1,6 +1,7 @@ import { PredefinedEffects, RgbMappingArrays } from 'src/mapDataManipulation/const'; +import { changeRgbMappingArraysWithFunction } from 'src/mapDataManipulation/mapDataManipulationUtils'; -export function gainEffectFunction( +export function runGainEffectFunction( rgbMappingArrays: RgbMappingArrays, predefinedEffects: PredefinedEffects, ): RgbMappingArrays { @@ -13,14 +14,11 @@ export function gainEffectFunction( offset = offset - factor * minValue; const transformValueWithGain = (x: number): number => Math.max(0.0, x * factor + offset); - rgbMappingArrays.red = rgbMappingArrays.red.map(x => transformValueWithGain(x)); - rgbMappingArrays.green = rgbMappingArrays.green.map(x => transformValueWithGain(x)); - rgbMappingArrays.blue = rgbMappingArrays.blue.map(x => transformValueWithGain(x)); - + rgbMappingArrays = changeRgbMappingArraysWithFunction(rgbMappingArrays, transformValueWithGain); return rgbMappingArrays; } -export function gammaEffectFunction( +export function runGammaEffectFunction( rgbMappingArrays: RgbMappingArrays, predefinedEffects: PredefinedEffects, ): RgbMappingArrays { @@ -29,10 +27,7 @@ export function gammaEffectFunction( if (gamma != 1.0) { const transformValueWithGamma = (x: number): number => Math.pow(x, gamma); - rgbMappingArrays.red = rgbMappingArrays.red.map(x => transformValueWithGamma(x)); - rgbMappingArrays.green = rgbMappingArrays.green.map(x => transformValueWithGamma(x)); - rgbMappingArrays.blue = rgbMappingArrays.blue.map(x => transformValueWithGamma(x)); + rgbMappingArrays = changeRgbMappingArraysWithFunction(rgbMappingArrays, transformValueWithGamma); } - return rgbMappingArrays; } diff --git a/src/mapDataManipulation/runPredefinedEffectFunctions.ts b/src/mapDataManipulation/runPredefinedEffectFunctions.ts index 85da2912..bac227dd 100644 --- a/src/mapDataManipulation/runPredefinedEffectFunctions.ts +++ b/src/mapDataManipulation/runPredefinedEffectFunctions.ts @@ -5,7 +5,10 @@ import { changeRgbMappingArraysInterval, prepareManipulatePixel, } from 'src/mapDataManipulation/mapDataManipulationUtils'; -import { gainEffectFunction, gammaEffectFunction } from 'src/mapDataManipulation/predefinedEffectFunctions'; +import { + runGainEffectFunction, + runGammaEffectFunction, +} from 'src/mapDataManipulation/predefinedEffectFunctions'; // The algorithm works with numbers between 0 and 1, so we must: // - change the interval of the values from [0, 255] to [0, 1] @@ -22,10 +25,10 @@ export async function runPredefinedEffectFunctions( rgbMappingArrays = changeRgbMappingArraysInterval(rgbMappingArrays, 0, 255, 0, 1); // change the values according to the algorithm (gain) - rgbMappingArrays = gainEffectFunction(rgbMappingArrays, predefinedEffects); + rgbMappingArrays = runGainEffectFunction(rgbMappingArrays, predefinedEffects); // change the values according to the algorithm (gamma) - rgbMappingArrays = gammaEffectFunction(rgbMappingArrays, predefinedEffects); + rgbMappingArrays = runGammaEffectFunction(rgbMappingArrays, predefinedEffects); // change the interval of the values from [0, 1] back to [0, 255], strictly limit values to the interval const limitValuesToInterval = true;