Skip to content

Color Selector

The Color Selector is a comprehensive color picking component that allows users to select colors in various formats (HEX, RGB, RGBA, HSL, HSLA, HSV, CMYK). It provides a visual interface with color maps, hue and alpha sliders, and predefined color swatches.

The Color Selector component depends on the following components:

  • Input
  • Radio
  • Farbe (color manipulation library)
<div data-role="color-selector"></div>
<div data-role="color-selector"
data-return-value-type="hex"
data-show-alpha-channel="true"
data-init-color="#3498db"
data-show-values="hex, rgb, rgba, hsl, hsla, hsv, cmyk"
data-user-colors="#FF0000,#00FF00,#0000FF">
</div>
<input type="text" data-role="input" id="color-controller">
<div data-role="color-selector"
data-controller="#color-controller"
data-init-color="#9c27b0">
</div>
<button class="button" onclick="Metro.getPlugin('#myColorSelector', 'color-selector').val('#aa00ff')">Set to Violet</button>
<button class="button" onclick="alert(Metro.getPlugin('#myColorSelector', 'color-selector').val())">Get color</button>
<div id="myColorSelector" data-role="color-selector"></div>
ParameterTypeDefaultDescription
defaultSwatchesString”#FFFFFF,#000000,#FFFB0D,#0532FF,#FF9300,#00F91A,#FF2700,#686868,#EE5464,#D27AEE,#5BA8C4,#E64AA9,#1ba1e2,#6a00ff,#bebebe,#f8f8f8”Comma-separated list of default color swatches
userColorsStringnullComma-separated list of user-defined colors
returnValueTypeString”hex”Format of the returned color value (hex, rgb, rgba, hsl, hsla, hsv, cmyk)
returnAsStringBooleantrueIf true, returns color as a string; if false, returns a color object
showValuesString”hex, rgb, rgba, hsl, hsla, hsv, cmyk”Comma-separated list of color formats to display
showAsStringStringnullComma-separated list of color formats to display as strings
showUserColorsBooleantrueIf true, shows the user colors section
controllerStringnullSelector for an input element to be used as a controller
addUserColorTitleStringnullTitle for the “Add User Color” button
userColorsTitleStringnullTitle for the user colors section
hslModeString”percent”Mode for HSL values display (“percent” or numeric)
showAlphaChannelBooleantrueIf true, shows the alpha channel slider
inputThresholdNumber300Debounce threshold for input changes in milliseconds
initColorStringnullInitial color value
readonlyInputBooleanfalseIf true, makes color value inputs readonly
clsSelectorString""Additional CSS class for the selector element
clsSwatchesString""Additional CSS class for the swatches container
clsSwatchString""Additional CSS class for individual swatches
clsValueString""Additional CSS class for color value elements
clsLabelString""Additional CSS class for labels
clsInputString""Additional CSS class for input elements
clsUserColorButtonString""Additional CSS class for the user color button
clsUserColorsString""Additional CSS class for the user colors container
clsUserColorsTitleString""Additional CSS class for the user colors title
clsUserColorString""Additional CSS class for user color elements
  • val(value) - Gets or sets the current color value.
// Get current color value
const color = Metro.getPlugin('#myColorSelector', 'color-selector').val();
// Set color value
Metro.getPlugin('#myColorSelector', 'color-selector').val('#FF0000');
  • user(colors) - Gets or sets the user colors.
// Get user colors
const userColors = Metro.getPlugin('#myColorSelector', 'color-selector').user();
// Set user colors as string
Metro.getPlugin('#myColorSelector', 'color-selector').user('#FF0000,#00FF00,#0000FF');
// Set user colors as array
Metro.getPlugin('#myColorSelector', 'color-selector').user(['#FF0000', '#00FF00', '#0000FF']);
  • getVal() - Gets the current color value in the format specified by returnValueType.
const color = Metro.getPlugin('#myColorSelector', 'color-selector').getVal();
  • destroy() - Destroy the color selector component and remove it from the DOM.
Metro.getPlugin('#myColorSelector', 'color-selector').destroy();
EventDescription
onSelectColorTriggered when a color is selected
onColorSelectorCreateTriggered when the color selector is created
<div id="myColorSelector" data-role="color-selector" data-on-select-color="handleColorSelect"></div>
<script>
function handleColorSelect(color, primitive) {
console.log("Selected color:", color);
console.log("Color primitives:", primitive);
}
</script>
VariableDefault (Light)Dark ModeDescription
—color-selector-backgroundvar(—default-background)var(—default-background)Background color of the color selector
:root {
--color-selector-background: #f8f8f8;
}
.dark-side {
--color-selector-background: #333333;
}
  • .color-selector - Main container class
  • .color-box - Container for the color picker elements
  • .default-swatches - Container for default color swatches
  • .user-colors - Container for user-defined colors
  • .color-map - Container for the color shade map
  • .hue-map - Container for the hue slider
  • .alpha-map - Container for the alpha slider
  • .color-values-block - Container for color value inputs
  • .no-alpha-channel - Applied when alpha channel is hidden
  • .dragging - Applied when a cursor is being dragged
<div id="colorSelector" data-role="color-selector"
data-return-value-type="hex"
data-show-alpha-channel="true"
data-init-color="#3498db">
</div>
<script>
// Get the selected color
const color = Metro.getPlugin('#colorSelector', 'color-selector').val();
// Set a new color
Metro.getPlugin('#colorSelector', 'color-selector').val('#FF5733');
// Listen for color selection events
$('#colorSelector').on('select-color', function(e, data) {
console.log("Selected color:", data.color);
console.log("Color primitives:", data.primitive);
});
</script>