Skip to content

Commit

Permalink
feat: add useRealValue option
Browse files Browse the repository at this point in the history
  • Loading branch information
hongfaqiu committed Jan 12, 2024
1 parent 64ef6ef commit 567861a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/TIFFImageryProvider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ interface SingleBandRenderOptions {
*/
colors?: [number, string][] | string[];

/** Determine whether to use the true value range for custom color ranges, defaults to false */
useRealValue?: boolean;

/** defaults to continuous */
type?: 'continuous' | 'discrete';

Expand Down
3 changes: 3 additions & 0 deletions packages/TIFFImageryProvider/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ interface SingleBandRenderOptions {
* [[0, 'red'], [0.6, 'green'], [1, 'blue']]
*/
colors?: [number, string][] | string[];

/** 控制是否使用真实值生成插值色带,colors参数的stopValue将使用真实值,默认为否 */
useRealValue?: boolean;

/** 默认为连续 */
type?: 'continuous' | 'discrete';
Expand Down
5 changes: 4 additions & 1 deletion packages/TIFFImageryProvider/src/TIFFImageryProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export interface SingleBandRenderOptions {
*/
colors?: [number, string][] | string[];

/** Determine whether to use the true value range for custom color ranges */
useRealValue?: boolean;

/** defaults to continuous */
type?: 'continuous' | 'discrete';

Expand Down Expand Up @@ -386,7 +389,7 @@ export class TIFFImageryProvider {
const { expression, colors, colorScaleImage } = single;
this.plot.setExpression(expression);
if (colors) {
const colorScale = generateColorScale(colors)
const colorScale = generateColorScale(colors, single?.useRealValue ? [band.min, band.max] : [0, 1])
addColorScale('temp', colorScale.colors, colorScale.positions);
this.plot.setColorScale('temp' as any);
} else if (!colorScaleImage) {
Expand Down
5 changes: 3 additions & 2 deletions packages/TIFFImageryProvider/src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ export function getRange(bands: Record<number, {
return { min, max, range };
}

export function generateColorScale(colors: [number, string][] | string[]) {
export function generateColorScale(colors: [number, string][] | string[], minMax: number[]) {
let stops: [number, string][];

if (typeof colors[0] === 'string') {
stops = (colors as string[]).map((color, index) => [index / colors.length, color])
} else {
stops = (colors as [number, string][])
const [min, max] = minMax;
stops = (colors as [number, string][]).map(item => [((item[0] - min) / (max - min)), item[1]])
}

stops.sort((a, b) => a[0] - b[0]);
Expand Down

0 comments on commit 567861a

Please sign in to comment.