A React-Native library which extracts prominent colors from an image. On Android, it wraps the Android Pallete Class. On iOS, it uses code from react-native-color-grabber.
An example app is included.
$ npm install react-native-palette --save
or
yarn add react-native-palette
Use version >= 1.0.0 of this package, which supports autolinking. No further installation steps are required.
Use version 0.4.x of this package and follow linking steps below:
$ react-native link react-native-palette
android/app/src/main/java/[...]/MainApplication.java
import io.palette.RNPalettePackage;
to the imports at the top of the filenew RNPalettePackage()
to the list returned by the getPackages()
methodAppend the following lines to android/settings.gradle
:
include ':react-native-palette'
project(':react-native-palette').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-palette/android')
android/app/build.gradle
:
compile project(':react-native-palette')
node_modules/react-native-palette/ios/RNPalette.xcodeproj
to your Xcode projectgetAllSwatches(options, image, (error, swatches) => {})
An object containing option properties. These properties are valid for iOS only. On Android, this parameter is ignored.
Property | Type | Info |
---|---|---|
threshold |
Float | Determines whether white or black text will be selected to contrast with the selected color. It is the value for L , in the complex formula at the end of this StackOverflow comment. The default value is 0.179. |
quality |
String | One of "low", "medium", or "high". Higher quality extracts more colors, takes more time and consumes more memory. Default is "low". |
alpha |
Float | From 0 to 1. Determine alpha of hex property. Default is 1. This value is multiplied by the alpha returned from the underlying platform-specific code. It does not affect the alpha of the color (rgba), titleTextColor , or bodyTextColor fields. |
A path to an image such as that returned by react-native-image-picker
. For iOS use the origURL
field of the image picker response for images from assets-library://
. For Android use the path
field. Also for IOS, you can use a local URI, such as that returned by RNFS.LibraryDirectoryPath
in the react-native-fs library.
For versions of react-native-image-picker
>= 3.0.0, use response.assets[0].uri.slice(7)
The callback is passed an error parameter and an array of swatches representing the dominant colors in the image. Typically 16 swatches are returned on Android, fewer on iOS.
getNamedSwatches(image, (error, swatches) => {})
Android only.
Same as in getAllSwatches
An object keyed by the qualities of colors defined by the Android Palette
Class.
The keys are the following:
The values are swatches (possibly null
) or with the fields defined below.
Colors include alpha in the react-native
rgba(255,255,255,1.0)
format. Note that on iOS10 devices UIExtendedSRGBColorSpace color values may be greater than 255 or less than 0, but they will render correctly on the device.
Field | Info |
---|---|
color | The main color of the swatch. |
population | The dominance of this swatch in the image. You can sort on this field to find the most dominant swatch. Android: A positive integer. iOS: A floating point number between 0 and 1. |
titleTextColor | A text color which contrasts well with the main swatch color for use in titles. |
bodyTextColor | A text color which contrasts well with the main swatch color for use as body text. |
swatchInfo | A string encapsulating all the above and more. Can be used for debugging. Android: Note that the hex strings are in the format #aarrggbb rather than the react-native format. iOS: the result string returned by the old (react-native-color-grabber ) API. |
hex | Hex code, computed from color, in format #rrggbbaa . |
import {getAllSwatches} from 'react-native-palette';
import ImagePicker from 'react-native-image-picker'
ImagePicker.launchImageLibrary({}, (response) => {
var path = Platform.OS === 'ios' ? response.origURL : response.path;
getAllSwatches({}, path, (error, swatches) => {
if (error) {
console.log(error);
} else {
swatches.sort((a, b) => {
return b.population - a.population;
});
swatches.forEach((swatch) => {
console.log(swatch.swatchInfo);
});
}
});
});
Thanks for your interest in improving react-native-palette
. Please review the following requests before submitting your PR.
prettier
spec for automating it. This goes double for Java/Objective-C changes, since those are foreign languages to me.Thanks again.