# Class Brush

GoJS® Diagramming Components
version 3.0.5
by Northwoods Software®

A Brush holds color information and describes how to draw the inside of a Shape or the stroke of a shape or a TextBlock or the background of any GraphObject.

A Brush must not be modified once it has been assigned to a GraphObject, such as the Shape.fill or TextBlock.stroke or GraphObject.background. However, a Brush may be shared by multiple GraphObjects.

#####
Index

### Constructors

constructor

### Accessors

color
colorStops
end
endRadius
pattern
start
startRadius
type

### Methods

addColorStop
copy
darkenBy
isDark
lightenBy
darken
darkenBy
isDark
isValidColor
lighten
lightenBy
mix
randomColor

### Properties

HSL
Lab
Linear
Pattern
Radial
Solid

## Constructors

### constructor

- new Brush(type?: string | BrushType, init?: Partial<Partial<Brush> & Record<string | number, string>>): Brush

- Construct a Brush class that holds the given color information.

  #### Parameters

  - ##### `Optional` type: string | BrushType

    Optional, one of the values BrushType.Solid, BrushType.Linear, BrushType.Radial, BrushType.Pattern, or a well-formed CSS string describing a solid color brush. No parameter defaults to a BrushType.Solid with a color description of 'black'.

  - ##### `Optional` init: Partial<Partial<Brush> & Record<string | number, string>>

    Optional initialization properties. In addition to Brush properties, this object can also contain color stops in the format: `{ 0: "#FEC901", 0.2: "#FFFFAA", 1: "#FEA200" }`

  #### Returns Brush

## Accessors

### color
: string

- Gets or sets the color of a solid Brush. The default value is 'black'. The value must be a valid CSS color string.

### colorStops
: Map<number, string>

- Gets or sets a Map holding all of the color stops used in this gradient, where the key is a number, the fractional distance between 0 and 1 (inclusive), and where the corresponding value is a color string.

  Call addColorStop in order to add color stops to this brush. This property value may be null if no gradient stops have been defined.

### end
: Spot

- Gets or sets the ending location for a linear or radial gradient. A Spot value specifies a relative point in the object's GraphObject.naturalBounds. The default value is Spot.BottomCenter for linear gradients and Spot.Center for radial gradients.

### endRadius
: number

- Gets or sets the radius of a radial brush at the end location. The default value is NaN.

### pattern
: HTMLCanvasElement | HTMLImageElement

- Gets or sets the pattern of a brush of type BrushType.Pattern.

### start
: Spot

- Gets or sets the starting location for a linear or radial gradient. A Spot value specifies a relative point in the object's GraphObject.naturalBounds. The default value is Spot.TopCenter for linear gradients and Spot.Center for radial gradients.

### startRadius
: number

- Gets or sets the radius of a radial brush at the start location. The default value is 0.

### type
: BrushType

- Gets or sets the type of brush. The default value is BrushType.Solid. The value must be a BrushType. If the new value is a linear or radial brush type, and if the start or end spots are not specific spots, they are changed to be specific spots, depending on the type of brush.

## Methods

### addColorStop

- addColorStop(loc: number, color: string): Brush

- Specify a particular color at a particular fraction of the distance. If the type is BrushType.Solid, change the type to BrushType.Linear. You should not have duplicate color stop values at the same fractional distance.

  #### Parameters

  - ##### loc: number

    A number between 0 and 1 (inclusive).

  - ##### color: string

    A valid CSS color string.

  #### Returns Brush

  this Brush

### copy

- copy(): Brush

- Create a copy of this Brush with the same values.

  #### Returns Brush

### darkenBy

- darkenBy(fraction?: number, mode?: ColorSpace): Brush

- Modifies all colors within this Brush, darkening them by some fraction.

  #### Parameters

  - ##### `Optional` fraction: number

    Fraction to darken the colors by. Defaults to 0.2, must be between 0 and 1 (inclusive).

  - ##### `Optional` mode: ColorSpace

    Color space to use for adjusting. Must be a ColorSpace value, defaults to ColorSpace.Oklch.

  #### Returns Brush

  This Brush with modified color values.

### isDark

- isDark(): boolean

- This function determines whether this Brush is "dark."

  #### Returns boolean

  `since`

  2.0

### lightenBy

- lightenBy(fraction?: number, mode?: ColorSpace): Brush

- Modifies all colors within this Brush, lightening them by some fraction.

  #### Parameters

  - ##### `Optional` fraction: number

    Fraction to lighten the colors by. Defaults to 0.2, must be between 0 and 1 (inclusive).

  - ##### `Optional` mode: ColorSpace

    Color space to use for adjusting. Must be a ColorSpace value, defaults to ColorSpace.Oklch.

  #### Returns Brush

  This Brush with modified color values.

### `Static` darken

- darken(color: string): string

- This static function takes a color and darkens it by 20% in the Lab color space. This is a convenience function which calls Brush.darkenBy.

  #### Parameters

  - ##### color: string

    A valid CSS color string.

  #### Returns string

  A CSS string for the darkened color in RGBA.

### `Static` darkenBy

- darkenBy(color: string, fraction?: number, mode?: ColorSpace): string

- This static function takes a color and darkens it.

  #### Parameters

  - ##### color: string

    A valid CSS color string

  - ##### `Optional` fraction: number

    Fraction to darken the color by. Defaults to 0.2, must be between 0 and 1 (inclusive).

  - ##### `Optional` mode: ColorSpace

    Color space to use for adjusting. Must be a ColorSpace value, defaults to ColorSpace.Oklch.

  #### Returns string

  A CSS string for the darkened color in RGBA or HSLA.

### `Static` isDark

- isDark(color: BrushLike): boolean

- This static function takes a color and determines whether it is "dark." Does not account for transparency.

  Example usage:

      myDiagram.nodeTemplate =
        $(go.Node, "Auto",
          $(go.Shape, "RoundedRectangle", { strokeWidth: 0 },
            new go.Binding("fill", "color")),
          $(go.TextBlock,
            { margin: 8 },
            // Dark nodes use white text, light nodes use black text
            new go.Binding("stroke", "color", c => go.Brush.isDark(c) ? "white" : "black"),
            new go.Binding("text", "key")
          )
        );

  #### Parameters

  - ##### color: BrushLike

    A valid CSS color string or a Brush.

  #### Returns boolean

  `since`

  2.0

### `Static` isValidColor

- isValidColor(color: string): boolean

- This static function returns true if a given color string is well-formed for drawing.

  #### Parameters

  - ##### color: string

    A color string to validate.

  #### Returns boolean

### `Static` lighten

- lighten(color: string): string

- This static function takes a color and lightens it by 20% in the Lab color space. This is a convenience function which calls Brush.lightenBy.

  #### Parameters

  - ##### color: string

    A valid CSS color string.

  #### Returns string

  A CSS string for the lightened color in RGBA.

### `Static` lightenBy

- lightenBy(color: string, fraction?: number, mode?: ColorSpace): string

- This static function takes a color and lightens it.

  #### Parameters

  - ##### color: string

    A valid CSS color string.

  - ##### `Optional` fraction: number

    Fraction to lighten the colors by. Defaults to 0.2, must be between 0 and 1 (inclusive).

  - ##### `Optional` mode: ColorSpace

    Color space to use for adjusting. Must be a ColorSpace value, defaults to ColorSpace.Oklch.

  #### Returns string

  A CSS string for the lightened color in RGBA or HSLA.

### `Static` mix

- mix(color1: string, color2: string, fraction?: number): string

- This static function takes two colors and mixes them together, using the (optionally) specified amount of the second color.

  #### Parameters

  - ##### color1: string

    A valid CSS color string.

  - ##### color2: string

    Another valid CSS color string to mix.

  - ##### `Optional` fraction: number

    Fraction specifying how much color2 to mix into color1. Defaults to .5, must be between 0 and 1 (inclusive).

  #### Returns string

  `since`

  2.0

### `Static` randomColor

- randomColor(min?: number, max?: number): string

- This static function can be used to generate a random color.

  #### Parameters

  - ##### `Optional` min: number

    A number between 0 and 255, defaults to 128.

  - ##### `Optional` max: number

    A number between 0 and 255, defaults to 255.

  #### Returns string

  A color value in # hexadecimal format.

## Properties

### `Static` `Readonly` HSL

`deprecated`

See ColorSpace.HSL.

### `Static` `Readonly` Lab

`deprecated`

See ColorSpace.Lab.

### `Static` `Readonly` Linear

`deprecated`

See BrushType.Linear.

### `Static` `Readonly` Pattern

`deprecated`

See BrushType.Pattern.

### `Static` `Readonly` Radial

`deprecated`

See BrushType.Radial.

### `Static` `Readonly` Solid

`deprecated`

See BrushType.Solid.

Copyright © 1998-2024 by Northwoods Software Corporation.