|
| 1 | +// Copyright (c) 2022, NVIDIA CORPORATION. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import {IndexType, Series, Uint32, Uint32Series} from '@rapidsai/cudf'; |
| 16 | + |
| 17 | +function getSize(col: Series<IndexType>|number) { |
| 18 | + return col instanceof Series ? col.length : undefined; |
| 19 | +} |
| 20 | + |
| 21 | +function validateSeries(col: Series<IndexType>) { |
| 22 | + if (col.min() < 0 || col.max() > 255) { |
| 23 | + throw new RangeError('invalid column, contains values outside of the range [0,255]'); |
| 24 | + } |
| 25 | + return col; |
| 26 | +} |
| 27 | + |
| 28 | +function hexToInteger(value: string) { |
| 29 | + value = value.replace('#', '0x'); |
| 30 | + |
| 31 | + return parseInt(`${value}ff`, 16); |
| 32 | +} |
| 33 | +/** |
| 34 | + * Convert 4 individual RGBA Series or values to a RGBA Integer Series to be consumed directly by |
| 35 | + * color buffers |
| 36 | + * @param r Series<IndexType> or number for the color red (valid values [0,255]) |
| 37 | + * @param g Series<IndexType> or number for the color red (valid values [0,255]) |
| 38 | + * @param b Series<IndexType> or number for the color red (valid values [0,255]) |
| 39 | + * @param a Series<IndexType> or number for the color red (valid values [0,255]) |
| 40 | + * @param size optional size of the series, if all of the r,g,b,a arguments are numbers. default is |
| 41 | + * 1 |
| 42 | + * @returns Uint32Series containing RGBA integer values |
| 43 | + */ |
| 44 | +export function RGBASeriestoIntSeries(r: Series<IndexType>|number, |
| 45 | + g: Series<IndexType>|number, |
| 46 | + b: Series<IndexType>|number, |
| 47 | + a: Series<IndexType>|number, |
| 48 | + size = 1): Uint32Series { |
| 49 | + size = getSize(r) || getSize(g) || getSize(b) || getSize(a) || size; |
| 50 | + r = r instanceof Series ? validateSeries(r) |
| 51 | + : Series.sequence({type: new Uint32, size: size, init: r, step: 0}); |
| 52 | + g = g instanceof Series ? validateSeries(g) |
| 53 | + : Series.sequence({type: new Uint32, size: size, init: g, step: 0}); |
| 54 | + b = b instanceof Series ? validateSeries(b) |
| 55 | + : Series.sequence({type: new Uint32, size: size, init: b, step: 0}); |
| 56 | + a = a instanceof Series ? validateSeries(a) |
| 57 | + : Series.sequence({type: new Uint32, size: size, init: a, step: 0}); |
| 58 | + |
| 59 | + return r.shiftLeft(BigInt(24)) |
| 60 | + .bitwiseOr(g.shiftLeft(BigInt(16))) |
| 61 | + .bitwiseOr(b.shiftLeft(BigInt(8))) |
| 62 | + .bitwiseOr(a) |
| 63 | + .cast(new Uint32); |
| 64 | +} |
| 65 | +/** |
| 66 | + * Convert rgba to 32 bit signed integer |
| 67 | + * @param rgba array of rgba value (a value is optional, default is 255) |
| 68 | + * @returns rgba 32 bit signed integer value |
| 69 | + */ |
| 70 | +export function RGBAtoInt(rgba: number[]): number { |
| 71 | + rgba.forEach((value) => { |
| 72 | + if (value < 0 || value > 255) { throw new RangeError('rgba values expected within [0,255]'); } |
| 73 | + }); |
| 74 | + if (rgba.length < 3 || rgba.length > 4) { |
| 75 | + throw new Error('invalid array length, provide values for r,g,b,a(optional)'); |
| 76 | + } |
| 77 | + if (rgba.length == 3) { rgba = rgba.concat(255); } // if only rgb values are provided |
| 78 | + return (rgba[0] << 24 >>> 0) + (rgba[1] << 16 >>> 0) + (rgba[2] << 8 >>> 0) + rgba[3]; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Helper function for quickly creating a cudf color Series based on color bins provided |
| 83 | + * @param values |
| 84 | + * @param domain |
| 85 | + * @param colors |
| 86 | + * possible input values: |
| 87 | + * - array of rgba arrays |
| 88 | + * - array of rgb arrays (a defaults to 255) |
| 89 | + * - array of 32bit rgba integers |
| 90 | + * - array of hex color strings |
| 91 | + * @param nullColor |
| 92 | + */ |
| 93 | +export function mapValuesToColorSeries( |
| 94 | + values: Series<IndexType>, |
| 95 | + domain: number[], |
| 96 | + colors: number[][]|number[]|string[], |
| 97 | + nullColor: number[]|number = [204, 204, 204, 255]): Uint32Series { |
| 98 | + // validate colors and domain lengths |
| 99 | + if (colors.length < 1 || domain.length < 1) { |
| 100 | + throw new Error('colors and domain must be arrays of length 1 or greater'); |
| 101 | + } |
| 102 | + |
| 103 | + // convert RGBA values to Integers accepted by deck gpu buffers |
| 104 | + const nullColorInteger = Array.isArray(nullColor) ? RGBAtoInt(nullColor) : nullColor; |
| 105 | + const colorsInteger: number[] = |
| 106 | + colors.map(value => Array.isArray(value) ? RGBAtoInt(value) |
| 107 | + : typeof value == 'string' ? hexToInteger(value) |
| 108 | + : value); |
| 109 | + let colorSeries = |
| 110 | + Series.sequence({type: new Uint32, init: colorsInteger[0], step: 0, size: values.length}); |
| 111 | + const colorIndices = Series.sequence({type: values.type, init: 0, step: 1, size: values.length}); |
| 112 | + |
| 113 | + if (domain.length == 1) { |
| 114 | + const boolMask = values.ge(domain[0]); |
| 115 | + const indices = colorIndices.filter(boolMask); |
| 116 | + colorSeries = |
| 117 | + colorSeries.scatter(colorsInteger[1] || colorsInteger[colors.length - 1], indices); |
| 118 | + } else { |
| 119 | + for (let i = 0; i < domain.length - 1; i++) { |
| 120 | + const boolMask = values.ge(domain[i]).logicalAnd(values.lt(domain[i + 1])); |
| 121 | + const indices = colorIndices.filter(boolMask); |
| 122 | + colorSeries = |
| 123 | + colorSeries.scatter(colorsInteger[i] || colorsInteger[colors.length - 1], indices); |
| 124 | + } |
| 125 | + } |
| 126 | + // handle nulls |
| 127 | + if (values.countNonNulls() !== values.length) { // contains null values |
| 128 | + const indices = colorIndices.filter(values.isNull()); |
| 129 | + colorSeries = colorSeries.scatter(nullColorInteger, indices); |
| 130 | + } |
| 131 | + |
| 132 | + return colorSeries; |
| 133 | +} |
0 commit comments