Nutrient Web SDK

Class Color

Color objects are used in annotations for defining colors. We're using an rgb representation internally with the r, g, b values clipped between 0 and 255 inclusive, and a transparent flag that can be used to indicate that the color is transparent, in which case the provided r, g, and b values are ignored and set to 0 in the instantiated Color.

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 TRANSPARENT value should be used instead.

The difference between using Color.TRANSPARENT and null as values for annotation color properties may depend on the context; if the annotation is being created or updated:

  • If an annotation with a non-transparent color value is updated to have a color value of Color.TRANSPARENT, the color value will be updated and be transparent.
  • But if that same annotation is updated to have a color value of 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.TRANSPARENT instead of null when updating annotations.

Class

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

ReadonlyImmutableRecordColor

Constructors

  • Parameters

    color{ b?: number; g?: number; r?: number; transparent?: boolean }

Properties

b

Readonly
number

The blue value of the color.

Default Value

0

g

Readonly
number

The green value of the color.

Default Value

0

r

Readonly
number

The red value of the color.

Default Value

0

transparent

Readonly
boolean

Transparency of the color.

Default Value

false

BLACK

Static

Simple black (CSS: rgb(0, 0, 0))

BLUE

Static

Blue (CSS: rgb(34, 147, 251))

DARK_BLUE

Static

Blue (CSS: rgb(36, 131, 199))

DARK_GREY

Static

Dark grey (CSS: rgb(64, 64, 64))

GREEN

Static

Green (CSS: rgb(110, 176, 0))

GREY

Static

Grey (CSS: rgb(128, 128, 128))

Light blue (CSS: rgb(141, 184, 255))

Light green (CSS: rgb(162, 250, 123))

Light grey (CSS: rgb(192, 192, 192))

Light orange (CSS: rgb(255, 139, 94))

LIGHT_RED

Static

Light red (CSS: rgb(247, 141, 138))

Light yellow (CSS: rgb(252, 238, 124))

MAUVE

Static

Mauve (CSS: rgb(245, 135, 255))

ORANGE

Static

Orange (CSS: rgb(243, 149, 0))

PINK

Static

Pink (CSS: rgb(255, 114, 147))

PURPLE

Static

Purple (CSS: rgb(255, 0, 255))

RED

Static

Red (CSS: rgb(248, 36, 0))

Transparent (CSS: transparent)

WHITE

Static

Simple white (CSS: rgb(255, 255, 255))

YELLOW

Static

Yellow (CSS: rgb(255, 255, 0))

Methods

  • Returns a darker version of the current Color.

    Parameters

    percentnumber

    The 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

    percentnumber

    The 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

    percentnumber

    The percentage of saturation between 0 and 100.

    Returns

    A Color with the new values.

    Example

    const color = NutrientViewer.Color.RED.saturate(50);
    
  • Parameters

    RGBComponentnumber

    Returns

    number
  • Converts the color to a CSS value (e.g. rgb(255, 0, 0)).

    Returns

    string

    A CSS color value in rgb format.

    Example

    NutrientViewer.Color.RED.toCSSValue(); // => 'rgb(248, 36, 0)'
    
  • Converts the color to a Hex value (e.g. #000000).

    Returns

    string

    A CSS color value in hex format.

    Example

    NutrientViewer.Color.RED.toHex(); // => '#f82400'
    

fromHex

Static
  • Converts a hex color value to a Color instance.

    Parameters

    hexColorstring

    The hex color value to convert.

    Returns

    A Color instance.

Immutable Record API

27
  • 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

    this

    A new record holding only default values.

  • Returns a record with key reset 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

    this

    A new record, or this one when the value already equals the default.

  • Returns a record with the value at keyPath removed. 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

    this

    A new record, or this one when nothing was removed.

  • Returns 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

    boolean

    True if equal, false otherwise.

    Example

    const color = NutrientViewer.Color.RED.equals({ r: 248, g: 36, b: 0 });
    
  • Returns the value stored under key.

    Type Parameters

    Kextends keyof { b: number; g: number; r: number; transparent: boolean }
    N = never

    Parameters

    keyK & ("r" | "g" | "b" | "transparent")

    One of the keys this record was defined with.

    notSetValueN
    Optional

    Returned when the record does not define key.

    Returns

    N | { b: number; g: number; r: number; transparent: boolean }[K]

    The current value for that key.

  • 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

    unknown

    The value at that path, or undefined when 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.

    Type Parameters

    N

    Parameters

    keyPathIterable<unknown>

    The keys to follow, outermost first.

    notSetValueN

    Returned when the path does not resolve.

    Returns

    unknown

    The value at that path, or notSetValue.

  • Reports whether key is one of the keys this record was defined with.

    A literal key must name a field; a key typed as string is narrowed to the field names.

    Parameters

    keystring

    The key to look for.

    Returns

    key is "r" | "g" | "b" | "transparent"

    true when the record defines that key, even if its value is null.

  • Computes a hash of this color, the same way Immutable.js does. Transparent colors all hash alike, as equals() treats them as equal whatever their r, g and b values.

    Returns

    number

    An integer hash.

  • Reports whether keyPath resolves, walking nested records and collections.

    Parameters

    keyPathIterable<unknown>

    The keys to follow, outermost first.

    Returns

    boolean

    true when every key along the path is present.

  • Reports whether other is a record of the same class holding equal values. Only the two records' own classes are compared; nested records compare as equals() does.

    Parameters

    otherunknown

    The value to compare against.

    Returns

    boolean

    true when both are the same class and every value matches.

  • Returns a record with the entries of each collection applied in order.

    Keys the record was not defined with are ignored.

    Parameters

    ...collections(
    | Partial<{ b: number; g: number; r: number; transparent: boolean }>
    | Iterable<readonly [string, unknown]>
    )[]

    Plain objects or key/value iterables to apply.

    Returns

    this

    A new record, or this one when no value changed.

  • Returns 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.

    Parameters

    ...collections(
    | Partial<{ r: number; g: number; b: number; transparent: boolean }>
    | Iterable<readonly [string, unknown]>
    )[]

    Plain objects or key/value iterables to merge in.

    Returns

    this

    A new record, or this one when no value changed.

  • 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

    this

    A new record, or this one when the value is unchanged.

  • Returns a record with the entries of each collection merged in deeply, calling merger where 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) => unknown

    Receives the current value, the incoming value and the key.

    ...collections(
    | Partial<{ r: number; g: number; b: number; transparent: boolean }>
    | Iterable<readonly [string, unknown]>
    )[]

    Plain objects or key/value iterables to merge in.

    Returns

    this

    A 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

    this

    A new record, or this one when the value is unchanged.

  • Returns a record with the entries of each collection merged in, calling merger for 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.

    ...collections(
    | Partial<{ b: number; g: number; r: number; transparent: boolean }>
    | Iterable<readonly [string, unknown]>
    )[]

    Plain objects or key/value iterables to merge in.

    Returns

    this

    A new record, or this one when no value changed.

  • An alias for delete.

    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

    this

    A new record, or this one when the value already equals the default.

  • An alias for deleteIn.

    Parameters

    keyPathIterable<unknown>

    The keys to follow, outermost first.

    Returns

    this

    A new record, or this one when nothing was removed.

  • Returns a record with key set to value.

    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

    keyK & ("r" | "g" | "b" | "transparent")

    One of the keys this record was defined with.

    value{ b: number; g: number; r: number; transparent: boolean }[K]

    The value to store.

    Returns

    this

    A new record, or this one when the value is unchanged or the key is not defined.

  • Returns a record with value written at keyPath.

    Parameters

    keyPathIterable<unknown>

    The keys to follow, outermost first.

    valueunknown

    The value to store.

    Returns

    this

    A 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

      0
    • g: number

      The green value of the color.

      Default Value

      0
    • b: number

      The blue value of the color.

      Default Value

      0
    • transparent: 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

      0
    • g: number

      The green value of the color.

      Default Value

      0
    • r: number

      The red value of the color.

      Default Value

      0
    • transparent: 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

      0
    • g: number

      The green value of the color.

      Default Value

      0
    • r: number

      The red value of the color.

      Default Value

      0
    • transparent: boolean

      Transparency of the color.

      Default Value

      false
  • Returns a record with key replaced by the result of applying updater to its current value.

    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.

    updater(
    value: { b: number; g: number; r: number; transparent: boolean }[K]
    ) => { b: number; g: number; r: number; transparent: boolean }[K]

    Receives the current value and returns the new one.

    Returns

    this

    A new record, or this one when the value is unchanged.

  • Returns a record with the value at keyPath replaced by the result of updater.

    Type Parameters

    N

    Parameters

    keyPathIterable<unknown>

    The keys to follow, outermost first.

    notSetValueN
    updater(value: unknown) => unknown

    Receives the current value and returns the new one.

    Returns

    this

    A new record, or this one when the value is unchanged.

  • Returns a record with the value at keyPath replaced by the result of updater.

    Parameters

    keyPathIterable<unknown>

    The keys to follow, outermost first.

    updater(value: unknown) => unknown

    Receives the current value and returns the new one.

    Returns

    this

    A 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 mutator may be written to with set; it is frozen again once mutator returns.

    Parameters

    mutator(mutable: this) => unknown

    Receives a temporarily writable copy of this record.

    Returns

    this

    A new record holding the applied changes, or this one when nothing changed.