ColorPickerView
šØ Android colorpicker for getting colors from any images by tapping on the desired color.
Install / Use
/learn @skydoves/ColorPickerViewREADME
ColorPicker Compose
If you're looking to implement a color picker in your Compose project, you can use colorpicker-compose instead.
Including in your project
Gradle
Add the dependency below to your module's build.gradle file:
dependencies {
implementation("com.github.skydoves:colorpickerview:2.4.0")
}
<img src="https://user-images.githubusercontent.com/24237865/53681606-38f75000-3d2f-11e9-8586-848d638f23b1.gif" align="right" width="30%">
Table of Contents
1. ColorPickerView
- ColorPickerView in layout
- ColorListener
- Palette
- ActionMode
- Debounce
- Create using builder
- Restore and save state
- Palette from Gallery <br>
2. AlphaSlideBar <br> 3. BrightnessSlideBar<br> 4. ColorPickerDialog <br> 5. FlagView <br> 6. AlphaTileView <br> 7. ColorPickerView Methods <br> 8. Other Libraries <br>
Usage
Add the following XML namespace inside your XML layout file.
xmlns:app="http://schemas.android.com/apk/res-auto"
ColorPickerView in XML layout
You can simply use ColorPickerView by defining it on your XML files. This ColorPickerView will be initialized with the default HSV color palette and the default selector.
<com.skydoves.colorpickerview.ColorPickerView
android:id="@+id/colorPickerView"
android:layout_width="300dp"
android:layout_height="300dp" />
Attributes
You can customize the palette image and selector or various options using the below attributes:
app:palette="@drawable/palette" // sets a custom palette image.
app:selector="@drawable/colorpickerview_wheel" // sets a custom selector image.
app:selector_size="32dp" // sets a width & height size of the selector.
app:alpha_selector="0.8" // sets an alpha of the selector.
app:alpha_flag="0.8" // sets an alpha of the flag.
app:actionMode="last" // sets action mode 'always' or 'last'.
// set an initial position of the selector using a specific color. This attribute will work with only a default HSV palette.
app:initialColor="@color/colorPrimary"
app:preferenceName="MyColorPicker" // sets a preference name.
app:debounceDuration="200" // sets a debounce duration of the invoking color listener.
ColorListener
ColorListener is triggered when a user taps the ColorPickerView or when a position is selected using a function.
colorPickerView.setColorListener(new ColorListener() {
@Override
public void onColorSelected(int color, boolean fromUser) {
LinearLayout linearLayout = findViewById(R.id.linearLayout);
linearLayout.setBackgroundColor(color);
}
});
ColorEnvelope
ColorEnvelope is a versatile wrapper class for color models, offering a wide range of color-related data. It provides access to HSV color values, Hex string codes, and ARGB values.
colorEnvelope.getColor() // returns a integer color.
colorEnvelope.getHexCode() // returns a hex code string.
colorEnvelope.getArgb() // returns a argb integer array.
ColorEnvelope Listener
ColorEnvelopeListener extends ColorListener and offers ColorEnvelope as a parameter, granting access to a variety of color values.
colorPickerView.setColorListener(new ColorEnvelopeListener() {
@Override
public void onColorSelected(ColorEnvelope envelope, boolean fromUser) {
linearLayout.setBackgroundColor(envelope.getColor());
textView.setText("#" + envelope.getHexCode());
}
});
Palette
If you do not set any custom palette, the default palette will be the ColorHsvPalette.<br>
You can manually select a specific point for the selector by specifying a particular color value using the following methods:
colorPickerView.selectByHsvColor(color);
colorPickerView.selectByHsvColorRes(R.color.colorPrimary);
You can change the default palette as a desired image drawable using the method below:<br>
colorPickerView.setPaletteDrawable(drawable);
If you wish to revert to the default HSV palette, you can do so using the method below:
colorPickerView.setHsvPaletteDrawable();
ActionMode
ActionMode is an option that restricts the invocation of the ColorListener based on user actions.
colorPickerView.setActionMode(ActionMode.LAST); // ColorListener will be invoked when the finger is released.
Debounce
If you want to emit color values to the listener with a particular delay, you can utilize debounceDuration attribute in your XML layout file:
app:debounceDuration="150"
Or you can set it programmatically.
colorPickerView.setDebounceDuration(150);
Create using builder
You can create an instance of ColorPickerView using ColorPickerView.Builder class like the example below:
ColorPickerView colorPickerView = new ColorPickerView.Builder(context)
.setColorListener(colorListener)
.setPreferenceName("MyColorPicker")
.setActionMode(ActionMode.LAST)
.setAlphaSlideBar(alphaSlideBar)
.setBrightnessSlideBar(brightnessSlideBar)
.setFlagView(new CustomFlag(context, R.layout.layout_flag))
.setPaletteDrawable(ContextCompat.getDrawable(context, R.drawable.palette))
.setSelectorDrawable(ContextCompat.getDrawable(context, R.drawable.selector))
.build();
Initial color
You can define an initial color and position the selector and slide bars based on that initial color. Please note that this function is compatible only with the default HSV palette. Additionally, if you set a preference name using the setPreferenceName method, this function will work only once.
app:initialColor="@color/colorPrimary"
Or you can use this method programmatically.
.setInitialColor(color);
.setInitialColorRes(R.color.colorPrimary);
Restore and save
This is how to restore the state of ColorPickerView.<br>
setPreferenceName() method restores all of the saved states (selector, color) automatically.
colorPickerView.setPreferenceName("MyColorPicker");
This is how to save the states of ColorPickerView.<br>
The setLifecycleOwner() method saves all of the states automatically when the lifecycleOwner is destroyed.
colorPickerView.setLifecycleOwner(this);
Or you can save the states manually using the method below:
ColorPickerPreferenceManager.getInstance(this).saveColorPickerData(colorPickerView);
Manipulate and clear
You can manipulate and clear the saved states using ColorPickerPreferenceManager.
ColorPickerPreferenceManager manager = ColorPickerPreferenceManager.getInstance(this);
manager.setColor("MyColorPicker", Color.RED); // manipulates the saved color data.
manager.setSelectorPosition("MyColorPicker", new Point(120, 120)); // manipulates the saved selector's position data.
manager.clearSavedAllData(); // clears all of the states.
manager.clearSavedColor("MyColorPicker"); // clears only saved color data.
manager.restoreColorPickerData(colorPickerView); // restores the saved states manually.
Palette from Gallery
Here is an example of how to get a bitmap drawable from the gallery image and set it to the palette.<br><br>
<img src="https://user-images.githubusercontent.
