颜色转换器
在HEX、RGB、HSL和HSV格式之间转换颜色,内置可视化选色器
点击色块打开选色器
或在下方直接编辑任意值
什么是颜色格式转换?
颜色转换器将颜色值从一种表示法转换为另一种——例如,从HEX三元组(#6366F1)转换为RGB元组(99, 102, 241)或HSL三元组(239, 84%, 67%)。同一颜色也可以表示为HSV三元组(239, 59%, 95%)。每种格式使用不同的数学模型对同一视觉颜色进行编码;不同的工具、语言和API根据使用场景需要不同的格式。
HEX和RGB都将颜色描述为红、绿、蓝光的混合。HEX是同一组三个0–255通道值的紧凑十六进制表示,而RGB以十进制展开这些值。HSL(色相、饱和度、亮度)和HSV(色相、饱和度、明度)将同样的信息重新组织为柱坐标系:色相是色轮上的角度(0–360度),饱和度和亮度/明度以百分比表示。这使得HSL和HSV在创建同一色相的较浅或较深色调时更直观。
这些格式之间的转换需要明确的数学公式。W3C CSS Color Module Level 4规范记录了浏览器将任何CSS颜色解析为sRGB三元组所用的算法。这些公式是确定性的:给定输入始终产生相同的输出,因此只要数值未被四舍五入或截断,转换就是无损的。大多数选色器和设计工具在底层依赖同样的公式。
为何使用此颜色转换器?
设计工具导出一种格式的颜色,CSS需要另一种格式,而你调用的API又期望第三种格式。无需编写转换代码或搜索正确的公式,粘贴一个值即可同时获取所有格式。
颜色转换器使用场景
颜色格式参考
下表列出了五种最常见的颜色格式,每种格式均以相同的靛蓝色(#6366F1)为例。HEX和RGB在CSS和JavaScript中支持最为广泛。HSL因调整亮度和饱和度方便而更适合主题配置。HSV是大多数图形设计软件选色器所用的模型。
| 格式 | 示例(靛蓝色) | 通道 | 常见用途 |
|---|---|---|---|
| HEX | #6366F1 | 6 (or 3/8) | CSS, design tools, brand guides |
| RGB | rgb(99, 102, 241) | 3 (0–255 each) | CSS, Canvas API, image processing |
| HSL | hsl(239, 84%, 67%) | 3 (deg, %, %) | CSS, color theming, accessibility tuning |
| HSV | hsv(239, 59%, 95%) | 3 (deg, %, %) | Color pickers, Photoshop, Figma |
| CMYK | cmyk(59%, 58%, 0%, 5%) | 4 (0–100% each) | Print design, prepress workflows |
HEX vs RGB vs HSL
三种格式均描述相同的sRGB色彩空间,区别在于数据的呈现方式,这影响了在不同场景下的可读性和操作便捷性。
代码示例
在常见语言和环境中实现HEX、RGB和HSL之间的颜色转换。以下示例均使用相同的靛蓝色(#6366F1)便于对比。
// HEX → RGB
function hexToRgb(hex) {
const n = parseInt(hex.replace('#', ''), 16)
return [(n >> 16) & 255, (n >> 8) & 255, n & 255]
}
hexToRgb('#6366f1') // → [99, 102, 241]
// RGB → HSL
function rgbToHsl(r, g, b) {
r /= 255; g /= 255; b /= 255
const max = Math.max(r, g, b), min = Math.min(r, g, b)
const l = (max + min) / 2
if (max === min) return [0, 0, Math.round(l * 100)]
const d = max - min
const s = l > 0.5 ? d / (2 - max - min) : d / (max + min)
let h = 0
if (max === r) h = ((g - b) / d + (g < b ? 6 : 0)) / 6
else if (max === g) h = ((b - r) / d + 2) / 6
else h = ((r - g) / d + 4) / 6
return [Math.round(h * 360), Math.round(s * 100), Math.round(l * 100)]
}
rgbToHsl(99, 102, 241) // → [239, 84, 67]import colorsys
# HEX → RGB
def hex_to_rgb(hex_str: str) -> tuple[int, int, int]:
h = hex_str.lstrip('#')
return tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
hex_to_rgb('#6366f1') # → (99, 102, 241)
# RGB → HSL (colorsys uses HLS order)
r, g, b = 99 / 255, 102 / 255, 241 / 255
h, l, s = colorsys.rgb_to_hls(r, g, b)
print(f"hsl({h * 360:.0f}, {s * 100:.0f}%, {l * 100:.0f}%)")
# → hsl(239, 84%, 67%)
# RGB → HSV
h, s, v = colorsys.rgb_to_hsv(r, g, b)
print(f"hsv({h * 360:.0f}, {s * 100:.0f}%, {v * 100:.0f}%)")
# → hsv(239, 59%, 95%)package main
import (
"fmt"
"math"
)
func hexToRGB(hex string) (int, int, int) {
var r, g, b int
fmt.Sscanf(hex, "#%02x%02x%02x", &r, &g, &b)
return r, g, b
}
func rgbToHSL(r, g, b int) (int, int, int) {
rf, gf, bf := float64(r)/255, float64(g)/255, float64(b)/255
max := math.Max(rf, math.Max(gf, bf))
min := math.Min(rf, math.Min(gf, bf))
l := (max + min) / 2
if max == min {
return 0, 0, int(math.Round(l * 100))
}
d := max - min
s := d / (2 - max - min)
if l <= 0.5 {
s = d / (max + min)
}
var h float64
switch max {
case rf: h = (gf - bf) / d; if gf < bf { h += 6 }
case gf: h = (bf - rf) / d + 2
case bf: h = (rf - gf) / d + 4
}
return int(math.Round(h / 6 * 360)), int(math.Round(s * 100)), int(math.Round(l * 100))
}
func main() {
r, g, b := hexToRGB("#6366f1")
fmt.Println(r, g, b) // → 99 102 241
fmt.Println(rgbToHSL(r, g, b)) // → 239 84 67
}/* Modern CSS supports multiple color formats natively */
/* HEX notation */
.button { color: #6366f1; }
/* RGB functional notation */
.button { color: rgb(99 102 241); }
/* HSL — easier to adjust lightness and saturation */
.button { color: hsl(239 84% 67%); }
/* CSS Color Level 4: relative color syntax (Chrome 119+) */
.button-light { color: hsl(from #6366f1 h s calc(l + 20%)); }
/* color-mix() for tinting without manual conversion */
.button-muted { color: color-mix(in srgb, #6366f1 70%, white); }