How to use Palette API to extract colors from Image or Vector drawable Image Fully detailed tutorial


extract colors from bitmap palette api example
The usages example of Palette API - Android Studio


Hello Guys, Today I going to share an amazing tutorial on Palette API, The API is very easy to implement and has many useful features, but today I going to share the only the process of how we can use Palette to get Color from regular Image, Or from vector drawbles.
The old constructor instances:

 Bitmap bmp; //(Keep In mind that, You need decode the bitmap or vector first to get its color from palette, and this is just an example)
Palette palette = Palette.generate(bmp); 

is deprecated from android API 29, So app target SDK 29 or Minimum SDK 29 we need an alternative version of this constructor.
Don't worry, I am here to help!

The Palette.generate(Bitmap) deprecated,The alternative code is here!


Now time to write the right code for palette.generate(Bitmap) & for color extraction

The Code:
 //Starts the code from here!

       //decode the bitmap so that palette can use this bitmap!
       Bitmap bmp = BitmapFactory.decodeResource(/*Your Activity name.this, but I used MainActivity.this for instances*/ MainActivity.this.getApplicationContext().getResources(), R.drawable.your_drawable_image_name_here);
    
   //now time to input the image for palette so that we can get the color from this image
       Palette palette = Palette.from(bmp).generate();
      
 //Now time to get the int of the color so that we can set the color direct for use
     
  //I Have used the Light Vibrant Color, there are another mode too that you can get
       int colorLight = palette.getLightVibrantColor(0x000000);
    
   //For instance, I use this color for my toolbar
        toolbar.setBackgroundColor(colorLight);
  
      //below I going to share process of how you can get the same result for Vector drawables
      
  //Continue read it!

Note that: This above code from above can be used to decode and get color from bitmap or regular Image, It'll Never work with Vector Image.

extract colors from bitmap palette api example
See this screenshot for understanding the extract colors from bitmap using palette


For Vector Image, We'll use another trick!

The code for Vector Drawable Is starts from here:


  //the code for extracts vector drawable starts from here.
       //Get the Vector drwable from res folder
        Drawable drawable = ContextCompat.getDrawable(MainActivity.this, R.drawable.your_vector_drawable_name_here);
        //Now time to code so that we can decode & make the vector drawble to an regular Bitmap Image
        //The below code will do this job
        Bitmap bitmap = Bitmap.createBitmap(Objects.requireNonNull(d).getIntrinsicWidth(),
                drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
      //now time to input the image for palette so that we can get the color from this image
       Palette palette = Palette.from(bitmap).generate();
       //Now time to get the int of the color so that we can set the color direct for use
       //I Have used the Light Vibrant Color, there are another mode too that you can get
       int colorLight = palette.getLightVibrantColor(0x000000);
       //For instance, I use this color for my toolbar
        toolbar.setBackgroundColor(colorLight);


extract colors from vector palette api example
Example code for extracting colors from vector drawable using palette


WoW damn easy to Implement, Am I Right??


You need not worry about compatibility! This code is compatible with the minimum SDK version of this library!


Comments

Popular posts from this blog

MIUI 12.6 Android 11 port for Poco M3 download - Well tested ROM

How Long to Keep Business Insurance Policies | Retain and Destroy Insurance Policies

RealMe GT NEO 2 Bootloader unlock method on windows 11 - Updated