Example
Create and update a color.
var color = new NutrientViewer.Color({ r: 245, g: 0, b: 0 });
color = color.set("r", 255);
color.r; // => 255
Default Value
{ r: 0, g: 0, b: 0, transparent: false }Hierarchy
Constructors
Parameters
color{ b?: number; g?: number; r?: number; transparent?: boolean }
Properties
Methods
Parameters
colorNutrientViewer.ColorReturns
number
Returns a darker version of the current Color.
Parameters
percentnumberThe percentage of lightness between 0 and 100.
Returns
A Color with the new values.
Example
const color = NutrientViewer.Color.RED.darker(50);
Returns the record's
[key, value]pairs, in the order its fields were defined.Returns
IterableIterator<[string, unknown]>An iterator over the record's fields.
Returns a lighter version of the current Color.
Parameters
percentnumberThe percentage of lightness between 0 and 100.
Returns
A Color with the new values.
Example
const color = NutrientViewer.Color.RED.lighter(50);
Returns
number
Modifies the saturation of the Color and returns a new one.
Parameters
percentnumberThe percentage of saturation between 0 and 100.
Returns
A Color with the new values.
Example
const color = NutrientViewer.Color.RED.saturate(50);
Parameters
RGBComponentnumberReturns
number
Converts the color to a CSS value (e.g.
rgb(255, 0, 0)).Returns
stringA CSS color value in
rgbformat.Example
NutrientViewer.Color.RED.toCSSValue(); // => 'rgb(248, 36, 0)'
Converts the color to a Hex value (e.g.
#000000).Returns
stringA CSS color value in
hexformat.Example
NutrientViewer.Color.RED.toHex(); // => '#f82400'
fromHex
Converts a hex color value to a Color instance.
Parameters
hexColorstringThe hex color value to convert.
Returns
A Color instance.
Immutable Record API
27
- "[iterator]"(): IterableIterator<
[
keyof { b: number; g: number; r: number; transparent: boolean },
number | boolean,
],
>Iterates the
[key, value]pairs, in the order the fields were defined.Returns
IterableIterator<
[
keyof { b: number; g: number; r: number; transparent: boolean },
number | boolean
]
>
Returns a record with every key reset to the default it was defined with.
Returns
thisA new record holding only default values.
- delete<
K extends
keyof { b: number; g: number; r: number; transparent: boolean },
>(
key: K & ("r" | "g" | "b" | "transparent"),
): thisReturns a record with
keyreset to the default it was defined with.Type Parameters
Kextends keyof { b: number; g: number; r: number; transparent: boolean }Parameters
keyK & ("r" | "g" | "b" | "transparent")One of the keys this record was defined with.
Returns
thisA new record, or this one when the value already equals the default.
Returns a record with the value at
keyPathremoved. A field of this record is reset to its default; a key of a nested collection or plain object is deleted.Parameters
keyPathIterable<unknown>The keys to follow, outermost first.
Returns
thisA new record, or this one when nothing was removed.
- equals(
color:
| NutrientViewer.Color
| { b: number; g: number; r: number; transparent: boolean },
): booleanReturns true if the provided color or object and the current Color have the same RGB values.
Parameters
colorNutrientViewer.Color | { b: number; g: number; r: number; transparent: boolean }Color instance or RGB object.
Returns
booleanTrue if equal, false otherwise.
Example
const color = NutrientViewer.Color.RED.equals({ r: 248, g: 36, b: 0 });
- get<
K extends
keyof { b: number; g: number; r: number; transparent: boolean },
N = never,
>(
key: K & ("r" | "g" | "b" | "transparent"),
notSetValue?: N,
): N | { b: number; g: number; r: number; transparent: boolean }[K]Returns the value stored under
key.Type Parameters
Kextends keyof { b: number; g: number; r: number; transparent: boolean }N = neverParameters
Returns the value at
keyPath, walking nested records and collections.A literal key path is checked against the record's fields and gives the value's type; a computed one gives
unknown.Parameters
keyPathIterable<unknown>The keys to follow, outermost first.
Returns
unknownThe value at that path, or
undefinedwhen it does not resolve.Returns the value at
keyPath, walking nested records and collections.A literal key path is checked against the record's fields and gives the value's type; a computed one gives
unknown.
Reports whether
keyis one of the keys this record was defined with.A literal key must name a field; a key typed as
stringis narrowed to the field names.Parameters
keystringThe key to look for.
Returns
key is "r" | "g" | "b" | "transparent"truewhen the record defines that key, even if its value isnull.
Computes a hash of this color, the same way Immutable.js does. Transparent colors all hash alike, as
equals()treats them as equal whatever theirr,gandbvalues.Returns
numberAn integer hash.
Reports whether
keyPathresolves, walking nested records and collections.Parameters
keyPathIterable<unknown>The keys to follow, outermost first.
Returns
booleantruewhen every key along the path is present.
Reports whether
otheris a record of the same class holding equal values. Only the two records' own classes are compared; nested records compare asequals()does.Parameters
otherunknownThe value to compare against.
Returns
booleantruewhen both are the same class and every value matches.
- mergeDeep(
...collections: (
| Partial<{ r: number; g: number; b: number; transparent: boolean }>
| Iterable<readonly [string, unknown]>
)[],
): thisReturns a record with the entries of each collection merged in, recursing into nested collections and plain objects instead of replacing them.
Keys the record was not defined with are ignored.
Returns a record with each collection merged deeply into the value at
keyPath.Parameters
keyPathIterable<unknown>The keys to follow, outermost first.
...collectionsunknown[]Collections or plain objects to merge into the value there.
Returns
thisA new record, or this one when the value is unchanged.
- mergeDeepWith(
merger: (previous: unknown, next: unknown, key: unknown) => unknown,
...collections: (
| Partial<{ r: number; g: number; b: number; transparent: boolean }>
| Iterable<readonly [string, unknown]>
)[],
): thisReturns a record with the entries of each collection merged in deeply, calling
mergerwhere a nested value is not a collection to decide the value that is kept.Keys the record was not defined with are ignored.
Parameters
merger(previous: unknown, next: unknown, key: unknown) => unknownReceives the current value, the incoming value and the key.
Returns
thisA new record, or this one when no value changed.
Returns a record with each collection merged into the value at
keyPath, one level at a time.Parameters
keyPathIterable<unknown>The keys to follow, outermost first.
...collectionsunknown[]Collections or plain objects to merge into the value there.
Returns
thisA new record, or this one when the value is unchanged.
- mergeWith(
merger: <K extends string | number | symbol>(
previous: { b: number; g: number; r: number; transparent: boolean }[K],
next: { b: number; g: number; r: number; transparent: boolean }[K],
key: K,
) => { b: number; g: number; r: number; transparent: boolean }[K],
...collections: (
| Partial<{ b: number; g: number; r: number; transparent: boolean }>
| Iterable<readonly [string, unknown]>
)[],
): thisReturns a record with the entries of each collection merged in, calling
mergerfor each key the record already holds to decide the value that is kept.Keys the record was not defined with are ignored.
Parameters
merger<K extends string | number | symbol>(
previous: { b: number; g: number; r: number; transparent: boolean }[K],
next: { b: number; g: number; r: number; transparent: boolean }[K],
key: K
) => { b: number; g: number; r: number; transparent: boolean }[K]Receives the current value, the incoming value and the key.
Returns
thisA new record, or this one when no value changed.
- set<K extends keyof { b: number; g: number; r: number; transparent: boolean }>(
key: K & ("r" | "g" | "b" | "transparent"),
value: { b: number; g: number; r: number; transparent: boolean }[K],
): thisReturns a record with
keyset tovalue.Setting a key to the value it already holds returns the same instance, so a record can be compared by reference to detect a change.
Type Parameters
Kextends keyof { b: number; g: number; r: number; transparent: boolean }Parameters
Returns
thisA new record, or this one when the value is unchanged or the key is not defined.
Returns a record with
valuewritten atkeyPath.Parameters
keyPathIterable<unknown>The keys to follow, outermost first.
valueunknownThe value to store.
Returns
thisA new record, or this one when the value is unchanged.
Converts this record and everything nested inside it to plain JavaScript values.
Returns
{ r: number; g: number; b: number; transparent: boolean }A plain object holding this record's values, converted all the way down.
r: number
The red value of the color.
Default Value
0g: number
The green value of the color.
Default Value
0b: number
The blue value of the color.
Default Value
0transparent: boolean
Transparency of the color.
Default Value
false
An alias for toObject, called by
JSON.stringify.Returns
{ b: number; g: number; r: number; transparent: boolean }A plain object holding this record's values.
b: number
The blue value of the color.
Default Value
0g: number
The green value of the color.
Default Value
0r: number
The red value of the color.
Default Value
0transparent: boolean
Transparency of the color.
Default Value
false
Converts this record to a plain object, one level deep. Nested records are left as they are.
Returns
{ b: number; g: number; r: number; transparent: boolean }A plain object holding this record's values.
b: number
The blue value of the color.
Default Value
0g: number
The green value of the color.
Default Value
0r: number
The red value of the color.
Default Value
0transparent: boolean
Transparency of the color.
Default Value
false
- update<
K extends
keyof { b: number; g: number; r: number; transparent: boolean },
>(
key: K & ("r" | "g" | "b" | "transparent"),
updater: (
value: { b: number; g: number; r: number; transparent: boolean }[K],
) => { b: number; g: number; r: number; transparent: boolean }[K],
): thisReturns a record with
keyreplaced by the result of applyingupdaterto its current value.Type Parameters
Kextends keyof { b: number; g: number; r: number; transparent: boolean }Parameters
Returns
thisA new record, or this one when the value is unchanged.
- updateIn<N>(
keyPath: Iterable<unknown>,
notSetValue: N,
updater: (value: unknown) => unknown,
): thisReturns a record with the value at
keyPathreplaced by the result ofupdater. Returns a record with the value at
keyPathreplaced by the result ofupdater.Parameters
keyPathIterable<unknown>The keys to follow, outermost first.
updater(value: unknown) => unknownReceives the current value and returns the new one.
Returns
thisA new record, or this one when the value is unchanged.
Applies several changes as one, so only a single new record is allocated.
The record handed to
mutatormay be written to withset; it is frozen again oncemutatorreturns.Parameters
mutator(mutable: this) => unknownReceives a temporarily writable copy of this record.
Returns
thisA new record holding the applied changes, or this one when nothing changed.
Color objects are used in annotations for defining colors. We're using an rgb representation internally with the
r,g,bvalues clipped between0and255inclusive, and atransparentflag that can be used to indicate that the color is transparent, in which case the providedr,g, andbvalues are ignored and set to0in the instantiatedColor.It is an immutable object and thus can be updated using
set(key, value), for example:color.set("r", 255).However, in order to obtain a transparent color the static
TRANSPARENTvalue should be used instead.The difference between using
Color.TRANSPARENTandnullas values for annotation color properties may depend on the context; if the annotation is being created or updated:Color.TRANSPARENT, the color value will be updated and be transparent.null, the color change will not be saved to the document, although it may appear as transparent in the viewer.To avoid inconsistencies, it is recommended to always use
Color.TRANSPARENTinstead ofnullwhen updating annotations.