2024-06-22 13:44:24 -06:00
|
|
|
package objects
|
|
|
|
|
|
|
|
import "image/color"
|
|
|
|
import "git.tebibyte.media/tomo/tomo"
|
|
|
|
import "git.tebibyte.media/tomo/tomo/input"
|
|
|
|
import "git.tebibyte.media/tomo/tomo/event"
|
|
|
|
import "git.tebibyte.media/tomo/tomo/canvas"
|
|
|
|
import "git.tebibyte.media/tomo/objects/layouts"
|
2024-09-05 20:46:58 -06:00
|
|
|
import icolor "git.tebibyte.media/tomo/objects/internal/color"
|
2024-06-22 13:44:24 -06:00
|
|
|
|
2024-08-24 17:28:48 -06:00
|
|
|
var _ tomo.Object = new(HSVAColorPicker)
|
|
|
|
|
2024-08-15 15:05:19 -06:00
|
|
|
// HSVAColorPicker allows the user to pick a color by controlling its HSVA
|
2024-06-22 13:44:24 -06:00
|
|
|
// parameters.
|
2024-08-25 00:36:05 -06:00
|
|
|
//
|
|
|
|
// Sub-components:
|
|
|
|
// - ColorPickerMap is a recangular control where the X axis controls
|
|
|
|
// saturation and the Y axis controls value.
|
2024-08-15 15:05:19 -06:00
|
|
|
type HSVAColorPicker struct {
|
2024-08-24 17:28:48 -06:00
|
|
|
box tomo.ContainerBox
|
2024-09-05 20:46:58 -06:00
|
|
|
value icolor.HSVA
|
2024-06-22 13:44:24 -06:00
|
|
|
|
2024-08-15 15:05:19 -06:00
|
|
|
pickerMap *hsvaColorPickerMap
|
2024-06-22 13:44:24 -06:00
|
|
|
hueSlider *Slider
|
|
|
|
alphaSlider *Slider
|
|
|
|
|
|
|
|
on struct {
|
|
|
|
valueChange event.FuncBroadcaster
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-15 15:05:19 -06:00
|
|
|
// NewHSVAColorPicker creates a new color picker with the specified color.
|
|
|
|
func NewHSVAColorPicker (value color.Color) *HSVAColorPicker {
|
|
|
|
picker := &HSVAColorPicker {
|
2024-08-24 17:28:48 -06:00
|
|
|
box: tomo.NewContainerBox(),
|
2024-06-22 13:44:24 -06:00
|
|
|
}
|
2024-08-24 17:28:48 -06:00
|
|
|
picker.box.SetRole(tomo.R("objects", "ColorPicker"))
|
|
|
|
picker.box.SetAttr(tomo.ALayout(layouts.Row { true, false, false }))
|
2024-08-15 15:05:19 -06:00
|
|
|
picker.pickerMap = newHsvaColorPickerMap(picker)
|
2024-08-24 17:28:48 -06:00
|
|
|
picker.box.Add(picker.pickerMap)
|
2024-06-22 13:44:24 -06:00
|
|
|
|
|
|
|
picker.hueSlider = NewVerticalSlider(0.0)
|
2024-08-24 17:28:48 -06:00
|
|
|
picker.box.Add(picker.hueSlider)
|
2024-06-27 12:01:14 -06:00
|
|
|
picker.hueSlider.OnValueChange(func () {
|
2024-06-22 13:44:24 -06:00
|
|
|
picker.value.H = picker.hueSlider.Value()
|
|
|
|
picker.on.valueChange.Broadcast()
|
|
|
|
picker.pickerMap.Invalidate()
|
|
|
|
})
|
|
|
|
|
|
|
|
picker.alphaSlider = NewVerticalSlider(0.0)
|
2024-08-24 17:28:48 -06:00
|
|
|
picker.box.Add(picker.alphaSlider)
|
2024-06-27 12:01:14 -06:00
|
|
|
picker.alphaSlider.OnValueChange(func () {
|
2024-08-15 14:42:31 -06:00
|
|
|
picker.value.A = uint16(picker.alphaSlider.Value() * 0xFFFF)
|
2024-06-22 13:44:24 -06:00
|
|
|
picker.on.valueChange.Broadcast()
|
|
|
|
picker.pickerMap.Invalidate()
|
|
|
|
})
|
|
|
|
|
|
|
|
if value == nil { value = color.Transparent }
|
|
|
|
picker.SetValue(value)
|
|
|
|
return picker
|
|
|
|
}
|
|
|
|
|
2024-08-24 17:28:48 -06:00
|
|
|
// GetBox returns the underlying box.
|
|
|
|
func (this *HSVAColorPicker) GetBox () tomo.Box {
|
|
|
|
return this.box
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFocused sets whether or not this color picker has keyboard focus. If set
|
|
|
|
// to true, this method will steal focus away from whichever object currently
|
|
|
|
// has focus.
|
|
|
|
func (this *HSVAColorPicker) SetFocused (focused bool) {
|
|
|
|
this.box.SetFocused(focused)
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:01:14 -06:00
|
|
|
// Value returns the color of the picker.
|
2024-08-15 15:05:19 -06:00
|
|
|
func (this *HSVAColorPicker) Value () color.Color {
|
2024-06-27 12:01:14 -06:00
|
|
|
return this.value
|
|
|
|
}
|
|
|
|
|
2024-06-22 13:44:24 -06:00
|
|
|
// SetValue sets the color of the picker.
|
2024-08-15 15:05:19 -06:00
|
|
|
func (this *HSVAColorPicker) SetValue (value color.Color) {
|
2024-06-22 13:44:24 -06:00
|
|
|
if value == nil { value = color.Transparent }
|
2024-09-05 20:46:58 -06:00
|
|
|
this.value = icolor.HSVAModel.Convert(value).(icolor.HSVA)
|
2024-06-22 13:44:24 -06:00
|
|
|
this.hueSlider.SetValue(this.value.H)
|
2024-08-16 11:50:22 -06:00
|
|
|
this.alphaSlider.SetValue(float64(this.value.A) / 0xFFFF)
|
2024-08-16 13:17:44 -06:00
|
|
|
this.pickerMap.Invalidate()
|
2024-06-22 13:44:24 -06:00
|
|
|
}
|
|
|
|
|
2024-06-27 12:01:14 -06:00
|
|
|
// OnValueChange specifies a function to be called when the user changes the
|
|
|
|
// swatch's color.
|
2024-08-15 15:05:19 -06:00
|
|
|
func (this *HSVAColorPicker) OnValueChange (callback func ()) event.Cookie {
|
2024-06-27 12:01:14 -06:00
|
|
|
return this.on.valueChange.Connect(callback)
|
2024-06-22 13:44:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// RGBA satisfies the color.Color interface
|
2024-08-15 15:05:19 -06:00
|
|
|
func (this *HSVAColorPicker) RGBA () (r, g, b, a uint32) {
|
2024-06-22 13:44:24 -06:00
|
|
|
return this.value.RGBA()
|
|
|
|
}
|
|
|
|
|
2024-08-15 15:05:19 -06:00
|
|
|
type hsvaColorPickerMap struct {
|
2024-06-22 13:44:24 -06:00
|
|
|
tomo.CanvasBox
|
|
|
|
dragging bool
|
2024-08-15 15:05:19 -06:00
|
|
|
parent *HSVAColorPicker
|
2024-06-22 13:44:24 -06:00
|
|
|
}
|
|
|
|
|
2024-08-15 15:05:19 -06:00
|
|
|
func newHsvaColorPickerMap (parent *HSVAColorPicker) *hsvaColorPickerMap {
|
|
|
|
picker := &hsvaColorPickerMap {
|
2024-06-22 13:44:24 -06:00
|
|
|
CanvasBox: tomo.NewCanvasBox(),
|
|
|
|
parent: parent,
|
|
|
|
}
|
|
|
|
picker.SetDrawer(picker)
|
2024-07-21 09:48:28 -06:00
|
|
|
picker.SetRole(tomo.R("objects", "ColorPickerMap"))
|
|
|
|
picker.OnButtonUp(picker.handleButtonUp)
|
|
|
|
picker.OnButtonDown(picker.handleButtonDown)
|
2024-06-22 13:44:24 -06:00
|
|
|
picker.OnMouseMove(picker.handleMouseMove)
|
|
|
|
return picker
|
|
|
|
}
|
|
|
|
|
2024-08-15 15:05:19 -06:00
|
|
|
func (this *hsvaColorPickerMap) handleButtonDown (button input.Button) bool {
|
2024-07-25 10:58:38 -06:00
|
|
|
if button != input.ButtonLeft { return false }
|
2024-06-22 13:44:24 -06:00
|
|
|
this.dragging = true
|
|
|
|
this.drag()
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2024-06-22 13:44:24 -06:00
|
|
|
}
|
|
|
|
|
2024-08-15 15:05:19 -06:00
|
|
|
func (this *hsvaColorPickerMap) handleButtonUp (button input.Button) bool {
|
2024-07-25 10:58:38 -06:00
|
|
|
if button != input.ButtonLeft { return false }
|
2024-06-22 13:44:24 -06:00
|
|
|
this.dragging = false
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2024-06-22 13:44:24 -06:00
|
|
|
}
|
|
|
|
|
2024-08-15 15:05:19 -06:00
|
|
|
func (this *hsvaColorPickerMap) handleMouseMove () bool {
|
2024-07-25 10:58:38 -06:00
|
|
|
if !this.dragging { return false }
|
2024-06-22 13:44:24 -06:00
|
|
|
this.drag()
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2024-06-22 13:44:24 -06:00
|
|
|
}
|
|
|
|
|
2024-08-15 15:05:19 -06:00
|
|
|
func (this *hsvaColorPickerMap) drag () {
|
2024-07-21 09:48:28 -06:00
|
|
|
pointer := this.Window().MousePosition()
|
2024-06-22 13:44:24 -06:00
|
|
|
bounds := this.InnerBounds()
|
|
|
|
this.parent.value.S = float64(pointer.X - bounds.Min.X) / float64(bounds.Dx())
|
|
|
|
this.parent.value.V = 1 - float64(pointer.Y - bounds.Min.Y) / float64(bounds.Dy())
|
|
|
|
this.parent.value = this.parent.value.Canon()
|
|
|
|
this.parent.on.valueChange.Broadcast()
|
|
|
|
this.Invalidate()
|
|
|
|
}
|
|
|
|
|
2024-08-15 15:05:19 -06:00
|
|
|
func (this *hsvaColorPickerMap) Draw (can canvas.Canvas) {
|
2024-06-22 13:44:24 -06:00
|
|
|
bounds := can.Bounds()
|
|
|
|
for y := bounds.Min.Y; y < bounds.Max.Y; y ++ {
|
|
|
|
for x := bounds.Min.X; x < bounds.Max.X; x ++ {
|
|
|
|
xx := x - bounds.Min.X
|
|
|
|
yy := y - bounds.Min.Y
|
|
|
|
|
2024-09-05 20:46:58 -06:00
|
|
|
pixel := icolor.HSVA {
|
2024-06-22 13:44:24 -06:00
|
|
|
H: this.parent.value.H,
|
|
|
|
S: float64(xx) / float64(bounds.Dx()),
|
|
|
|
V: 1 - float64(yy) / float64(bounds.Dy()),
|
2024-08-15 14:42:31 -06:00
|
|
|
A: 0xFFFF,
|
2024-06-22 13:44:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
sPos := int( this.parent.value.S * float64(bounds.Dx()))
|
|
|
|
vPos := int((1 - this.parent.value.V) * float64(bounds.Dy()))
|
|
|
|
sDist := sPos - xx
|
|
|
|
vDist := vPos - yy
|
|
|
|
crosshair :=
|
|
|
|
(sDist == 0 || vDist == 0) &&
|
|
|
|
-8 < sDist && sDist < 8 &&
|
|
|
|
-8 < vDist && vDist < 8
|
|
|
|
if crosshair {
|
|
|
|
pixel.S = 1 - pixel.S
|
|
|
|
pixel.V = 1 - pixel.V
|
|
|
|
}
|
|
|
|
|
|
|
|
can.Set(x, y, pixel)
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|