Skip to content

Commit

Permalink
extract the function that is run for R,G,B arrays for easier modifica…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
zcernigoj committed May 18, 2020
1 parent a372b22 commit 0b690cd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
41 changes: 13 additions & 28 deletions src/mapDataManipulation/mapDataManipulationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,26 @@ export function prepareRgbMappingArrays(): RgbMappingArrays {
// [0,255] to [0,1]: a = 0, b = 255; c = 0, d = 1
// [0,1] to [0,255]: a = 0, b = 1; c = 0, d = 255

function changeValueToInterval(
x: number,
oldMin: number,
oldMax: number,
newMin: number,
newMax: number,
strictLimits: boolean,
): number {
let newX = newMin + ((newMax - newMin) / (oldMax - oldMin)) * (x - oldMin);

if (strictLimits) {
newX = Math.max(newX, newMin);
newX = Math.min(newX, newMax);
}

return newX;
}

export function changeRgbMappingArraysInterval(
rgbMappingArrays: RgbMappingArrays,
oldMin: number,
oldMax: number,
newMin: number,
newMax: number,
strictLimits: boolean = false,
strictlyLimitValuesToInterval: boolean = false,
): RgbMappingArrays {
rgbMappingArrays.red = rgbMappingArrays.red.map(x =>
changeValueToInterval(x, oldMin, oldMax, newMin, newMax, strictLimits),
);
rgbMappingArrays.green = rgbMappingArrays.green.map(x =>
changeValueToInterval(x, oldMin, oldMax, newMin, newMax, strictLimits),
);
rgbMappingArrays.blue = rgbMappingArrays.blue.map(x =>
changeValueToInterval(x, oldMin, oldMax, newMin, newMax, strictLimits),
);
const transformValueToInterval = (x: number): number => {
let newX = newMin + ((newMax - newMin) / (oldMax - oldMin)) * (x - oldMin);
if (strictlyLimitValuesToInterval) {
newX = Math.max(newX, newMin);
newX = Math.min(newX, newMax);
}
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));
return rgbMappingArrays;
}

Expand Down
16 changes: 10 additions & 6 deletions src/mapDataManipulation/predefinedEffectFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ export function gainEffectFunction(
const factor = gain / (maxValue - minValue);
let offset = 0.0;
offset = offset - factor * minValue;
rgbMappingArrays.red = rgbMappingArrays.red.map(x => Math.max(0.0, x * factor + offset));
rgbMappingArrays.green = rgbMappingArrays.green.map(x => Math.max(0.0, x * factor + offset));
rgbMappingArrays.blue = rgbMappingArrays.blue.map(x => Math.max(0.0, x * factor + offset));

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));

return rgbMappingArrays;
}
Expand All @@ -24,10 +26,12 @@ export function gammaEffectFunction(
): RgbMappingArrays {
// change the values according to the algorithm (gamma)
const gamma = predefinedEffects.gamma ? predefinedEffects.gamma : 1.0;

if (gamma != 1.0) {
rgbMappingArrays.red = rgbMappingArrays.red.map(x => Math.pow(x, gamma));
rgbMappingArrays.green = rgbMappingArrays.green.map(x => Math.pow(x, gamma));
rgbMappingArrays.blue = rgbMappingArrays.blue.map(x => Math.pow(x, gamma));
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));
}

return rgbMappingArrays;
Expand Down

0 comments on commit 0b690cd

Please sign in to comment.