ColorUtils

Last Updated on : 2024-06-05 03:14:35download

This topic describes the utility methods that are used to convert color values.

Convert from HSV to RGB

API operation name

hsvToRgb

API description

Convert hue, saturation, value (HSV) color values to red, green, blue (RGB) color values.

Request parameter

Name Data type Description Required
h number The hue of a color. Valid values: to 360°. Yes
s number The saturation of a color. Valid values: 0% to 100%. Yes
v number The value of a color. Valid values: 0% to 100%. Yes

Return parameter

Name Data type Description
rgb { r: number, g: number, b: number} The RGB color values.

Sample request

import { Utils } from "tuya-panel-kit"; const { hsvToRgb } = Utils.ColorUtils; hsvToRgb( h, s, v ); // Example: hsvToRgb(0, 1, 1);

Sample response

{ r: 255, g: 0, b: 0 }

Convert from RGB to HSV

API operation name

rgbToHsv

API description

Convert RGB color values to HSV color values.

Request parameter

Name Data type Description Required
r number The red value of a color. Valid values: 0 to 255. Yes
g number The green value of a color. Valid values: 0 to 255. Yes
b number The blue value of a color. Valid values: 0 to 255. Yes

Return parameter

Name Data type Description
hsv { h: number, s: number, v: number} The HSV color values.

Sample request

import { Utils } from "tuya-panel-kit"; const { rgbToHsv } = Utils.ColorUtils; rgbToHsv(r, g, b); // Example: rgbToHsv(255, 0, 0); rgbToHsv(128, 1, 0);

Sample response

{h: 0, s: 1, v: 1} {h: 0, s: 1, v: 0.5019607843137255}

Convert from HEX to HSV

API operation name

hex2hsv

API description

Convert hexadecimal color values to HSV color values.

Request parameter

Name Data type Description Required
hex string The hexadecimal color values. Yes

Return parameter

Name Data type Description
hsv number[] The HSV color values.

Sample request

import { Utils } from "tuya-panel-kit"; const { color } = Utils.ColorUtils; color.hex2hsv(hex); // Example: color.hex2hsv('#FF00FF');

Sample response

[ 300, 100, 100 ]

Convert from RGB to HEX

API operation name

rgb2hex

API description

Convert RGB color values to hexadecimal color values.

Request parameter

Name Data type Description Required
r number The red value of a color. Valid values: 0 to 255. Yes
g number The green value of a color. Valid values: 0 to 255. Yes
b number The blue value of a color. Valid values: 0 to 255. Yes

Return parameter

Name Data type Description
hex string The hexadecimal color values.

Sample request

import { Utils } from "tuya-panel-kit"; const { color } = Utils.ColorUtils; color.rgb2hex(r, g, b); // Example: color.rgb2hex(255, 255, 255);

Sample response

#FFFFFF

Convert from HEX to RGBA

API operation name

hex2RgbString

API description

Convert hexadecimal color values to red, green, blue, alpha (RGBA) color values.

Request parameter

Name Data type Description Required
hex string The hexadecimal color values. Yes
alpha number The opacity of a color. Yes

Return parameter

Name Data type Description
rgba { r: number, g: number, b: number, a: number} The red, green, blue, alpha (RGBA) color values.

Sample request

import { Utils } from "tuya-panel-kit"; const { color } = Utils.ColorUtils; color.hex2RgbString(hex, alpha); // Example: color.hex2RgbString('#FF00FF', 1);

Sample response

rgba(255, 0, 255, 1)

Convert from HSV to RGBA

API operation name

hsv2RgbString

API description

Convert HSV color values to RGBA color values.

Request parameter

Name Data type Description Required
h number The hue of a color. Valid values: to 360°. Yes
s number The saturation of a color. Valid values: 0% to 100%. Yes
v number The value of a color. Valid values: 0% to 100%. Yes
alpha number The opacity of a color. Yes

Return parameter

Name Data type Description
rgba { r: number, g: number, b: number, a: number} The red, green, blue, alpha (RGBA) color values.

Sample request

import { Utils } from "tuya-panel-kit"; const { color } = Utils.ColorUtils; color.hsv2RgbString(h, s, v, alpha); // Example: color.hex2RgbString(231, 231, 231, 1);

Sample response

rgba(0, 38, 255, 1)