ColorPickerPanel 取色面板
把 ColorPicker 的取色面板直接嵌入页面,不显示弹出触发器,颜色修改会立即写回。
何时使用
- 需要让取色器始终可见,例如主题编辑器、绘图属性或设计面板。
- 需要同时展示色相、饱和度/明度、透明度、预设色和文本输入时。
- 只需要点击色块后再编辑时,用 ColorPicker 颜色选择器;只显示颜色结果时用普通文本或色块。
- 需要编辑渐变时使用
ColorPickerPanelGradient,而不是把渐变当成一个单色值。
在线体验
基本用法
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/theme"
)
var accent = theme.Hex("#1677ff")
func page(c *ui.Context) {
ui.ColorPickerPanel(c, &accent)
}面板绑定 *theme.Color;在所有设计系统中都即时写回,没有确认按钮。
常见用法
预设色与格式
Presets 显示色板,PresetGroups 可按标题分组;Format 控制文本输入格式,支持 hex、rgb、hsb 和 hsv。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/theme"
)
var brand = theme.Hex("#409eff")
var presets = []theme.Color{theme.Hex("#1677ff"), theme.Hex("#67c23a"), theme.Hex("#f56c6c")}
func page(c *ui.Context) {
ui.ColorPickerPanel(c, &brand).
Presets(presets...).PresetLabel("品牌色").Format("rgb").NoAlpha()
}分组预设与清除
ColorPresetGroup.Closed 可让 Ant Design 或 shadcn 主题的分组初始折叠;AllowClear 增加无色状态。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/theme"
)
var surface = theme.Hex("#ffffff")
func page(c *ui.Context) {
ui.ColorPickerPanel(c, &surface).
PresetGroups(
ui.ColorPresetGroup{Label: "品牌色", Colors: []theme.Color{theme.Hex("#1677ff"), theme.Hex("#722ed1")}},
ui.ColorPresetGroup{Label: "状态色", Colors: []theme.Color{theme.Hex("#52c41a"), theme.Hex("#f5222d")}, Closed: true},
).
AllowClear()
}线性渐变
ColorPickerPanelGradient 绑定 *theme.Gradient,面板上方显示色标和角度,下面编辑当前色标的颜色。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/theme"
)
var background = theme.Gradient{Angle: 90, Stops: []theme.GradientStop{
{Offset: 0, Color: theme.Hex("#1677ff")},
{Offset: 1, Color: theme.Hex("#722ed1")},
}}
func page(c *ui.Context) {
ui.ColorPickerPanelGradient(c, &background).NoAlpha()
}边框、禁用与变化通知
Border(false) 适合已经放在卡片中的面板;Disabled(true) 保留内容但阻止编辑。Changed 在本帧颜色写回后返回 true。
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/theme"
)
var chartColor = theme.Hex("#13c2c2")
func page(c *ui.Context) {
panel := ui.ColorPickerPanel(c, &chartColor).Border(false)
if panel.Changed() {
ui.Message(c, "success", "图表颜色已更新")
}
}主题与样式
- 面板使用
theme.Components.ColorPicker的ColorPickerToken,包括PanelWidth、PaletteHeight、BarHeight、HandleSize和SwatchSize。 - Ant Design、Element Plus、shadcn 会分别调整面板圆角、输入区、预设色分组和边框;
Border只控制外层边框。 ConfigProvider.Defaults("ColorPicker", ...)的size、allowClear、disabledAlpha、format默认值也会作用于面板。
注意事项
theme.Color和theme.Gradient必须放在稳定状态中;颜色零值表示无色,不等于黑色。- 面板没有取消操作,若业务需要撤销,请在打开或编辑前保存旧值并自行恢复。
- 渐变应至少保留两个色标;面板会把角度、色标位置和颜色写回同一个
theme.Gradient。 - 面板本身没有触发器标签;放在表单项中使用表单标签,独立使用时给相关控件或容器提供可理解的无障碍名称。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.ColorPickerPanel
func ColorPickerPanel(c *Context, val *theme.Color) *ColorPickerPanelWColorPickerPanel adds an inline color panel bound to *val; changes apply immediately in every design system (there is no confirm step).
ui.ColorPickerPanel(c, &col).Presets(theme.Hex("#409eff"), theme.Hex("#67c23a"))
返回 *ColorPickerPanelW,可链式调用的方法见 ColorPickerPanelW 方法。
ColorPickerPanelW 方法
ui.ColorPickerPanelW:ColorPickerPanelW is an inline color panel bound to a color.
| 方法 | 说明 | 签名 |
|---|---|---|
AllowClear | AllowClear adds the "no color" control (Ant). | AllowClear() *ColorPickerPanelW |
Border | Border toggles the panel border (default on). | Border(on bool) *ColorPickerPanelW |
Changed | Changed reports a new color this frame. | Changed() bool |
Disabled | Disabled turns the panel off. | Disabled(on bool) *ColorPickerPanelW |
DisabledFormat | DisabledFormat leaves the format select out (Ant disabledFormat). | DisabledFormat(on bool) *ColorPickerPanelW |
Format | Format sets the value text format: "hex" (default), "rgb", "hsb" or "hsv". | Format(f string) *ColorPickerPanelW |
ID | ID returns the panel's ID. | ID() ID |
NoAlpha | NoAlpha hides the alpha bar and input. | NoAlpha() *ColorPickerPanelW |
PresetGroups | PresetGroups shows several titled preset groups (see ColorPickerW.PresetGroups). | PresetGroups(groups ...ColorPresetGroup) *ColorPickerPanelW |
PresetLabel | PresetLabel names the preset group (Ant). | PresetLabel(s string) *ColorPickerPanelW |
Presets | Presets adds preset swatches (Element predefine, Ant presets). | Presets(cols ...theme.Color) *ColorPickerPanelW |