Skip to content

rgbaToHex

RGBA 转 Hex

引入版本

0.55.0

Demo

ts
import { rgbaToHex } from '@wyfex/iutils'

console.log('==== RGB 不带透明度 ====')
console.log(rgbaToHex(255, 0, 0)) // #ff0000
console.log(rgbaToHex(0, 255, 0)) // #00ff00
console.log(rgbaToHex(0, 0, 0)) // #000000
console.log(rgbaToHex(255, 0, 127)) //#ff007f

console.log('\n==== 带透明度 0~1 小数 ====')
console.log(rgbaToHex(255, 0, 0, 1)) // #ff0000ff
console.log(rgbaToHex(255, 0, 0, 0.5)) // #ff000080
console.log(rgbaToHex(255, 0, 0, 0)) // #ff000000

console.log('\n==== 边界数值自动钳位 ====')
console.log(rgbaToHex(300, -20, 999, 1.5)) // #ff00ff
console.log(rgbaToHex(100, 100, 100, -0.2)) // #64646400

console.log('\n==== 浮点数rgb兼容 ====')
console.log(rgbaToHex(122.6, 45.1, 78.9)) // #7a2d4e

console.log('\n==== 字符串形式数字 ====')
console.log(rgbaToHex('100', '150', '200')) // #6496c8

基于 MIT 许可发布