x4js gallery

ColorInput

A color field: a swatch and its text (rgb or hex), with a ColorPicker a click away.

import { ColorInput } from "x4js"

Picker and swatches

A click on the swatch opens a ColorPicker; swatches are the colors offered under its sliders.

export function picker( ) {
	return new ColorInput( { width: 260, color: "#2e7d32", format: "hex", picker: true, swatches: [
		"#c62828", "#ef6c00", "#f9a825", "#2e7d32", "#1565c0", "#6a1b9a",
	] } );
}

Formats

The field writes the color in rgb (the default) or in hex; a color that is not opaque gets its alpha.

export function formats( ) {
	return new VBox( { content: [
		new ColorInput( { width: 260, color: "#1565c0", format: "rgb" } ),
		new ColorInput( { width: 260, color: "#1565c0", format: "hex" } ),
		new ColorInput( { width: 260, color: "rgba(21, 101, 192, 0.5)", format: "hex" } ),
	] } );
}

No color

nullable: the field may be emptied, it then has no color; placeholder is the text of the empty field.

export function noColor( ) {
	const label = new Label( { text: "" } );

	return new HBox( { content: [
		new ColorInput( { width: 260, color: null, nullable: true, picker: true, placeholder: "no color", change: ( ev ) => {
			label.setText( `value: ${ev.value}` );
		} } ),
		label,
	] } );
}

Change and setColor

change gives the color the user typed or picked ("#rrggbb"); setColor writes one, without a change.

export function changeColor( ) {
	const label = new Label( { text: "" } );
	const input = new ColorInput( { width: 260, color: "#ef6c00", format: "hex", alpha: false, picker: true, change: ( ev ) => {
		label.setText( `change: ${ev.value}` );
	} } );

	return new VBox( { content: [
		input,
		new HBox( { content: [
			new Button( { label: "Red", cls: "outline", click: ( ) => input.setColor( "#c62828" ) } ),
			new Button( { label: "Blue", cls: "outline", click: ( ) => input.setColor( "#1565c0" ) } ),
			new Button( { label: "getValue", click: ( ) => label.setText( `getValue: ${input.getValue( )}` ) } ),
			label,
		] } ),
	] } );
}

For an assistant: llms.txt, llms-full.txt.