B4A Library Palette - Create color palette from bitmap

For some (Material Design) color effects it is important to create a color palette or just a Highlight color from a Bitmap. Google added the Palette Class for this to the support library.
This is a wrapper for this class.

First some example images of the example app:
PalExample1.png PalExample2.png PalExample3.png

Installation:
You need to use B4A 6.30+ and the Android Support Repository must be installed with SDK Manager.
Copy all files (.xml, .jar, .aar) from the PaletteLibx_xx.zip file to your additional libraries folder.

Your support:
Creating libraries and wrappers for existing library projects is a lot of work. The use of this library is totally free and you even don't need to mention in your app that you use it.
But if you use this library in your projects and you think it is useful to you please consider to make a donation:

Thanks very much for your support.


Short usage sample:
B4X:
[...]
    Dim paletteBuilder As PaletteBuilder
    paletteBuilder.Initialize("Palette", ImageView1.Bitmap)
    paletteBuilder.GenerateAsync
[...]

Sub Palette_Generated (Palette As Palette)
    Panel1.Color = Palette.GetVibrantColor(Colors.Black)
    If Palette.VibrantSwatch <> Null Then
        Label1.TextColor = Palette.VibrantSwatch.BodyTextColor
        Label1.Text = "Vibrant: " & Palette.VibrantSwatch.Population
    Else
        Label1.TextColor = Colors.White
        Label1.Text = "Vibrant (not in Palette)"
    End If
[...]

First initialize a PaletteBuilder object with eventname and a bitmap which should be analyzed. Then call PaletteBuilder.GenerateAsync. You should not use PaletteBuilder.Generate in the UI thread since analyzing the image will take some time and the UI thread will be blocked!

If analyzing of the image is done, the "Generated" event will be fired with a Palette object as a parameter. The palette will have "vibrant" and "muted" colors that are extracted from the image. Additionally there are so called Swatches which have the normal color (.RGB property) and high contrast TitleText and BodyText colors.

See the example of the usage.

Official Android documentation

Reference:
Palette
Author:
Markus Stipp
Version: 1
  • Palette
    Methods:
    • GetAllSwatches As List
      Returns all of the swatches which make up the palette.
    • GetDarkMutedColor (DefaultColor As Int) As Int
      Returns a muted and dark color from the palette as an RGB packed int.

      DefaultColor - value to return if the swatch isn't available
    • GetDarkVibrantColor (DefaultColor As Int) As Int
      Returns a dark and vibrant color from the palette as an RGB packed int.

      DefaultColor - value to return if the swatch isn't available
    • GetDominantColor (DefaultColor As Int) As Int
      Returns the color of the dominant swatch from the palette, as an RGB packed int.

      DefaultColor - value to return if the swatch isn't available
    • GetLightMutedColor (DefaultColor As Int) As Int
      Returns a muted and light color from the palette as an RGB packed int.

      DefaultColor - value to return if the swatch isn't available
    • GetLightVibrantColor (DefaultColor As Int) As Int
      Returns a light and vibrant color from the palette as an RGB packed int.

      DefaultColor - value to return if the swatch isn't available
    • GetMutedColor (DefaultColor As Int) As Int
      Returns a muted color from the palette as an RGB packed int.

      DefaultColor - value to return if the swatch isn't available
    • GetVibrantColor (DefaultColor As Int) As Int
      Returns the most vibrant color in the palette as an RGB packed int.

      DefaultColor - value to return if the swatch isn't available
    • IsInitialized As Boolean
    Properties:
    • DarkMutedSwatch As SwatchWrapper [read only]
      Returns a muted and dark swatch from the palette. Might be null.
    • DarkVibrantSwatch As SwatchWrapper [read only]
      Returns a dark and vibrant swatch from the palette. Might be null.
    • DominantSwatch As SwatchWrapper [read only]
      The dominant swatch is defined as the swatch with the greatest population (frequency) within the palette.
    • LightMutedSwatch As SwatchWrapper [read only]
      Returns a muted and light swatch from the palette. Might be null.
    • LightVibrantSwatch As SwatchWrapper [read only]
      Returns a light and vibrant swatch from the palette. Might be null.
    • MutedSwatch As SwatchWrapper [read only]
      Returns a muted swatch from the palette. Might be null.
    • VibrantSwatch As SwatchWrapper [read only]
      Returns the most vibrant swatch in the palette. Might be null.
  • PaletteBuilder
    Events:
    • Generated (Palette as Palette As )
    Methods:
    • ClearRegion
      Clear a region which was previously set.
    • Generate As PaletteWrapper
      Generate and return the Palette synchronously.

      ATTENTION: This should not be called from the main UI thread. Use GenerateAsync() instead!
    • GenerateAsync
      Generate the Palette asynchronously. The Generated event will be fired with the resulting Palette object.
    • Initialize (EventName As String, Bitmap As Bitmap)
      Initializs the object.

      EventName - Event name for the events fired.
      Bitmap - A Bitmap that will be analyzed.
    • SetRegion (Left As Int, Top As Int, Right As Int, Bottom As Int)
      Set a region of the bitmap to be used exclusively when calculating the palette.
    Properties:
    • MaximumColorCount As Int [write only]
      Set the maximum number of colors to use in the quantization step when using a Bitmap as the source.

      Good values for depend on the source image type. For landscapes, good values are in the range 10-16. For images which are largely made up of people's faces then this value should be increased to ~24.
    • ResizeBitmapArea As Int [write only]
      Set the resize value when using a Bitmap as the source. If the bitmap's area is greater than the value specified, then the bitmap will be resized so that it's area matches area. If the bitmap is smaller or equal, the original is used as-is.

      This value has a large effect on the processing time. The larger the resized image is, the greater time it will take to generate the palette. The smaller the image is, the more detail is lost in the resulting image and thus less precision for color selection.

      The value is the number of pixels that the intermediary scaled down Bitmap should cover, or any value <= 0 to disable resizing.
  • Swatch
    Methods:
    • IsInitialized As Boolean
      Check if the object is initialized.
    Properties:
    • BodyTextColor As Int [read only]
      Returns an appropriate color to use for any 'body' text which is displayed over this Swatch's color. This color is guaranteed to have sufficient contrast.
    • Population As Int [read only]
      The number of pixels represented by this swatch
    • RGB As Int [read only]
      This swatch's RGB color value
    • TitleTextColor As Int [read only]
      Returns an appropriate color to use for any 'title' text which is displayed over this Swatch's color. This color is guaranteed to have sufficient contrast.

Version History:
V1.0:
  • Initial version
 

Attachments

  • PaletteExample1_0.zip
    344.9 KB · Views: 388
  • PaletteLib1_0.zip
    7.3 KB · Views: 357
Last edited:
Top