reanimated-color-picker
repository·main·Indexed 19 days ago
https://github.com/alabsi91/reanimated-color-pickerA highly customizable, pure JavaScript color picker library for React Native supporting iOS, Android, Expo, and Web. It features support for RTL layouts and integrates with react-native-reanimated and react-native-gesture-handler for high-performance updates via worklets. The library provides a central <ColorPicker /> wrapper and various components like <Panel1 />, <HueSlider />, and <OpacitySlider /> to create flexible color selection interfaces.
What's inside reanimated-color-picker
- Reanimated Color Picker is a highly customizable, pure JavaScript color picker designed specifically for React Native. It is built to work across multiple platforms and environments, ensuring a consistent experience for color selection in your applications.
What is colorKit?
maincolorKitis a collection of color utility tools used internally by theColorPicker. It provides capabilities for color conversion, format identification, and extracting specific color channel information (like RGB or HSL values).How the `<ColorPicker />` component works
mainThe
ColorPickercomponent acts as the central manager for all built-in color picker components (likeHueSlider,OpacitySlider,Swatches, etc.).Crucial Requirement: All built-in components must be wrapped within a
<ColorPicker>component to function correctly. You can nest these components inside standard layout components like<View>to create custom layouts.<ColorPicker> <Preview /> <View> <Panel1 /> <HueSlider vertical /> </View> <View> <Text>Opacity</Text> <OpacitySlider /> </View> <Swatches /> </ColorPicker>Supported Platforms and Features
mainReanimated Color Picker provides broad compatibility and support for modern mobile and web development needs:
- Platforms: Supports iOS, Android, Expo, and Web.
- Layouts: Supports right-to-left (RTL) layouts.
- Implementation: Pure JavaScript implementation for React Native.
Transform thumb position with `<ExtraThumb />`
mainYou can control where an
<ExtraThumb />appears within the panel using several transformation props. These allow you to map specific color channels to the thumb's position.Channel-specific transforms
Use these props to transform a single channel. They accept negative values or percentage strings (e.g.,
'50%'or50):hueTransform: Transforms the hue channel.saturationTransform: Transforms the saturation channel.brightnessTransform: Transforms the brightness channel.
Full color transformation
For complex logic, use
colorTransform. This is a worklet function that receives anHSVObjectand must return a newHSVObject. This returned object determines the thumb's position inside the panel.<ExtraThumb thumbShape="circle" colorTransform={color => { "worklet"; // Example using a hypothetical colorKit utility return colorKit.runOnUI().spin(color, 180).hsv().object(); }} />Supported color formats in colorKit
maincolorKitaccepts a wide variety of color formats for its input, including:- RGB/RGBA: Strings like
"rgb(255, 0, 255)","rgba(255, 0, 255, 1.0)", or objects{r: number, g: number, b: number, a?: number}. - HEX: Strings like
"#f0f","#ff00ff","#f0ff","#ff00ff00", or integer representations like0xff00ffff. - HSL/HSLA: Strings like
"hsl(360, 100%, 100%)"or objects{h: number, s: number, l: number, a?: number}. - HSV/HSVA: Strings like
"hsv(360, 100%, 100%)"or objects{h: number, s: number, v: number, a?: number}. - HWB/HWBA: Strings like
"hwb(360, 100%, 100%)"or objects{h: number, w: number, b: number, a?: number}. - Color Keywords: Named CSS colors (e.g.,
"aliceblue","aqua","red").
- RGB/RGBA: Strings like
Install reanimated-color-picker
mainTo install
reanimated-color-picker, you must first ensure that the following peer dependencies are installed in your project:react-native-gesture-handler(version2.0.0or higher)react-native-reanimated(version2.0.0or higher)
If you are using the Expo managed workflow, you must use Expo version
44or higher.Once the prerequisites are met, install the package using npm:
npm i reanimated-color-pickerRun the Expo example project
mainTo run this specific example project, install the dependencies and start the Expo development server. You can then open the app in a development build, an Android emulator, an iOS simulator, or Expo Go.
npm install npx expo startUse the ColorPicker component and its built-in components
mainThe
ColorPickercomponent acts as a container for various built-in UI components that you can add, remove, rearrange, or style.Key Components:
Preview: Displays the currently selected color.Panel1: A color selection panel.HueSlider: A slider for adjusting the hue.OpacitySlider: A slider for adjusting opacity.Swatches: A collection of color swatches.
Important Note on Callbacks: When defining callback functions like
onCompleteoronChange, useonCompleteJSandonChangeJSif you need to call non-worklet functions (standard JavaScript functions). If you are using a worklet, you can useonCompleteoronChangedirectly.import ColorPicker, { Panel1, Swatches, Preview, OpacitySlider, HueSlider } from "reanimated-color-picker"; // ... inside your component <ColorPicker style={{ width: "70%" }} value="red" onComplete={({ hex }) => { "worklet"; console.log(hex); }} > <Preview /> <Panel1 /> <HueSlider /> <OpacitySlider /> <Swatches /> </ColorPicker>Build and run the Android app
mainWith Metro running, open a new terminal window and run the following command to build and launch the app on an Android Emulator or connected device:
# Using npm npm run android # OR using Yarn yarn androidUse the `<ExtraThumb />` component
mainThe
<ExtraThumb />component adds an extra visual indicator (thumb) to thePanel3component. It is used exclusively as a color indicator and does not respond to gestures.Important: This component only works when placed inside a
<Panel3 />component.<Panel3 style={styles.panelStyle} renderCenterLine adaptSpectrum> <ExtraThumb thumbShape="circle" hueTransform={120} /> <ExtraThumb thumbShape="circle" hueTransform={140} /> {/* ... other thumbs */} </Panel3>Prerequisites for reanimated-color-picker
mainBefore using
reanimated-color-picker, verify your environment meets these requirements:Dependency Minimum Version Notes react-native-gesture-handler2.0.0Required react-native-reanimated2.0.0Required Expo44Required for managed workflow New Architecture (Fabric) Support: To use the React Native New Architecture, ensure you are using:
react-native-gesture-handlerversion2.3.0or higher.react-native-reanimatedversion3.0.0or higher.