Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/skydoves/colorpicker-compose

🎨 Kotlin Multiplatform color picker library for getting colors from any images by tapping on the desired color.
https://github.com/skydoves/colorpicker-compose

android color-picker colorpicker compose jetpack-compose skydoves

Last synced: 13 days ago
JSON representation

🎨 Kotlin Multiplatform color picker library for getting colors from any images by tapping on the desired color.

Awesome Lists containing this project

README

        

![colorpicker-compose](https://user-images.githubusercontent.com/24237865/157398567-9210c48d-9e9f-4ae4-aa82-b4a4a2740433.jpg)


Google

License
API
API
Android Weekly
Profile



🎨 Kotlin Multiplatform color picker library that allows you to get colors from any images like gallery pictures by tapping on the desired color.
Also, it supports brightness and alpha slider, which can adjust your ARGB factors.

## Preview





![kmp](https://github.com/user-attachments/assets/f81c43df-7172-4f44-8e11-898445551806)

## Download
[![Maven Central](https://img.shields.io/maven-central/v/com.github.skydoves/colorpicker-compose.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.github.skydoves%22%20AND%20a:%22colorpicker-compose%22)

### Version Catalog

If you're using Version Catalog, you can configure the dependency by adding it to your `libs.versions.toml` file as follows:

```toml
[versions]
#...
colorpicker = "1.1.2"

[libraries]
#...
compose-colorpicker = { module = "com.github.skydoves:colorpicker-compose", version.ref = "colorpicker" }
```

### Gradle
Add the dependency below to your **module**'s `build.gradle.kts` file:

```gradle
dependencies {
implementation("com.github.skydoves:colorpicker-compose:1.1.2")

// if you're using Version Catalog
implementation(libs.compose.colorpicker)
}
```

For Kotlin Multiplatform, add the dependency below to your **module**'s `build.gradle.kts` file:

```gradle
sourceSets {
commonMain.dependencies {
implementation("com.github.skydoves:colorpicker-compose:$version")
}
}
```

### Kotlin Multiplatform Support Maps

| Platform | Support |
|:---------------------------------:|:-------:|
| Android | ✅ |
| Desktop | ✅ |
| iOSX64/iOSArm64/iosSimulatorArm64 | ✅ |
| macosX64/macosArm64 | ✅ |
| js/WasmJs | ✅ |

## Usage

First, you should initialize `ColorPickerController`, which allows you to control color pickers and all subcomponents.

```kotlin
val controller = rememberColorPickerController()
```

Next, you can implement a color picker with the `ImageColorPicker` composable function.

```kotlin
ImageColorPicker(
modifier = Modifier.fillMaxSize(),
paletteImageBitmap = ImageBitmap.imageResource(R.drawable.palettebar),
controller = controller
)
```

### ImageColorPicker

**ImageColorPicker** allows you to get colors from any images such as gallery pictures or drawable resources by tapping on the desired color.
It interacts with the `ColorPickerController` to control the color picker and other components. You can use the `ImageColorPicker` as the following example:

```kotlin
ImageColorPicker(
modifier = Modifier
.fillMaxWidth()
.height(450.dp)
.padding(10.dp),
controller = controller,
paletteImageBitmap = ImageBitmap.imageResource(R.drawable.palettebar),
paletteContentScale = PaletteContentScale.FIT,
onColorChanged = { colorEnvelope: ColorEnvelope ->
// do something
}
)
```

With the [modernstorage](https://github.com/google/modernstorage)'s [Photo Picker](https://google.github.io/modernstorage/photopicker/), you can set an desired image as the palette like the below:

```kotlin
val context = LocalContext.current
val photoPicker =
rememberLauncherForActivityResult(PhotoPicker()) { uris ->
val uri = uris.firstOrNull() ?: return@rememberLauncherForActivityResult

val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.decodeBitmap(ImageDecoder.createSource(context.contentResolver, uri))
} else {
MediaStore.Images.Media.getBitmap(context.contentResolver, uri)
}

controller.setPaletteImageBitmap(bitmap.asImageBitmap())
}
```

As you can see the above, you can set the palette with the `setPaletteImageBitmap` function of the controller.

#### PaletteContentScale

You can adjust your palette's image scale with the `setPaletteContentScale` function of the controller as the below:

```kotlin
controller.setPaletteContentScale(PaletteContentScale.FIT) // scale the image to fit width and height.
controller.setPaletteContentScale(PaletteContentScale.CROP) // center crop the image.
```

### HsvColorPicker

HsvColorPicker allows you to get colors from HSV color palette by tapping on the desired color.
It interacts with the `ColorPickerController` to control the color picker and other components. You can use the `HsvColorPicker` as the following example:

```kotlin
HsvColorPicker(
modifier = Modifier
.fillMaxWidth()
.height(450.dp)
.padding(10.dp),
controller = controller,
onColorChanged = { colorEnvelope: ColorEnvelope ->
// do something
}
)
```

To initialize the color picker with a specific color, pass the color to the `initialColor` argument. Initial color is white by default.

> **Note**: If you use `HsvColorPicker`, you can not set the palette and content scale with the `setPaletteImageBitmap` and `setPaletteContentScale` functions.

### ColorEnvelope

**ColorEnvelope** is a data transfer object that includes updated color factors. If you pass the **onColorChanged** lambda function to the `ImageColorPicker` or `HsvColorPicker`, the lambda receives **ColorEnvelope**.
**ColorEnvelope** includes the following properties:

```kotlin
onColorChanged = { colorEnvelope: ColorEnvelope ->
val color: Color = colorEnvelope.color // ARGB color value.
val hexCode: String = colorEnvelope.hexCode // Color hex code, which represents color value.
val fromUser: Boolean = colorEnvelope.fromUser // Represents this event is triggered by user or not.
}
```

### ColorPickerController

**ColorPickerController** interacts with color pickers and it allows you to control the all subcomponents.

#### Custom Wheel

You can customize the wheel with the following functions:

```kotlin
.setWheelRadius(40.dp) // set the radius size of the wheel.
.setWheelColor(Color.Blue) // set the color of the wheel.
.setWheelAlpha(0.5f) // set the transparency of the wheel.
.setWheelImageBitmap(imageBitmap) // set the wheel image with your custom ImageBitmap.
```

#### Select Points

You can select specific points with the functions below:

```kotlin
.selectByCoordinate(x = 100f, y = 100f, fromUser = false) // select x = 100, y = 100.
.selectCenter(fromUser = false) // select center of the palette.
```

#### Debounce

You can set the debounce duration, which decides to invoke the color listener from the last tapping. Debounce can be useful to reduce overhead. For example, communicating with IoT devices or relevant works that require heavy operations.

```kotlin
controller.debounceDuration = 200L
```

#### Enable and Disable

You can enable or disable your color picker with the below function:

```kotlin
controller.enabled = false
```

### AlphaSlider

**AlphaSlider** allows you to adjust the alpha value of the selected color from color pickers.
**AlphaSlider** needs to be tied to the `ColorPickerController`, and the value changes will be assembled with the selected color factors.
You can implement **AlphaSlider** as the following example:

```kotlin
AlphaSlider(
modifier = Modifier
.fillMaxWidth()
.padding(10.dp)
.height(35.dp),
controller = controller,
)
```

You can customize the border of the sider with the following parameters:

```kotlin
AlphaSlider(
borderRadius = 6.dp,
borderSize = 5.dp,
borderColor = Color.LightGray,
..
)
```

You can customize the wheel of the sider with the following parameters:

```kotlin
AlphaSlider(
wheelRadius = 30.dp,
wheelColor = Color.White,
wheelPaint = Paint().apply { color = wheelColor },
wheelImageBitmap = ImageBitmap.imageResource(R.drawable.wheel),
..
)
```

Also, you can customize tiles of the background with the following parameters:

```kotlin
AlphaSlider(
tileOddColor = Color.White,
tileEvenColor = Color.LightGray,
tileSize = 30.dp,
..
)
```

### BrightnessSlider

**BrightnessSlider** allows you to adjust the brightness value of the selected color from color pickers.
**BrightnessSlider** needs to be tied to the `ColorPickerController`, and the value changes will be assembled with the selected color factors.
You can implement **BrightnessSlider** as the following example:

```kotlin
BrightnessSlider(
modifier = Modifier
.fillMaxWidth()
.padding(10.dp)
.height(35.dp),
controller = controller,
)
```

You can customize the wheel of the sider with the following parameters:

```kotlin
BrightnessSlider(
wheelRadius = 30.dp,
wheelColor = Color.White,
wheelPaint = Paint().apply { color = wheelColor },
wheelImageBitmap = ImageBitmap.imageResource(R.drawable.wheel),
..
)
```

### AlphaTile

**AlphaTile** allows you to display ARGB colors including transparency with tiles.

```kotlin
AlphaTile(
modifier = Modifier
.size(80.dp)
.clip(RoundedCornerShape(6.dp)),
controller = controller
)
```

Also, you can customize tiles of the background with the following parameters:

```kotlin
AlphaTile(
tileOddColor = Color.White,
tileEvenColor = Color.LightGray,
tileSize = 30.dp,
..
)
```

## Find this repository useful? :heart:
Support it by joining __[stargazers](https://github.com/skydoves/colorpicker-compose/stargazers)__ for this repository. :star:

Also, __[follow me](https://github.com/skydoves)__ on GitHub for my next creations! 🤩

# License
```xml
Designed and developed by 2022 skydoves (Jaewoong Eum)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```