Options
All
  • Public
  • Public/Protected
  • All
Menu

The main entry point or namespace for Coloris. This object is callable and can be used to initialize Coloris. It also contains several utility methods.

Index

Type Aliases

ColorFormat: "hex" | "rgb" | "hsl" | "auto" | "mixed"

Color format used by the color picker. The format affects which value is shown in the input field.

  • hex outputs #RRGGBB or #RRGGBBAA.
  • rgb outputs rgb(R, G, B) or rgba(R, G, B, A).
  • hsl outputs hsl(H, S, L) or hsla(H, S, L, A).
  • auto guesses the format from the active input field. Defaults to hex if it fails.
  • mixed outputs #RRGGBB when alpha is 1; otherwise rgba(R, G, B, A).
OnChangeCallback: ((color: string) => void)

Type declaration

    • (color: string): void
    • A function that is called whenever a new color is picked.

      since

      0.18.0

      Parameters

      • color: string

        The newly selected color, as a CSS color string.

      Returns void

Theme: "default" | "large" | "polaroid" | "pill"

All color themes supported by the color picker. More themes might be added in the future.

ThemeMode: "light" | "dark" | "auto"

All theme modes.

Functions

  • close(revert?: boolean): void
  • The color picker dialog can be closed by clicking anywhere on the page or by pressing the ESC on the keyboard. The later will also revert the color to its original value.

    If you would like to close the dialog programmatically, you can do so by calling this method.

    Parameters

    • Optional revert: boolean

      When true, resets the color to its original value. Defaults to false.

    Returns void

  • init(): void
  • Initializes the Coloris color picker and binds the color picker to all input fields with the data-coloris attribute.

    When the script file is loaded directly in a browser, this method is called automatically. When called in a module environment (e.g. browserify, rollup, or webpack), you need to call this method once before any other calls to any {@link Coloris} methods. This method checks for when the document is ready, so you do not have to call this method inside some document ready block.

    Returns void

  • removeInstance(selector: string): void
  • Removes a virtual instance that was added by setInstance. Note that to remove an instance, the selector must be exactly equal to what was passed to setInstance, it cannot merely be a different selector that happens to match the same elements.

    Parameters

    • selector: string

      CSS selector to remove from the set of virtual instances.

    Returns void

  • Adds a virtual instance with separate options.

    Although there is only one physical instance of the color picker in the document, it is possible to simulate multiple instances, each with its own appearance and behavior, by updating the configuration at runtime, when the color picker is opened.

    Here is an example of how to do it by manually setting configuration options in response to click events:

    // Regular color fields use the default light theme
    document.querySelectorAll('.color-fields').forEach(input => {
    input.addEventListener('click', e => {
    Coloris({
    theme: 'default',
    themeMode: 'light',
    });
    });
    });

    // But the special color fields use the polaroid dark theme
    document.querySelectorAll('.special-color-fields').forEach(input => {
    input.addEventListener('click', e => {
    Coloris({
    theme: 'polaroid',
    themeMode: 'dark',
    });
    });
    });

    This works well and is quite versatile, but it can get a little hard to keep track of each change every "instance" makes and revert them to the default values.

    So as of version 0.15.0, there is a new way to automatically manage virtual instances. This works by assigning configuration overrides to a CSS selector representing one or more color fields.

    example
    // Color fields that have the class "instance1" have a format toggle,
    // no alpha slider, a dark theme and custom swatches
    Coloris.setInstance('.instance1', {
    theme: 'polaroid',
    themeMode: 'dark',
    alpha: false,
    formatToggle: true,
    swatches: [
    '#264653',
    '#2a9d8f',
    '#e9c46a'
    ]
    });

    // Fields matching the class "instance2" show color swatches only
    Coloris.setInstance('.instance2', {
    swatchesOnly: true,
    swatches: [
    '#264653',
    '#2a9d8f',
    '#e9c46a'
    ]
    });
    since

    0.15.0

    Parameters

    • selector: string

      CSS selector for the input fields to which the options should apply.

    • opts: Partial<export=.ColorisVirtualInstanceOptions>

      Options to apply to all color picker input fields matching the given selector.

    Returns void

  • updatePosition(): void
  • Update the color picker's position and the color gradient's offset.

    Returns void

Generated using TypeDoc