Skip to content

Commit 36b85bb

Browse files
Denis-ShamakinDShamakin
andauthored
feat: color function (#2054) (#2152)
Added the ability to specify a function instead of color tables --------- Co-authored-by: DShamakin <[email protected]>
1 parent 807fff4 commit 36b85bb

File tree

7 files changed

+65
-20
lines changed

7 files changed

+65
-20
lines changed

typescript/packages/well-log-viewer/src/WellLogViewer.stories.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,23 @@ function addTemplateTrack(
268268
return templateNew;
269269
}
270270

271+
// Custom function to demonstrate the ability to specify a function instead of color tables
272+
function colorTableFunc(v: number): [number, number, number] {
273+
if (v >= 0 && v < 0.25) return [255, 0.0, 0.0];
274+
else if (v >= 0.25 && v < 0.5) return [182, 182, 0.0];
275+
else if (v >= 0.5 && v < 0.75) return [0.0, 255, 0.0];
276+
else if (v >= 0.75 && v < 1) return [0.0, 182, 182];
277+
else if (v == 1) return [0.0, 0.0, 255];
278+
else return [0, 0, 0];
279+
}
280+
271281
export const Default: StoryObj<typeof StoryTemplate> = {
272282
args: {
273283
id: "Well-Log-Viewer",
274284
horizontal: false,
275285
welllog: require("../../../../example-data/L898MUD.json")[0], // eslint-disable-line
276286
template: require("../../../../example-data/welllog_template_1.json"), // eslint-disable-line
277-
colorTables: colorTables,
287+
colorTables: colorTableFunc,
278288
wellpick: wellpick,
279289
axisTitles: axisTitles,
280290
axisMnemos: axisMnemos,

typescript/packages/well-log-viewer/src/WellLogViewer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ WellLogViewer.propTypes = {
278278
/**
279279
* Prop containing color table data
280280
*/
281-
colorTables: PropTypes.array, //.isRequired,
281+
colorTables: PropTypes.any, //.isRequired,
282282

283283
/**
284284
* Orientation of the track plots on the screen. Default is false

typescript/packages/well-log-viewer/src/components/PlotDialog.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ export function createBooleanItems(): ReactNode[] {
9191

9292
function createColorTableItems(colorTables: ColorTable[]): ReactNode[] {
9393
const nodes: ReactNode[] = [];
94+
95+
if (typeof colorTables === "function") {
96+
nodes.push(<option key="Function"> Function </option>);
97+
return nodes;
98+
}
99+
94100
if (!colorTables) {
95101
console.error(
96102
"colorTables is missed or empty in createColorTableItems()"
@@ -217,7 +223,10 @@ export class PlotPropertiesDialog extends Component<Props, State> {
217223

218224
// for 'gradientfill' plot
219225
colorTable:
220-
this.props.wellLogView.props.colorTables?.[0]?.name,
226+
typeof this.props.wellLogView.props.colorTables ===
227+
"function"
228+
? "Function"
229+
: this.props.wellLogView.props.colorTables?.[0]?.name,
221230
inverseColorTable: undefined,
222231
colorScale: undefined,
223232
inverseColorScale: undefined,
@@ -335,7 +344,7 @@ export class PlotPropertiesDialog extends Component<Props, State> {
335344
];
336345
} else if (type === "gradientfill") {
337346
const colorTables = this.props.wellLogView.props.colorTables;
338-
[
347+
return [
339348
this.createSelectControl(
340349
"colorTable",
341350
"Fill Color table",
@@ -388,7 +397,7 @@ export class PlotPropertiesDialog extends Component<Props, State> {
388397
createScaleItems()
389398
),
390399
]
391-
: []}
400+
: [<FormControl fullWidth key="12" />]}
392401

393402
{this.createSelectControl(
394403
"name",

typescript/packages/well-log-viewer/src/components/WellLogView.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -838,10 +838,14 @@ function fillPlotTemplate(
838838
inverseColor: options.inverseColor || "",
839839
fill: (options1 ? options1.fill : options.fill) || "",
840840
fill2: options2 ? options2.fill : "",
841-
colorTable: options.colorTable ? options.colorTable.name : "",
842-
inverseColorTable: options.inverseColorTable
843-
? options.inverseColorTable.name
844-
: "",
841+
colorTable:
842+
typeof options.colorTable === "function"
843+
? "Function"
844+
: options.colorTable?.name ?? "",
845+
inverseColorTable:
846+
typeof options.inverseColorTable === "function"
847+
? "Function"
848+
: options.inverseColorTable?.name ?? "",
845849
colorScale: options.colorScale,
846850
inverseColorScale: options.inverseColorScale,
847851
};
@@ -2098,7 +2102,7 @@ export function _propTypesWellLogView(): Record<string, unknown> {
20982102
/**
20992103
* Prop containing color table data for discrete well logs
21002104
*/
2101-
colorTables: PropTypes.array, //.isRequired,
2105+
colorTables: PropTypes.any, //.isRequired,
21022106

21032107
/**
21042108
* Well picks data

typescript/packages/well-log-viewer/src/utils/color-table.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,15 @@ export function getExactColor(
7373
get HTML string with interpolated color value in #xxxxxx format
7474
*/
7575
export function getInterpolatedColor(
76-
colorTable: ColorTable,
76+
colorTable: ColorTable | ((v: number) => [number, number, number]),
7777
v: number
7878
): [number, number, number] {
7979
// TODO: Do not compute these 3 constants (cNaN, cBelow, cAbove) every time!
8080

81+
if (typeof colorTable === "function") {
82+
return colorTable(v);
83+
}
84+
8185
const cNaN: [number, number, number] = colorTable.colorNaN
8286
? colorTable.colorNaN
8387
: [255, 255, 255]; // "white"
@@ -115,9 +119,13 @@ export function getInterpolatedColor(
115119
get HTML string with interpolated color value in #xxxxxx format
116120
*/
117121
export function getInterpolatedColorString(
118-
colorTable: ColorTable,
122+
colorTable: ColorTable | ((v: number) => [number, number, number]),
119123
v: number
120124
): string {
125+
if (typeof colorTable === "function") {
126+
return colorToString(colorTable(v), "#ffffff");
127+
}
128+
121129
// TODO: Do not compute these 3 constants (cNaN, cBelow, cAbove) every time!
122130
const cNaN = colorToString(colorTable.colorNaN, "#ffffff"); // "white"
123131

typescript/packages/well-log-viewer/src/utils/gradientfill-plot-legend.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ function createGradient(
2929
.attr("x2", "100%") //since it's a horizontal linear gradient
3030
.attr("y1", "0%")
3131
.attr("y2", "0%");
32-
const colors = colorTable.colors;
3332
if (rLogarithmic !== undefined) {
3433
const yDelta = Math.log(rLogarithmic); // log(max/min)
3534
const d = rLogarithmic - 1;
@@ -44,12 +43,24 @@ function createGradient(
4443
.style("stop-color", c);
4544
}
4645
} else {
47-
for (let i = 0; i < colors.length; i++) {
48-
const color = colors[i];
49-
const c = color4ToString(color);
50-
lg.append("stop")
51-
.attr("offset", color[0] * 100.0 + "%")
52-
.style("stop-color", c);
46+
if (typeof colorTable === "function") {
47+
const nIntervals = 5;
48+
for (let i = 0; i < nIntervals; i++) {
49+
const fraction = i / nIntervals;
50+
const c = getInterpolatedColorString(colorTable, fraction);
51+
lg.append("stop")
52+
.attr("offset", fraction * 100.0 + "%")
53+
.style("stop-color", c);
54+
}
55+
} else {
56+
const colors = colorTable.colors;
57+
for (let i = 0; i < colors.length; i++) {
58+
const color = colors[i];
59+
const c = color4ToString(color);
60+
lg.append("stop")
61+
.attr("offset", color[0] * 100.0 + "%")
62+
.style("stop-color", c);
63+
}
5364
}
5465
}
5566
return id;

typescript/packages/well-log-viewer/src/utils/tracks.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,10 @@ function getColorTable(
370370
return defColorTable;
371371
}
372372
if (id && colorTables) {
373-
const colorTable = colorTables.find((value) => value.name === id);
373+
const colorTable =
374+
typeof colorTables === "function"
375+
? colorTables
376+
: colorTables.find((value) => value.name === id);
374377
if (colorTable) return colorTable;
375378
console.error(
376379
"colorTable id='" + id + "' is not found in getColorTable()"

0 commit comments

Comments
 (0)