Documentation/D2DBitmap
classWORKERCLONEABLETRANSFERABLE

D2DBitmap

js/d2d.js:1
D2DBitmap(arg)

Parameters

NameTypeDescription
argD2DBitmap
propertyWORKERreadonly

Height

js/d2d.js:10
Height: number
propertyWORKERreadonly

Width

js/d2d.js:17
Width: number
methodWORKER

ApplyAlpha

js/d2d.js:24
ApplyAlpha(alpha)

Parameters

NameTypeDescription
alphanumber

Valid values 0-255.

Returns

D2DBitmap
methodWORKER

ApplyAlphaIndirect

js/d2d.js:31
ApplyAlphaIndirect(alpha)

Apllies alpha for entire image. Unlike D2DBitmap#ApplyAlpha changes the current object

Parameters

NameTypeDescription
alphanumber

Valid values 0-255.

methodWORKER

ApplyMask

js/d2d.js:38
ApplyMask(img)

Changes will be saved in the current bitmap.

Parameters

NameTypeDescription
imgD2DBitmap

Example source

methodWORKER

ApplyMaskNew

js/d2d.js:48
ApplyMaskNew(mask)

Applies a mask and returns a new bitmap, leaving the current bitmap unchanged. The mask must have the same dimensions and pixel format as the current bitmap.

Parameters

NameTypeDescription
maskD2DBitmap

Returns

D2DBitmap
methodWORKER

Clone

js/d2d.js:58
Clone(x, y, w, h)

Create partial copy of image represented by D2DBitmap object

Parameters

NameTypeDescription
xnumber
ynumber
wnumber
hnumber

Returns

D2DBitmap
methodWORKER

CreateRawBitmap

js/d2d.js:70
CreateRawBitmap()

Create clone bitmap from D2DBitmap object

Returns

D2DBitmap
methodWORKER

GetColourScheme

js/d2d.js:78
GetColourScheme(max_count)

Returns up to max_count representative colours found in the image.
This is a legacy colour extraction method. For colour frequency information and more advanced clustering, use GetColourSchemeJSON or GetColourSchemeJSONV2.

Parameters

NameTypeDescription
max_countnumber

maximum number of colours to return

Returns

Array<number>
methodWORKER

GetColourSchemeJSON

js/d2d.js:90
GetColourSchemeJSON(max_count)

Extracts representative colours from the image using K-means clustering in the RGB colour space.
Returns a JSON array in string form so you need to use JSON.parse() on the result.
Each entry contains a colour and its relative frequency in the clustered image.
Uses a different method for calculating colours than GetColourScheme.
Image is automatically resized during processing for performance reasons so there's no need to resize before calling the method.

Parameters

NameTypeDescription
max_countnumber

maximum number of colours to return

Returns

string

Example

// See docs\Helpers.js for "toRGB" function.
img = ... // use utils.GetAlbumArtV2 / gdi.Image / etc
colours = JSON.parse(img.GetColourSchemeJSON(5));
console.log(colours[0].col); // 4290772992
console.log(colours[0].freq); // 0.34
console.log(toRGB(colours[0].col)); // [192, 0, 0]
methodWORKER

GetColourSchemeJSONV2

js/d2d.js:112
GetColourSchemeJSONV2(max_count, min_chroma)

Extracts representative colours from the image using K-means++ clustering in the Oklab colour space.
Returns a JSON array in string form so you need to use JSON.parse() on the result.
Each entry contains a colour and its relative frequency in the clustered image.
K-means++ initialization improves the distribution of the initial cluster centres, while Oklab provides a perceptually more uniform distance metric than RGB.
The optional min_chroma parameter limits the initial cluster centre selection to pixels with at least the specified Oklab chroma. It does not filter pixels or colours from the clustering result.

Parameters

NameTypeDescription
max_countnumber

maximum number of colours to return

min_chroma = 0.0optionalnumber

minimum Oklab chroma for pixels used as initial cluster centres

Returns

string

Example

// See docs\Helpers.js for "toRGB" function.
img = ... // use utils.GetAlbumArtV2 / gdi.Image / etc
colours = JSON.parse(img.GetColourSchemeJSONV2(10, 0.02));
console.log(colours[0].col); // 4290772992
console.log(colours[0].freq); // 0.34
console.log(toRGB(colours[0].col)); // [192, 0, 0]
methodWORKER

GetGraphics

js/d2d.js:176
GetGraphics()

IMPORTANT: You MUST call ReleaseGraphics after work on D2DGraphics is done!
It is illegal to call any methods of D2DBitmap object while working with the obtained D2DGraphics object until ReleaseGraphics is called.

Returns

D2DGraphics
methodWORKER

GetPixelData

js/d2d.js:185
GetPixelData(format)

Extract raw pixels from bitmap as a byte array in specified pixel format

Parameters

NameTypeDescription
format = "bgra32"optionalstring

Pixel format string (default: "bgra32") Supported formats:
"bgra32" 32bpp BGRA
"rgba32" 32bpp RGBA
"bgr24" 24bpp BGR
"rgb24" 24bpp RGB

Returns

Uint8Array —

null if was an error (for example, bitmap in unsupported format or unsupported format specified)

Example source

methodWORKER

GetThemeColourSchemeJSON

js/d2d.js:136
GetThemeColourSchemeJSON(paletteSize)

Builds an image-derived UI colour scheme for styling interface surfaces, text and interaction states from the bitmap.

The image is analysed in OKLab using the same weighted K-means++ palette extraction as GetColourSchemeJSONV2.
The palette stage uses paletteSize centroids after which colours are assigned to five semantic UI roles:
Background, Text, Playing background, Focus frame and Selection background.

The returned colours are unsigned 32-bit ARGB values.
Background and Text are opaque.
State colours may contain alpha and are intended to be composited over Background.
playingBackgroundColor contains the Playing accent RGB together with the recommended alpha for a background highlight.
The same RGB channels can also be used at full opacity for a solid Playing indicator.

The algorithm favours a representative image colour family for Background, prefers light body text unless the selected surface requires dark text, and uses independent image colour families for interaction/state accents when suitable candidates are available.

Returns an empty object {} if a colour scheme cannot be generated.

Parameters

NameTypeDescription
paletteSize = 15optionalnumber

Palette size. Number of K-means++ colour centroids used as the input palette.

Returns

string —

JSON object string. Contains backgroundColor, textColor, playingBackgroundColor, focusFrameColor and selectionBackgroundColor.

Example

const colours = JSON.parse(image.GetThemeColourSchemeJSON());

if (Object.keys(colours).length) {
    const playingIndicatorColor =
        0xFF000000 | (colours.playingBackgroundColor & 0x00FFFFFF);

    console.log(colours.backgroundColor);
    console.log(colours.textColor);
    console.log(colours.playingBackgroundColor);
    console.log(playingIndicatorColor);
    console.log(colours.focusFrameColor);
    console.log(colours.selectionBackgroundColor);
}

Example source

methodWORKER

InvertColours

js/d2d.js:201
InvertColours()

Inverts the colours in a bitmap, to create a negative image. i.e. White becomes black, black becomes white, etc.

Returns

D2DBitmap
methodWORKER

ReleaseGraphics

js/d2d.js:209
ReleaseGraphics(gr)

Parameters

NameTypeDescription
grD2DGraphics

Returns

number
methodWORKER

Resize

js/d2d.js:216
Resize(w, h, mode)

Parameters

NameTypeDescription
wnumber
hnumber
mode = 0optionalnumber

See InterpolationMode

Returns

D2DBitmap
methodWORKER

ResizeIndirect

js/d2d.js:225
ResizeIndirect(w, h, mode)

Resizes image. Unlike D2DBitmap#Resize changes the current object

Parameters

NameTypeDescription
wnumber
hnumber
mode = 0optionalnumber

See InterpolationMode

sharpness = 0.0optionalnumber
border_hard = falseoptionalnumber
cx = 0optionalnumber

Scale center point x

cy = 0optionalnumber

Scale center point y

methodWORKER

RotateFlip

js/d2d.js:238
RotateFlip(mode)

Changes will be saved in the current bitmap.

Parameters

NameTypeDescription
modenumber

See RotateFlipType

methodWORKER

SaveAs

js/d2d.js:246
SaveAs(path, format)

Saves the bitmap to an image file.

Relative filesystem paths are resolved as described in utils.ReadTextFile.

Parameters

NameTypeDescription
pathstring

Absolute or script-relative file path including file extension. The parent folder must already exist.

format = 'image/png'optionalstring

"image/png"
"image/bmp"
"image/jpeg"
"image/gif"
"image/tiff"

Returns

boolean

Example

let img = utils.GetAlbumArtEmbedded(fb.GetFocusItem().RawPath, 0);
if (img) {
    img.SaveAs("D:\\export.jpg", "image/jpeg");
}
methodWORKER

StackBlur

js/d2d.js:269
StackBlur(radius)

Changes will be saved in the current bitmap.

Parameters

NameTypeDescription
radiusnumber

Valid values 2-254.

Examples

<caption>Blur image<caption>
// `samples/basic/StackBlur (image).js`
<caption>Blur text<caption>
// `samples/basic/StackBlur (text).js`