Image Color Picker in Android using Jetpack Compose
Many android applications require functionality in which the user should be able to pick the color from the image which is present within the android application. In this article, we will take a look at How to implement Image Color Picker in Android applications using Jetpack Compose. We will be able to pick the colors from the image within the android application. A sample video is given below to get an idea about what we are going to do in this article.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Adding dependency in build.gradle file
Navigate to app>build.gradle and add the below dependency in the dependencies section.
implementation "com.github.skydoves:colorpicker-compose:1.0.0"
Now sync your project to install the dependency within the android application.
Step 3: Adding a new color to the Color.kt file
Navigate to app > java > your app’s package name > ui.theme > Color.kt file and add the below code to it.
Kotlin
package com.example.newcanaryproject.ui.theme import androidx.compose.ui.graphics.Color val Purple200 = Color( 0xFF0F9D58 ) val Purple500 = Color( 0xFF0F9D58 ) val Purple700 = Color( 0xFF3700B3 ) val Teal200 = Color( 0xFF03DAC5 ) // on below line we are adding different colors. val greenColor = Color( 0xFF0F9D58 ) |
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.example.newcanaryproject import android.app.Activity import android.content.Context import android.net.Uri import android.os.Bundle import android.util.DisplayMetrics import android.util.Log import android.view.WindowManager import android.widget.Chronometer import android.widget.MediaController import android.widget.Toast import android.widget.VideoView import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.* import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.* import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Add import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.* import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.imageResource import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.* import com.example.newcanaryproject.ui.theme.* import com.github.skydoves.colorpicker.compose.* import dev.shreyaspatil.easyupipayment.EasyUpiPayment import dev.shreyaspatil.easyupipayment.listener.PaymentStatusListener import dev.shreyaspatil.easyupipayment.model.PaymentApp import dev.shreyaspatil.easyupipayment.model.TransactionDetails import java.text.SimpleDateFormat import java.util.* class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContent { NewCanaryProjectTheme { // on below line we are specifying background color for our application Surface( // on below line we are specifying modifier and color for our app modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background ) { // on the below line we are specifying the theme as the scaffold. Scaffold( // in scaffold we are specifying the top bar. topBar = { // inside top bar we are specifying background color. TopAppBar(backgroundColor = greenColor, // along with that we are specifying title for our top bar. title = { // in the top bar we are specifying tile as a text Text( // on below line we are specifying // text to display in top app bar. text = "GFG" , // on below line we are specifying // modifier to fill max width. modifier = Modifier.fillMaxWidth(), // on below line we are specifying // text alignment. textAlign = TextAlign.Center, // on below line we are specifying // color for our text. color = Color.White ) }) }) { // on below line we are calling // image color picker function imageColorPicker() } } } } } } @Composable fun imageColorPicker() { // on below line we are creating a variable // for controller and initializing it. val controller = rememberColorPickerController() // on below line we are creating a column // and adding a modifier to it. Column(modifier = Modifier.fillMaxSize()) { // on below line we are adding a spacer to it. Spacer(modifier = Modifier.height( 30 .dp)) // on below line we are creating an image color picker. ImageColorPicker( // on below line we are adding a modifier to it. modifier = Modifier .fillMaxWidth() // on below line we are adding height. .height( 300 .dp) // on below line we are adding a passing. .padding( 20 .dp), // on below line we are adding our image to pick color from paletteImageBitmap = ImageBitmap.imageResource(R.drawable.img1), // on below line we are // adding a controller to it. controller = controller, // on below line we are adding a palette scale as fit. paletteContentScale = PaletteContentScale.FIT, // on below line we are checking for color change. onColorChanged = { colorEnvelope: ColorEnvelope -> } ) // on below line we are adding a spacer. Spacer(modifier = Modifier.height( 12 .dp)) // on below line we are adding a row. Row( // on below line we // are adding a modifier to it modifier = Modifier .fillMaxWidth() // on below line we are // adding a padding to it. .padding(horizontal = 30 .dp), // on below line we are adding a // horizontal and vertical alignment. horizontalArrangement = Arrangement.Center, verticalAlignment = Alignment.CenterVertically ) { // on below line we are adding a alpha tile. AlphaTile( // on below line // we are adding a modifier modifier = Modifier .fillMaxWidth() // on below line // we are adding a height .height( 60 .dp) // on below line we are // adding a clip for round edges. .clip(RoundedCornerShape( 6 .dp)), // on below line we are adding a controller. controller = controller ) } // on below line we are // adding an alpha slider AlphaSlider( // on below line we are // adding a modifier. modifier = Modifier .fillMaxWidth() .padding( 10 .dp) .height( 35 .dp), // on below line we are // adding a controller controller = controller, ) // on below line we are // creating a brightness slider. BrightnessSlider( // on below line we are // adding modifier to // add padding, height modifier = Modifier .fillMaxWidth() .padding( 10 .dp) .height( 35 .dp), // on below line we are // adding a controller. controller = controller, ) } } |
Now run your application and see the output of it.
Output: