Documentation

ColorThief
in package

Table of Contents

FRACT_BY_POPULATIONS  = 0.75
MAX_ITERATIONS  = 1000
RSHIFT  = 3
SIGBITS  = 5
THRESHOLD_ALPHA  = 62
THRESHOLD_WHITE  = 250
getColor()  : mixed
Gets the dominant color from the image using the median cut algorithm to cluster similar colors.
getColorIndex()  : int
Get combined color index (3 colors as one integer) from RGB values (0-255) or RGB Histogram Buckets (0-31).
getColorsFromIndex()  : array<string|int, mixed>
Get RGB values (0-255) or RGB Histogram Buckets from a combined color index (3 colors as one integer).
getPalette()  : array<string|int, mixed>
Gets a palette of dominant colors from the image using the median cut algorithm to cluster similar colors.
doCut()  : array<string|int, mixed>|null
getVBoxColorRanges()  : array<string|int, array<string|int, int>>
loadImage()  : int
medianCutApply()  : array<string|int, VBox>|null
quantize()  : array<string|int, Color>
quantizeIter()  : void
Inner function to do the iteration.
sumColors()  : array<string|int, mixed>
Find the partial sum arrays along the selected axis.
vboxFromHistogram()  : VBox

Constants

FRACT_BY_POPULATIONS

public mixed FRACT_BY_POPULATIONS = 0.75

MAX_ITERATIONS

public mixed MAX_ITERATIONS = 1000

THRESHOLD_ALPHA

public mixed THRESHOLD_ALPHA = 62

THRESHOLD_WHITE

public mixed THRESHOLD_WHITE = 250

Methods

getColor()

Gets the dominant color from the image using the median cut algorithm to cluster similar colors.

public static getColor(mixed $sourceImage[, int $quality = 10 ][, array<string|int, mixed>|null $area = null ][, string $outputFormat = 'array' ][, AdapterInterface|string|null $adapter = null ]) : mixed
Parameters
$sourceImage : mixed

Path/URL to the image, GD resource, Imagick/Gmagick instance, or image as binary string

$quality : int = 10

1 is the highest quality. There is a trade-off between quality and speed. It determines how many pixels are skipped before the next one is sampled. We rarely need to sample every single pixel in the image to get good results. The bigger the number, the faster the palette generation but the greater the likelihood that colors will be missed.

$area : array<string|int, mixed>|null = null

It allows you to specify a rectangular area in the image in order to get colors only for this area. It needs to be an associative array with the following keys: $area['x']: The x-coordinate of the top left corner of the area. Default to 0. $area['y']: The y-coordinate of the top left corner of the area. Default to 0. $area['w']: The width of the area. Default to image width minus x-coordinate. $area['h']: The height of the area. Default to image height minus y-coordinate.

$outputFormat : string = 'array'

By default, color is returned as an array of three integers representing red, green, and blue values. You can choose another output format by passing one of the following values: 'rgb' : RGB string notation (ex: rgb(253, 42, 152)). 'hex' : String of the hexadecimal representation (ex: #fd2a98). 'int' : Integer color value (ex: 16591512). 'array' : Default format (ex: [253, 42, 152]). 'obj' : Instance of ColorThief\Color, for custom processing.

$adapter : AdapterInterface|string|null = null

Optional argument to choose a preferred image adapter to use for loading the image. By default, the adapter is automatically chosen depending on the available extensions and the type of $sourceImage (for example Imagick is used if $sourceImage is an Imagick instance). You can pass one of the 'Imagick', 'Gmagick' or 'Gd' string to use the corresponding underlying image extension, or you can pass an instance of any class implementing the AdapterInterface interface to use a custom image loader.

Tags
phpstan-param

?RectangularArea $area

phpstan-return

ColorRGB|Color|int|string|null

Return values
mixed

getColorIndex()

Get combined color index (3 colors as one integer) from RGB values (0-255) or RGB Histogram Buckets (0-31).

public static getColorIndex(int $red, int $green, int $blue[, int $sigBits = self::SIGBITS ]) : int
Parameters
$red : int
$green : int
$blue : int
$sigBits : int = self::SIGBITS
Return values
int

getColorsFromIndex()

Get RGB values (0-255) or RGB Histogram Buckets from a combined color index (3 colors as one integer).

public static getColorsFromIndex(int $index[, int $sigBits = 8 ]) : array<string|int, mixed>
Parameters
$index : int
$sigBits : int = 8
Tags
phpstan-return

ColorRGB

Return values
array<string|int, mixed>

getPalette()

Gets a palette of dominant colors from the image using the median cut algorithm to cluster similar colors.

public static getPalette(mixed $sourceImage[, int $colorCount = 10 ][, int $quality = 10 ][, array<string|int, mixed>|null $area = null ][, string $outputFormat = 'array' ][, AdapterInterface|string|null $adapter = null ]) : array<string|int, mixed>
Parameters
$sourceImage : mixed

Path/URL to the image, GD resource, Imagick/Gmagick instance, or image as binary string

$colorCount : int = 10

it determines the size of the palette; the number of colors returned

$quality : int = 10

1 is the highest quality. There is a trade-off between quality and speed. It determines how many pixels are skipped before the next one is sampled. We rarely need to sample every single pixel in the image to get good results. The bigger the number, the faster the palette generation but the greater the likelihood that colors will be missed.

$area : array<string|int, mixed>|null = null

It allows you to specify a rectangular area in the image in order to get colors only for this area. It needs to be an associative array with the following keys: $area['x']: The x-coordinate of the top left corner of the area. Default to 0. $area['y']: The y-coordinate of the top left corner of the area. Default to 0. $area['w']: The width of the area. Default to image width minus x-coordinate. $area['h']: The height of the area. Default to image height minus y-coordinate.

$outputFormat : string = 'array'

By default, colors are returned as an array of three integers representing red, green, and blue values. You can choose another output format by passing one of the following values: 'rgb' : RGB string notation (ex: rgb(253, 42, 152)). 'hex' : String of the hexadecimal representation (ex: #fd2a98). 'int' : Integer color value (ex: 16591512). 'array' : Default format (ex: [253, 42, 152]). 'obj' : Instance of ColorThief\Color, for custom processing.

$adapter : AdapterInterface|string|null = null

Optional argument to choose a preferred image adapter to use for loading the image. By default, the adapter is automatically chosen depending on the available extensions and the type of $sourceImage (e.g. Imagick is used if $sourceImage is an Imagick instance). You can pass one of the 'Imagick', 'Gmagick' or 'Gd' string to use the corresponding underlying image extension, or you can pass an instance of any class implementing the AdapterInterface interface to use a custom image loader.

Tags
phpstan-param

?RectangularArea $area

phpstan-return

ColorRGB[]|Color[]|int[]|string[]|null

Return values
array<string|int, mixed>

doCut()

private static doCut(string $color, VBox $vBox, array<string|int, int> $partialSum, int $total) : array<string|int, mixed>|null
Parameters
$color : string
$vBox : VBox
$partialSum : array<string|int, int>
$total : int
Return values
array<string|int, mixed>|null

getVBoxColorRanges()

private static getVBoxColorRanges(VBox $vBox, array<string|int, mixed> $order) : array<string|int, array<string|int, int>>
Parameters
$vBox : VBox
$order : array<string|int, mixed>
Tags
phpstan-param

array<'r'|'g'|'b'> $order

phpstan-return

array{int[], int[], int[]}

Return values
array<string|int, array<string|int, int>>

loadImage()

private static loadImage(mixed $sourceImage, int $quality, array<int, int> &$histo[, array<string|int, mixed> $area = null ][, AdapterInterface|string|null $adapter = null ]) : int
Parameters
$sourceImage : mixed

Path/URL to the image, GD resource, Imagick instance, or image as binary string

$quality : int

Analyze every $quality pixels

$histo : array<int, int>

Histogram

$area : array<string|int, mixed> = null
$adapter : AdapterInterface|string|null = null

Image adapter to use for loading the image

Tags
phpstan-param

?RectangularArea $area

Return values
int

medianCutApply()

private static medianCutApply(array<int, int> $histo, VBox $vBox) : array<string|int, VBox>|null
Parameters
$histo : array<int, int>
$vBox : VBox
Return values
array<string|int, VBox>|null

quantize()

private static quantize(int $numPixels, int $maxColors, array<int, int> &$histo) : array<string|int, Color>
Parameters
$numPixels : int

Number of image pixels analyzed

$maxColors : int
$histo : array<int, int>

Histogram

Return values
array<string|int, Color>

quantizeIter()

Inner function to do the iteration.

private static quantizeIter(PQueue<string|int, VBox&$priorityQueue, float $target, array<int, int> $histo) : void
Parameters
$priorityQueue : PQueue<string|int, VBox>
$target : float
$histo : array<int, int>
Return values
void

sumColors()

Find the partial sum arrays along the selected axis.

private static sumColors(string $axis, array<int, int> $histo, VBox $vBox) : array<string|int, mixed>
Parameters
$axis : string

r|g|b

$histo : array<int, int>
$vBox : VBox
Tags
phpstan-param

'r'|'g'|'b' $axis

Return values
array<string|int, mixed>

vboxFromHistogram()

private static vboxFromHistogram(array<int, int> $histo) : VBox
Parameters
$histo : array<int, int>
Return values
VBox

Search results