openharmony 鸿蒙 arkts-apis-graphics-drawing-ColorFilter

2026-08-25 浏览 (1)

Class (ColorFilter)

Defines a color filter.

NOTE

  • The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • This module uses the physical pixel unit, px.

  • This module operates under a single-threaded model. The caller needs to manage thread safety and context state transitions.

Modules to Import

import { drawing } from '@kit.ArkGraphics2D';

createBlendModeColorFilter

createBlendModeColorFilter(color: common2D.Color, mode: BlendMode) : ColorFilter

Creates a ColorFilter object with a given color and blend mode.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
colorcommon2D.ColorYesColor in ARGB format. The value of each color channel is an integer ranging from 0 to 255.
modeBlendModeYesBlend mode.

Returns

TypeDescription
ColorFilterColor filter.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed.

Example

import { common2D, drawing } from '@kit.ArkGraphics2D';

const color : common2D.Color = { alpha: 255, red: 255, green: 0, blue: 0 };
let colorFilter = drawing.ColorFilter.createBlendModeColorFilter(color, drawing.BlendMode.SRC);

createBlendModeColorFilter18+

static createBlendModeColorFilter(color: common2D.Color|number, mode: BlendMode) : ColorFilter

Creates a ColorFilter object with a given color and blend mode.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
colorcommon2D.Color |numberYesColor, represented by an unsigned integer in hexadecimal ARGB format.
modeBlendModeYesBlend mode.

Returns

TypeDescription
ColorFilterColor filter.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed.

Example

import { drawing } from '@kit.ArkGraphics2D';

let colorFilter = drawing.ColorFilter.createBlendModeColorFilter(0xffff0000, drawing.BlendMode.SRC);

createComposeColorFilter

createComposeColorFilter(outer: ColorFilter, inner: ColorFilter) : ColorFilter

Creates a ColorFilter object by combining another two color filters.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
outerColorFilterYesColor filter that takes effect later in the new filter.
innerColorFilterYesColor filter that takes effect first in the new filter.

Returns

TypeDescription
ColorFilterColor filter.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types.

Example

import { common2D, drawing } from '@kit.ArkGraphics2D';

const color : common2D.Color = { alpha: 255, red: 255, green: 0, blue: 0 };
let colorFilter1 = drawing.ColorFilter.createBlendModeColorFilter(color, drawing.BlendMode.SRC);
let colorFilter2 = drawing.ColorFilter.createBlendModeColorFilter(color, drawing.BlendMode.DST);
let colorFilter = drawing.ColorFilter.createComposeColorFilter(colorFilter1, colorFilter2);

createLinearToSRGBGamma

createLinearToSRGBGamma() : ColorFilter

Creates a ColorFilter object that applies the sRGB gamma curve to the RGB channels.

System capability: SystemCapability.Graphics.Drawing

Returns

TypeDescription
ColorFilterColor filter.

Example

import { drawing } from '@kit.ArkGraphics2D';

let colorFilter = drawing.ColorFilter.createLinearToSRGBGamma();

createSRGBGammaToLinear

createSRGBGammaToLinear() : ColorFilter

Creates a ColorFilter object that applies the RGB channels to the sRGB gamma curve.

System capability: SystemCapability.Graphics.Drawing

Returns

TypeDescription
ColorFilterColor filter.

Example

import { drawing } from '@kit.ArkGraphics2D';

let colorFilter = drawing.ColorFilter.createSRGBGammaToLinear();

createLumaColorFilter

createLumaColorFilter() : ColorFilter

Creates a ColorFilter object that multiplies the luma into the alpha channel and sets the RGB channels to zero.

System capability: SystemCapability.Graphics.Drawing

Returns

TypeDescription
ColorFilterColor filter.

Example

import { drawing } from '@kit.ArkGraphics2D';

let colorFilter = drawing.ColorFilter.createLumaColorFilter();

createMatrixColorFilter12+

static createMatrixColorFilter(matrix: Array<number>): ColorFilter

Creates a color filter object with a 4*5 color matrix.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
matrixArray<number>YesAn array of 20 numbers, indicating the 4*5 matrix.

Returns

TypeDescription
ColorFilterColor filter.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed.

Example

import { drawing } from '@kit.ArkGraphics2D';

let matrix: Array<number> = [
  1, 0, 0, 0, 0,
  0, 1, 0, 0, 0,
  0, 0, 100, 0, 0,
  0, 0, 0, 1, 0
];
let colorFilter = drawing.ColorFilter.createMatrixColorFilter(matrix);

createLightingColorFilter20+

static createLightingColorFilter(mutColor: common2D.Color|number, addColor: common2D.Color|number): ColorFilter

Creates a lighting color filter. It multiplies the RGB channel values by one color and then adds another color value. The final output stays between 0 and 255.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
mutColorcommon2D.Color |numberYesColor used for multiplication. The value is in the ARGB format, and each color channel is an integer ranging from 0 to 255. If the value is of the number type, it must be an unsigned integer in the hexadecimal ARGB format.
addColorcommon2D.Color |numberYesColor used for addition. The value is in the ARGB format, and each color channel is an integer ranging from 0 to 255. If the value is of the number type, it must be an unsigned integer in the hexadecimal ARGB format.

Returns

TypeDescription
ColorFilterColorFilter object created.

Example

import { common2D, drawing } from '@kit.ArkGraphics2D';
let mulColor : common2D.Color = { alpha: 0, red: 0, green: 0, blue: 20 };
let addColor : common2D.Color = { alpha: 0, red: 0, green: 0, blue: 125 };
let colorFilter = drawing.ColorFilter.createLightingColorFilter(mulColor, addColor);

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-drawing-gpu-context-h

openharmony 鸿蒙 capi-drawing-nativepixelmap-

openharmony 鸿蒙 capi-nativevsync-oh-nativevsync-expectedraterange

openharmony 鸿蒙 capi-drawing-oh-drawing-brush

openharmony 鸿蒙 capi-drawing-oh-drawing-image-info

openharmony 鸿蒙 capi-drawing-text-linetypography-h

openharmony 鸿蒙 capi-oh-nativeimage-oh-nativeimage

openharmony 鸿蒙 capi-drawing-typeface-h

openharmony 鸿蒙 capi-drawing-brush-h

openharmony 鸿蒙 capi-nativewindow

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/MTT61S7e