Last Updated on : 2024-06-05 03:14:35download
RatioUtils
provides a quick way to adapt to the screen aspect ratio.
Property name | Description |
---|---|
hRatio | Horizontal ratio of the screen, which is the width ratio of the current screen to ip6 screen. |
vRatio | Vertical ratio of the screen, which is the height ratio of the current screen to ip6 screen. |
width, viewWidth | Width of the current screen. |
height | Height of the current screen. |
ratio | Diagonal line ratio, which is the ratio of diagonal line length of the current screen to diagonal line length of ip6 screen. |
convertX | Use hRatio to convert the length. |
convertY | Use vRatio to convert the length. |
convert | Use ratio to convert the length. |
isIos | Whether it is an iOS system. |
isIphoneX,iPhoneX | Whether it belongs to the iPhoneX series (including iPhone XS and iPhone XS Max). |
import { Utils } from "tuya-panel-kit";
const { convertX } = Utils.RatioUtils;
// The width of a component on the design draft is 13
width = convertX(13); // The final width of the component on the current device
ColorUtils
is a tool for color conversion.
/**
* @param {Number} h - Hue, ranging from 0° to 360°
* @param {Number} s - Saturation, ranging from 0% to 100%
* @param {Number} v - Value, ranging from 0% to 100%
* returns {Object} RGB color value
*/
hsvToRgb(0, 1, 1); // {r: 255, g: 0, b: 0}
/**
* @param {Number} r - red value, ranging from 0 to 255
* @param {Number} g - green value, ranging from 0 to 255
* @param {Number} b - blue value, ranging from 0 to 255
* returns {Object} hsv value
*/
rgbToHsv(255, 0, 0); // {h: 0, s: 1, v: 1}
rgbToHsv(128, 1, 0); // {h: 0, s: 1, v: 0.5019607843137255}
/**
* @param {Number} h - Hue, ranging from 0° to 360°
* @param {Number} s - Saturation, ranging from 0% to 100%
* @param {Number} v - Lightness, ranging from 0% to 100%
* returns {Object} RGB color value
*/
hslToRgb(0, 1, 0.5); // {r: 255, g: 0, b: 0}
hslToRgb(120, 1, 0.75); // {r: 128, g: 255, b: 128}
hslToRgb(240, 1, 0.25); // {r: 0, g: 0, b: 128}
/**
* @param {Number} rr - red value, ranging from 0 to 255
* @param {Number} gg - green value, ranging from 0 to 255
* @param {Number} bb - blue value, ranging from 0 to 255
* returns {Object} HSL color value
*/
rgbToHsl(255, 0, 0); // {h: 0, s: 1, l: 0.5}
rgbToHsl(128, 255, 128); // {h: 120, s: 1, l: 0.7509803921568627}
rgbToHsl(0, 0, 128); // {h: 240, s: 1, l: 0.25098039215686274}
Number related tools.
/**
* @param {Number} num
* @param {Number} Length of the return string
* returns {String}
*/
toFixedString(111, 5); // '00111'
/**
* @param {Number} num
* @param {Number} Length of the return string
* returns {String}
*/
toFixedString(111, 5); // '00111'
/**
* @param {Number} num
* @param {Number} idx idx is reverse, starting from 0
* returns {Number}
*/
// 17 = 10001(2)
getBitValue(17, 0); // 1
getBitValue(17, 1); // 0
getBitValue(17, 4); // 1
/**
* @param {Number} idx idx is reverse, starting from 0
* returns {Number}
*/
// 17 = 10001(2)
setBitValueWithOne(17, 0); // 17, 10001
setBitValueWithOne(17, 1); // 19, 10010
setBitValueWithOne(17, 5); // 49, 110001
/**
* @param {Number} idx idx is reverse, starting from 0
* returns {Number}
*/
// 17 = 10001(2)
setBitValueWithZero(17, 0); // 16, 10000
setBitValueWithZero(17, 1); // 17, 10001
setBitValueWithZero(17, 4); // 1, 00001
/**
* @param {Array} byte, byte is a number array with 8-bit integers.
* returns {String}
*/
bytesToHexString([1, 2]); // '0102'
bytesToHexString([23, 2]); // '1702'
/**
* @param {Number} The converted string, and each character is hexadecimal
* returns {String}
*/
numToHexString(111); // '6f'
numToHexString(15); // '0f'
/**
* @param {Number} number
* returns {String}
*/
numToByteNumbers(111); // [0, 111]
numToHexString(1040001); // [15, 222, 129]
/**
* @param {Number} A high 8-bit number
* @param {Number} A low 8-bit number
* returns {Number}
*/
highLowToInt(11, 22); // 2838
highLowToInt(22, 11); // 5643
/**
* @param {Number} number
* returns {Number} An array of numbers. The first is the high 8-bit number, and the second is the low 8-bit number
*/
highLowToInt(2838); // [11, 22]
highLowToInt(5643); // [22, 11]
/**
* @param {Number} value
* @param {Number} value
* @param {Number} value
* returns {Number} Returns the middle value of the three numbers
*/
inMaxMin(2838, 1, 233); // 233
inMaxMin(1, 2, 0); // 1
/**
* @param {Number} value A number with 10 times scale
* @param {Number} value
* returns {Number}
*/
scaleNumber(2, 10245); // 102.45
/**
* @param {Number} start The initial value
* @param {Number} end The end value
* @param {Number} value Step length
* returns (Array) returns an array from start to end with step length
*/
range(1, 10); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
range(1, 10, 2); // [1, 3, 5, 7, 9]
/**
* @param {Number} value The original value
* @param {Number} min The original minimum range
* @param {Number} max The original maximum range
* @param {Number} newMin The new minimum range
* @param {Number} newMax The new maximum range
* returns {Number} Convert the value in the original range to the value in the new range
*/
calcPosition(50, 0, 100, -100, 0); // -50
calcPosition(255, 0, 255, 0, 100); // 100
/**
* @param {Number} min
* @param {Number} max
* @param {Number} value The value
* returns (Number) offset The percentage start offset (0–1)
*/
calcPercent(25, 255, 25); // 0
calcPercent(25, 255, 25, 0.1); // 0.1
Time related tools.
/**
* @param {Number} t The number based on seconds
* @param {Number} n is a number, which means the length of the string is fixed. The default value is 2
* returns {Array} is an array of strings. Each item is a string of length n
*/
parseSecond(111); // ['00', '01', '51']
parseSecond(3333333); // ['25', '55', '33']
/**
* @param {Number} t The number based on seconds
* returns {String}
*/
parseHour12(111); // '01:55 AM'
parseHour12(3333333); // '12:01 PM'
/**
* @param {String} timeStr Time point
* returns {String} The number converted from timeStr
*/
stringToSecond("11:30"); // 690
stringToSecond("22:11:30"); // 79890
/**
* @param {String} dateStr Time point
* returns {String} The number converted from dateStr
*/
stringToSecond("11:30"); // 690
stringToSecond("22:11:30"); // 79890
/**
* @param {String} month (M), day (d), hour (h), minute (m), second (s), and quarter (q) can use 1–2 placeholders; year (y) can use 1–4 placeholders; and there is only 1 placeholder for milliseconds (S) (1 to 3-digit number).
* returns {String} The extension of Date, which converts Date to String in the specified format
*/
new Date().Format("yyyy-MM-dd hh:mm:ss.S"); // 2006-07-02 08:09:04.423
new Date().Format("yyyy-M-d h:m:s.S"); // 2006-7-2 8:9:4.18
/**
* @desc is an extension of Date, which is used to get the current time zone
* returns {String} Current time zone
*/
timezone(); // +08:00
String tools.
/**
* @param {String} bits, bits is a hexadecimal string
* returns {Array} Array of numbers converted from the hexadecimal string
*/
hexStringToNumber("AD03"); // [173, 3]
/**
* @param {String} str, str is a string. It contains "0" and "1", and can contain other characters
* returns {String} The string converted from str. Each character is hexadecimal
*/
hexStringToBinString("A7B9"); // 1010011110111001
hexStringToBinString("0709"); // 0000011100001001
/**
* @param {String} str, str is a string
* returns {String} Convert the midline and underline to camel case
*/
camelize("abb_aa-cc"); // abbAaCc
Core tools.
/**
* @param {String} str, str is a string
* @param {Number} str, count is number, which returns the length of the string
* returns {String}
*/
toFixed("111", 5); // '00111'
toFixed("3456111", 5); // '56111'
/**
* @param {String} str, str is a string
* @param {Number} str, count is number, which returns the length of the string
* returns {String}
*/
toFilled("111", 5); // '00111'
toFilled("3456111", 5); // '3456111'
/**
* @param {String} str, str is a string
* @param {Number} str, count is number, which returns the length of the string
* returns {Array} An array whose item is a substring and its length is a block
*/
partition("1234567", 3); // ['123', '456', '7']
/**
* @desc Determine whether it is object
* @param {Any} Any property
* returns {Boolean}
*/
/**
* @desc Determine whether it is array
* @param {Any} Any property
* returns {Boolean}
*/
/**
* @desc Determine whether it is a date
* @param {Any} Any property
* returns {Boolean}
*/
/**
* @desc Determine whether it is regexp
* @param {Any} Any property
* returns {Boolean}
*/
/**
* @desc Determine whether it is boolean
* @param {Any} Any property
* returns {Boolean}
*/
/**
* @desc Determine whether it is number
* @param {Any} Any property
* returns {Boolean}
*/
Temperature-related tools, and the value is rounded.
/**
* @param {Number} Fahrenheit degree
* returns {Number} Fahrenheit to Celsius conversion
*/
f2c(100); // 38
/**
* @param {Number} Celsius degree
* returns {Number} Celsius to Fahrenheit conversion
*/
c2f(100); // 212
JSON related tools.
/**
* @param {String} str, str is a string
* returns {Object} Parse the JSON string
*/
parseJSON("{a:1, b:2}"); // {a: 1, b: 2}
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback