Skip to content

Color picker

The ColorPicker component is a powerful UI element designed for seamless color selection. It is ideal for applications such as design tools, image editors, or any interface requiring color customization.

As part of the complex components in Gameface UI, the ColorPicker combines multiple basic components to deliver advanced functionality. It enables users to adjust hue, saturation, brightness, and transparency, while providing a color preview area for visual feedback.

Internally, the component utilizes the following basic components:

  • XYSlider for adjusting brightness and saturation.
  • Slider for controlling hue and alpha (transparency).
  • Segment for toggling the color preview mode between HEX and RGBA.

To use the ColorPicker component, simply import and render it as shown below:

import ColorPicker from '@components/Complex/ColorPicker/ColorPicker';
const App = () => {
return (
<ColorPicker />
);
};
export default App;
Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the color picker’s wrapper element.
classstring""Additional CSS classes to apply to the color picker’s wrapper element.
refColorPickerRef | undefinedundefinedA reference to the component, providing access to its methods and the underlying HTML element.
valuestringrgba(255, 0, 0, 1)Initial value of the color picker. Accepts colors as strings in HEX or RGBA format.
onChange(value: ColorData) => voidundefinedA callback function triggered whenever the color changes. It can be used to retrieve the updated color value.
MethodParametersReturn ValueDescription
valueNone{rgba: string, hex: string}Retrieves the currently selected color value from the color picker into rgba and hex format.
changeColornewColor: stringvoidUpdates the color picker programmatically with a new color. This method accepts color strings in either RGBA or HEX format, which are automatically converted to ColorData internally.
PropertyTypeDescription
hnumberRepresents the hue of the color (0-360).
snumberRepresents the saturation of the color (0-100).
vnumberRepresents the brightness of the color (0-100).
anumberRepresents the alpha (transparency) of the color (0-1).

To simplify color format conversions, utility methods are available for translating between HSVA, RGBA, and HEX. These methods can be imported from @components/Complex/ColorPicker/colorPickerUtils:

import { RGBAOrHEXToHSVA, RGBAToHSVA, parseHSVAColor } from "@components/Complex/ColorPicker/colorPickerUtils";

RGBAOrHEXToHSVA(color: string) => ColorData

Section titled “RGBAOrHEXToHSVA(color: string) => ColorData”

Converts a color string in RGBA or HEX format to ColorData. The method automatically detects the input format and performs the conversion.

import { RGBAOrHEXToHSVA } from "@components/Complex/ColorPicker/colorPickerUtils";
RGBAOrHEXToHSVA('rgba(0, 0, 0, 0.5)'); // Returns ColorData: { h: 0, s: 0, v: 0, a: 0.5 }
RGBAOrHEXToHSVA('#00000080'); // Returns ColorData: { h: 0, s: 0, v: 0, a: 0.5 }

Converts an RGBA color string to ColorData.

import { RGBAToHSVA } from "@components/Complex/ColorPicker/colorPickerUtils";
RGBAToHSVA('rgba(0, 0, 0, 0.5)'); // Returns ColorData: { h: 0, s: 0, v: 0, a: 0.5 }

parseHSVAColor(color: ColorData) => string

Section titled “parseHSVAColor(color: ColorData) => string”

Converts ColorData to a color string in RGBA or HEX format.

import { parseHSVAColor } from "@components/Complex/ColorPicker/colorPickerUtils";
parseHSVAColor({ h: 0, s: 0, v: 0, a: 1 }); // Returns { rgba: 'rgba(0, 0, 0, 1)', hex: '#000000FF' }

To initialize the color picker with a value other than the default rgba(255, 0, 0, 1), use the value prop. This prop accepts color strings in either RGBA or HEX format. Internally, the component converts the provided value to HSVA format for internal processing.

import ColorPicker from '@components/Complex/ColorPicker/ColorPicker';
const App = () => {
return (
<ColorPicker value="rgba(0, 0, 0, 0.5)" />
// Or
<ColorPicker value="#00000080" />
);
};
export default App;

Programmatically changing the color picker value

Section titled “Programmatically changing the color picker value”

To update the color picker value programmatically, use the ref prop to access the component’s instance. The changeColor method allows you to set a new color. The method accepts string values in RGBA or HEX format, which are automatically converted to ColorData internally.

import ColorPicker from '@components/Complex/ColorPicker/ColorPicker';
const App = () => {
let colorPickerRef!: ColorPickerRef;
const changeColor = () => {
if (colorPickerRef) {
// Update the color to rgba(0, 0, 0, 0.5)
colorPickerRef.changeColor('rgba(0, 0, 0, 0.5)');
}
};
return (
<>
<ColorPicker ref={colorPickerRef} />
<button onClick={changeColor}>Change Color</button>
</>
);
};

To access the current value of the color picker, use the ref prop to obtain a reference to the component instance. This reference provides methods for interacting with the color picker.

The value method retrieves the current color in {rgba: string, hex: string} format. If you need the color in HSVA format, use the RGBAOrHEXToHSVA utility method to convert the rgba or hex string to ColorData which represents the color in hue, saturation, value, and alpha components.

import ColorPicker from '@components/Complex/ColorPicker/ColorPicker';
const App = () => {
let colorPickerRef!: ColorPickerRef;
const getColor = () => {
if (colorPickerRef) {
// Retrieve the current color value from the color picker
const color = colorPickerRef.value();
console.log(color); // Logs the color in RGBA and HEX format
// Example output: { rgba: 'rgba(0, 0, 0, 0.5)', hex: '#00000080' }
}
};
return (
<>
<ColorPicker ref={colorPickerRef} />
<button onClick={getColor}>Get Color</button>
</>
);
};

To respond to color changes in the ColorPicker, use the onChange prop. This prop accepts a callback function that receives the updated color value as an argument. The value is provided in ColorData format, which includes hue, saturation, brightness, and alpha components.

Because the provided value of the onChange callback is in ColorData format, you can use the parseHSVAColor utility method to convert it to a string in either RGBA or HEX format if needed.

import ColorPicker, { ColorData } from '@components/Complex/ColorPicker/ColorPicker';
import { parseHSVAColor } from '@components/Complex/ColorPicker/colorPickerUtils';
const App = () => {
const handleColorChange = (color: ColorData) => {
console.log('Color changed:', color);
// Example output: { h: 0, s: 0, v: 0, a: 0.5 }
// Convert to RGBA or HEX string if needed
const rgbaString = parseHSVAColor(color).rgba; // 'rgba(0, 0, 0, 0.5)'
const hexString = parseHSVAColor(color).hex; // '#00000080'
console.log('RGBA:', rgbaString);
console.log('HEX:', hexString);
};
return (
<ColorPicker onChange={handleColorChange} />
);
};

The ColorPicker component can be tailored to fit your application’s design by using the style and class props. These props allow you to apply custom styles and CSS classes to the wrapper element of the color picker.

import ColorPicker from '@components/Complex/ColorPicker/ColorPicker';
const App = () => {
return (
<ColorPicker
style={{ width: '300px', height: '300px' }}
class="custom-color-picker"
/>
);
};
export default App;