Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
return arbitrary grayscale colorizer instead of an empty colorizer in…
Browse files Browse the repository at this point in the history
… no-config situation
  • Loading branch information
ChristianBeilschmidt committed Oct 25, 2019
1 parent 73a761a commit 8c1b93b
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/app/colors/colorizer-data.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ export class ColorizerData implements IColorizerData {

static fromDict(dict: IColorizerData): ColorizerData {
if (!dict) { // return some default value in case of empty deserialization
return ColorizerData.empty();
return ColorizerData.grayScaleColorizer({
min: 0,
max: 100,
});
}

return new ColorizerData(dict);
}

Expand Down Expand Up @@ -125,14 +129,14 @@ export class ColorizerData implements IColorizerData {
getBreakpointForValue(value: BreakPointValue, interpolate: boolean = false): ColorBreakpoint | undefined {
// console.log('ColorizerData', 'getBreakpointForValue', '#1', value, interpolate);

if (!value || !this.breakpoints || this.breakpoints.length <= 0 ) {
if (!value || !this.breakpoints || this.breakpoints.length <= 0) {
return undefined;
}

const isGradient = this.type === 'gradient';
const isNumber = typeof value === 'number';
const firstBrkIsNumber = this.getBreakpointAt(0).valueIsNumber(); // TODO: this is prob. not always the correct type.
const lookUpValue = (firstBrkIsNumber && ! isNumber) ? parseFloat(value as string) : value;
const lookUpValue = (firstBrkIsNumber && !isNumber) ? parseFloat(value as string) : value;
const isLookupNumber = typeof lookUpValue === 'number';
// console.log('ColorizerData', 'getBreakpointForValue', '#2', isGradient, isNumber, isLookupNumber, firstBrkIsNumber, lookUpValue);

Expand All @@ -147,21 +151,21 @@ export class ColorizerData implements IColorizerData {
}
}
const brk = this.breakpoints[brk_index];
const validBrk = brk_index >= 0 && ( this.breakpoints.length > 1 || brk.value === lookUpValue);
const validBrk = brk_index >= 0 && (this.breakpoints.length > 1 || brk.value === lookUpValue);
const isLastBrk = brk_index >= this.breakpoints.length - 1;
// console.log('ColorizerData', 'getBreakpointForValue', '#3', brk_index, validBrk, isLastBrk);

if ( !validBrk ) {
if (!validBrk) {
return undefined;
}

if ( !interpolate || isLastBrk || brk.value === lookUpValue || !isGradient ) {
if (!interpolate || isLastBrk || brk.value === lookUpValue || !isGradient) {
return brk;
}

// handling gradients for numbers...
const brk_next = this.breakpoints[brk_index + 1];
if ( typeof lookUpValue === 'number' && typeof brk.value === 'number' && typeof brk_next.value === 'number' ) {
if (typeof lookUpValue === 'number' && typeof brk.value === 'number' && typeof brk_next.value === 'number') {
const diff = lookUpValue - brk.value;
const frac_diff = diff / (brk_next.value - brk.value);
const color = Color.interpolate(brk.rgba, brk_next.rgba, frac_diff);
Expand Down Expand Up @@ -195,7 +199,7 @@ export class ColorizerData implements IColorizerData {
}

for (let i = 0; i < this.breakpoints.length; i++) {
if ( !this.getBreakpointAt(i).equals(other.getBreakpointAt(i)) ) {
if (!this.getBreakpointAt(i).equals(other.getBreakpointAt(i))) {
return false;
}
}
Expand Down

0 comments on commit 8c1b93b

Please sign in to comment.