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 Name | Type | Default | Description |
|---|---|---|---|
style | JSX.CSSProperties | {} | Inline styles to apply directly to the color picker’s wrapper element. |
class | string | "" | Additional CSS classes to apply to the color picker’s wrapper element. |
ref | ColorPickerRef | undefined | undefined | A reference to the component, providing access to its methods and the underlying HTML element. |
value | string | rgba(255, 0, 0, 1) | Initial value of the color picker. Accepts colors as strings in HEX or RGBA format. |
size | 'S' | 'M' | 'L' | 'XL' | 'L' | Sets the size of the color picker. The size affects the overall dimensions and font sizes within the component. Ideal for quick customization. |
onChange | (value: ColorData) => void | undefined | A callback function triggered whenever the color changes. It can be used to retrieve the updated color value. |
Ref API
Section titled “Ref API”To interact with the ColorPicker programmatically, you can use the ColorPickerRef interface. This interface provides properties and methods to access and manipulate the component’s state.
Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
element | HTMLDivElement | A reference to the color picker’s root HTML element, useful for DOM access or styling. |
Methods
Section titled “Methods”| Method | Parameters | Return Value | Description |
|---|---|---|---|
value | None | {rgba: string, hex: string} | Retrieves the currently selected color value from the color picker into rgba and hex format. |
changeColor | newColor: string | void | Updates 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. |
ColorData Type
Section titled “ColorData Type”| Property | Type | Description |
|---|---|---|
h | number | Represents the hue of the color (0-360). |
s | number | Represents the saturation of the color (0-100). |
v | number | Represents the brightness of the color (0-100). |
a | number | Represents the alpha (transparency) of the color (0-1). |
Color Picker Utilities
Section titled “Color Picker Utilities”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 }RGBAToHSVA(rgba: string) => ColorData
Section titled “RGBAToHSVA(rgba: string) => ColorData”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' }Setting an initial value
Section titled “Setting an initial value”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> </> );};Retrieve the color picker value
Section titled “Retrieve the color picker value”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> </> );};Listen to color changes
Section titled “Listen to color changes”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} /> );};Customizing the component
Section titled “Customizing the component”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;