Register

Color Picker

An Add-on Module for Foundry Virtual Tabletop

Author: Russell Project Source: Project URL Versions 10+ (Verified 11.300) Last Updated 9 months, 4 weeks ago

Color Picker for Foundry VTT

A customisable color picker with a built-in opacity (alpha) slider for use as a library in Foundry VTT.

Color Picker

Licence

This module uses a modified version of the jscolor Color Picker by Jan Odvárko (East Desire) under the terms of the GNU GPL v3 licence.

For info about jscolor Color Picker, see the jscolor website.

How to Use

Register a Module Setting

As with FoundryVTT's ClientSettings.register function, use the ColorPicker.register(module, key, {settingOptions}, {pickerOptions}) function to register a new color picker setting for a module. module is the ID of the module, key is the name of the setting, {settingOptions} is a comma-separated list of options related to the ClientSettings.register function (see Setting Options) and {pickerOptions} is a comma-separated list of options for the picker (see Picker Options).

Setting Options

name

The name of the setting for end users.

Example: name: 'Background Color'

hint

The description of the registered setting and its behavior.

Example: hint: 'Set the background color'

default

The default color value, e.g., '#FF0000FF' for opaque red. If unset, the default is '#FFFFFFFF'.

Example: default: '#FF0000FF'

scope

The scope of the setting:

Example: scope: 'world'

config

Whether to display the setting in the configuration view. If set to false, the color picker will not be available. Default is true.

Example: config: false

Module Setting Examples

ColorPicker.register(
  'my-module',
  'background-color', 
  {
    name: 'Background Color',
    hint: 'Set the background color'
    scope: 'world',
    config: true
  },
  {
    format: 'hexa',
    alphaChannel: true
  }
)

Add an Input

  1. Add the following html to your template file:
    <input type="text" data-color-picker="{pickerOptions}" value="">
    
    • pickerOptions is a list of comma-separated options in the format: option: value. See "Picker Options" for a list of options
    • type does not need to be defined, but Foundry VTT will automatically format the element if it is included.
  2. Add ColorPicker.install() to your script after the template is rendered.

Picker Options

alpha

The initial alpha (opacity) value.

Example: alpha: 0.5

alphaChannel

Whether the alpha (opacity) channel is enabled and the alpha slider is visible. The default is auto.

Example: alphaChannel: true

alphaElement

The DOM element that will be used to edit and display the alpha value (opacity).

backgroundColor

The background color of the color picker (in CSS color notation). Default is 'rgba(255, 255, 255, 1)'.

Example: backgroundColor: 'rgba(218, 216, 204, 1

borderColor

The border color of the color picker (in CSS color notation). Default is 'rgba(187, 187, 187 ,1)'.

Example: borderColor: 'rgba(122, 121, 113, 1)'

borderRadius

The border radius of the color picker (in pixels). Default is 5.

Example: borderRadius: 10

borderWidth

The border width of the color pickeer (in pixels). Default is 1.

Example: borderWidth: 2

buttonColor

The text and border color of the Close button (in CSS notation). Default is 'rgba(0, 0, 0, 1)'

Example: buttonColor: 'rgba(0, 0, 0, 1)'

buttonHeight

The height of the Close button (in pixels). Default is 18.

Example: buttonHeight: 25

closeButton

Whether the Close button is displayed. Default is false.

Example: closeButton: true

closeText

The text of the Close button. Default is Close.

Example: closeText: 'Exit'

controlBorderColor

The border color of the color picker's controls (in CSS color notation). Default is 'rgba(187, 187, 187, 1)'.

Example: controlBorderColor: 'rgba(122, 121, 113, 1)'

controlBorderWidth

The border width of the color picker's controls (in pixels). Default is 1.

Example: controlBorderWidth: 2

crossSize

The size of the crosshair cursor (in pixels). Default is 8.

forceStyle

Whether to overwrite the CSS style of the previewElement using the !important flag. Default is true.

Example: forceStyle: false

format

The format of the displayed color value.

Example: format: 'hexa'

hash

Whether the valueElement should be prefixed with #. Default is true

Example: hash: false

height

The height of the color spectrum area (in pixels). Default is 101.

Example: height: 200

hideOnLeave

Whether to hide the color picker when clicking away from the target element. Default is true.

Example: hideOnleave: false

hideOnPaletteClick

Whether to hide the color picker when clicking the palette. Default is false.

Example: hideOnPaletteClick: true

mode

The layout of the color picker controls. Default is HSV.

Example: mode: 'HVS'

onChange

A callback function called when the color is changed.

onInput

A callback function called repeatedly as the color is changed.

padding

The padding of the color picker (in pixels). Default is 12

Example: padding: 5

palette

The colors to be displayed in the palette (in array or space-separated string notation).

Array notation example: palette: ['#ffe438', '#88dd20', 'rgba(0,154,255,0.6)', 'rgba(187,0,255,0.3)'] String notation example: palette: '#ffe438 #88dd20 rgba(0,154,255,0.6) rgba(187,0,255,0.3)'

paletteCols

The number of columns in the palette. Default is 10.

Example: paletteCols: 5

paletteHeight

The maximum height of a row in the palette (in pixels). Default is 16.

Example: paletteHeight: 10

paletteSetsAlpha

Whether the palette colors will set alpha. Default is auto.

Example: paletteSetsAlpha: true

paletteSpacing

The distance between color samples in the palette. Default is 4.

Example: paletteSpacing: 2

pointerBorderColor

The border color of the pointers inside the color picker's controls (in CSS color notation). Default is 'rgba(255, 255, 255, 1)'

Example: pointerBorderColor: 'rgba(0, 14, 238, 1)'

pointerBorderWidth

The border width of the pointers inside the color picker's controls. Default is 1.

Example: pointerBorderColor: 2

pointerColor

The color of the pointers iside the color picker's controls (in CCS color notation). Default is 'rgba(76,76,76,1)'.

pointerThickness

The thickness of the pointers inside the color picker's controls (in pixels). Default is 2

Example: pointerThickness: 1

position

The position of the color pickeer relative to the target element. Default is: 'bottom'.

Available values:

Example: position: 'right'

previewElement

The DOM element that will contain a preview of the picked color using CSS background image. Default is targetElement.

previewPadding

The space between the color preview image and content of the previewElement (in pixels). Default is 8.

Example: previewPadding: 20

previewPosition

The position of the color preview image in the previewElement. Default is 'left'.

Available values:

Example: previewPosition: 'right'

previewSize

The width of the color preview image in the previewElement (in pixels). Default is 32

Example: previewSize: 50

random

Whether to generate a random initial color. Default is false.

Example: random: false

required

Whether the valueElement must always contain a color value. Default is true.

Example: required: false

shadow

Whether to display a shadow behind the color picker. Default is true.

Example: shadow: false

shadowBlur

The blur radius of the color picker's shadow (in pixels). Default is 15.

Example: shadowBlur: 5

shadowColor

The color of the color picker's shadow (in CSS color notation). Default is 'rgba(0,0,0,0.2)'.

Example: shadowColor: 'rgba(255, 0, 0 , 1)'

showOnClick

Whether to display the color picker on clicking the target element. Default is true.

Example: showOnClick: false

sliderSize

The width of the sliders (in pixels). Default is 16.

Example: sliderSize: 10

smartPosition

Whether to automatically move the position of the color picker when there is not enough space for it at the specified position. Default is true.

Example: smartPosition: false

uppercase

Whether to display the hex color code in uppercase. Default is true.

Example: uppercase: false

value

The initial color value in any support formatted. If not set, the value attribute of the valueElement will be used.

Example: value: '#ffffffff'

valueElement

The DOM element that will contain the color code. Default is the target element.

width

The width of the color spectrum area (in pixels). Default is 181.

Example: width: 200

zIndex

The z-index of the color picker box. Default is 5000.

Example: zIndex: 1

Categories

Available Versions

  1. Version 1.3

    9 months, 4 weeks ago
    Foundry Version 10+ (Verified 11.300) Manifest URL Read Notes
  2. Version 1.2

    Foundry Version 10+ (Verified 10) Manifest URL Read Notes
  3. Version 0.2

    Foundry Version 9 - 9 (Verified 9) Manifest URL Read Notes