B4A Library [B4X]ColourThief (Cross platform colour palette generating algorithm)

This is a port to B4X of the original Color-Thief algorithm by Lokesh Dhakar as described here.

I've included the original site pictures in the demo for comparison.

I'm English so I have used the correct spelling of the word in its class name ;-)

The MIT License (MIT)

Copyright (c) 2015 Lokesh Dhakar

Permission Is hereby granted, free of charge, To any person obtaining a copy
of this software And associated documentation files (the "Software"), To deal
in the Software without restriction, including without limitation the rights
To use, copy, modify, merge, publish, distribute, sublicense, And/Or sell
copies of the Software, And To permit persons To whom the Software Is
furnished To Do so, subject To the following conditions:

The above copyright notice And this permission notice shall be included in all
copies Or substantial portions of the Software.

THE SOFTWARE Is PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS Or
IMPLIED, INCLUDING BUT Not LIMITED To THE WARRANTIES OF MERCHANTABILITY,
FITNESS For A PARTICULAR PURPOSE And NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS Or COPYRIGHT HOLDERS BE LIABLE For ANY CLAIM, DAMAGES Or OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT Or OTHERWISE, ARISING FROM,
OUT OF Or IN CONNECTION WITH THE SOFTWARE Or THE USE Or OTHER DEALINGS IN THE
SOFTWARE.

I needed a fully cross platform colour palette generator for an ongoing project and I initially converted a KMeans algorithm to B4X however I faced a couple of issues with it. Mainly the fact that random points are used as starting points so running the code multiple times gives different results. Also I simply couldn't get it to run particularly quickly on IOS.

A bit of searching came up with this algorithm written in Java so here it is converted to B4X. Benefits with this - 1 - You will always get the same palette given the same image. 2 - Very fast on all platforms.

Use:


B4X:
    Dim CF As ColourThief
    CF.Initialize

Then the functions you're interested in are


B4X:
' Returns the single most dominant colour as an ARGBColor value
' Req - sourceImage As B4XBitmap - Any size bitmap - it will be converted/optimised within the class

Public Sub getDominantColor(sourceImage As B4XBitmap) As ARGBColor

' More control here - quality (default is 10) is how many pixels to skip between samples
' ignoreWhite (default True) simply skips any white pixels it detects.

Public Sub getDominantColor2(sourceImage As BitmapCreator, quality As Int, ignoreWhite As Boolean)  As ARGBColor

'Returns a map of ARGBColor values, set how many with colorCount
'Ordered by most dominant to least dominant
'colorCount should be 2 - 255

Public Sub getPalette(sourceImage As B4XBitmap, colorCount As Int) As Map

'As previous with more control

Public Sub getPalette2(sourceImage As BitmapCreator, colorCount As Int, quality As Int, ignoreWhite As Boolean) As Map

'And finally a CMap object is a list of VBox classes used to quantize each of the colours using Mediancuts.
'There's more information in here about each colour selected such as count, average, histograms etc.
'Probably of very little use.

Public Sub getColorMap(sourceImage As B4XBitmap, colorCount As Int) As CMap

Public Sub getColorMap2(sourceImage As BitmapCreator, colorCount As Int, quality As Int, ignoreWhite As Boolean) As CMap

Demo grab of an 8 colour palette.

Capture.JPG


[Edit] I could optimise it further with a few tweaks but I doubt I will bother given it works near enough instantly on an iPhone 6 which means it's well fast enough for my needs. If anyone actually needs it tweaking for whatever reason please shout out. The only change I'm currently considering is to use B4XComparatorSort that Erel recently posted about here - It was the only bit of the original Java that confused me converting to B4X and shortly after I'd come up with my own way of doing it he released his solution. I strongly suspect he's omnipotent and is preempting my every move.
 

Attachments

  • ColourThief.b4xlib
    5.3 KB · Views: 331
  • ColourThiefDemo.zip
    475.2 KB · Views: 346
Last edited:

Lucas Siqueira

Active Member
Licensed User
Longtime User
Hello, I took the liberty of making some modifications with the help of some posts here on the forum.

The objective is to select an image on the computer and also be able to copy it from the internet and place it with the paste button (CTRL+V).

When you click on the palette list, we copy the color code to the Windows clipboard.
If you click on any part of the image, we also identify the color and copy the code to the Windows clipboard.

Adjustments made only in B4J.


1721335092542.png
1721335122103.png
 

Attachments

  • ProjectcOLORpALETE.zip
    476.8 KB · Views: 89

John Naylor

Active Member
Licensed User
Longtime User
Hello, I took the liberty of making some modifications with the help of some posts here on the forum.

The objective is to select an image on the computer and also be able to copy it from the internet and place it with the paste button (CTRL+V).

When you click on the palette list, we copy the color code to the Windows clipboard.
If you click on any part of the image, we also identify the color and copy the code to the Windows clipboard.

Adjustments made only in B4J.


View attachment 155511View attachment 155512
That's a handy modification to the demo but to be honest I would say it could easily be in a post of its own as a utility. My original simple demo was only there to show off the use of my b4xlib. What you have created here could well be useful to a lot of people who may miss it because it's hidden away in this library thread.
 

Lucas Siqueira

Active Member
Licensed User
Longtime User
That's a handy modification to the demo but to be honest I would say it could easily be in a post of its own as a utility. My original simple demo was only there to show off the use of my b4xlib. What you have created here could well be useful to a lot of people who may miss it because it's hidden away in this library thread.
I still want to add the zoom function to the image. After adding can I be publishing?
 

John Naylor

Active Member
Licensed User
Longtime User
I still want to add the zoom function to the image. After adding can I be publishing?
Please do! My work will be recognised because of the lib so I am very pleased and I for one will definately use your app once you publish it. I've been doing a lot of work with screen layouts and colour palettes recently and what you have created will assist me further!
 
Top