Skip to content

Commit

Permalink
Add color unit options to color picker
Browse files Browse the repository at this point in the history
Add:
- color-serializer-options
  - :css-percentage-unit
  - :css-angle-unit
- Menu
- Key F%, FA
  • Loading branch information
misohena committed Feb 21, 2025
1 parent a9327f9 commit f6b0f43
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 45 deletions.
122 changes: 103 additions & 19 deletions edraw-color-picker.el
Original file line number Diff line number Diff line change
Expand Up @@ -2061,7 +2061,11 @@ but the reverse can also be done."
(when (eq prefer-syntax 'css-color-function)
(plist-get serializer-options :css-prefer-color-function)))
(color-name-usage
(plist-get serializer-options :color-name-usage)))
(plist-get serializer-options :color-name-usage))
(percentage-unit
(plist-get serializer-options :css-percentage-unit))
(angle-unit
(plist-get serializer-options :css-angle-unit)))

(when (stringp prefer-function)
(setq prefer-function (downcase prefer-function))) ;; lowercase
Expand Down Expand Up @@ -2090,6 +2094,35 @@ but the reverse can also be done."
((edraw-msg "Enabled")
edraw-color-picker-set-output-format-color-name-usage-enabled
:button (:toggle . ,(memq color-name-usage '(t enabled))))))
((edraw-msg "Percentage Unit")
(((edraw-msg "Auto")
edraw-color-picker-set-output-format-css-percentage-unit-auto
:button (:toggle . ,(eq percentage-unit nil)))
((edraw-msg "None")
edraw-color-picker-set-output-format-css-percentage-unit-none
:button (:toggle . ,(eq percentage-unit 'none)))
((edraw-msg "%")
edraw-color-picker-set-output-format-css-percentage-unit-percent
:button (:toggle . ,(equal percentage-unit "%")))))
((edraw-msg "Angle Unit")
(((edraw-msg "Auto")
edraw-color-picker-set-output-format-css-angle-unit-auto
:button (:toggle . ,(eq angle-unit nil)))
((edraw-msg "None")
edraw-color-picker-set-output-format-css-angle-unit-none
:button (:toggle . ,(eq angle-unit 'none)))
((edraw-msg "deg")
edraw-color-picker-set-output-format-css-angle-unit-deg
:button (:toggle . ,(equal angle-unit "deg")))
((edraw-msg "rad")
edraw-color-picker-set-output-format-css-angle-unit-rad
:button (:toggle . ,(equal angle-unit "rad")))
((edraw-msg "grad")
edraw-color-picker-set-output-format-css-angle-unit-grad
:button (:toggle . ,(equal angle-unit "grad")))
((edraw-msg "turn")
edraw-color-picker-set-output-format-css-angle-unit-turn
:button (:toggle . ,(equal angle-unit "turn")))))
("--single-line")
((edraw-msg "Auto") edraw-color-picker-set-output-format-css-auto
:button (:toggle . ,(null prefer-syntax)))
Expand Down Expand Up @@ -2186,6 +2219,9 @@ but the reverse can also be done."
)))

(defun edraw-color-picker-toggle-serializer-options (picker &rest keys)
(unless picker
(setq picker
(edraw-color-picker-at-input-or-error (edraw-this-command-event))))
;; Note: Modify the contents of PICKER options list
(cl-loop for key in keys
do
Expand All @@ -2197,6 +2233,9 @@ but the reverse can also be done."
(edraw-notify-options-change picker))

(defun edraw-color-picker-set-serializer-options (picker &rest new-props)
(unless picker
(setq picker
(edraw-color-picker-at-input-or-error (edraw-this-command-event))))
;; Note: Modify the contents of PICKER options list
(cl-loop for (key value) on new-props by #'cddr
do
Expand All @@ -2209,82 +2248,113 @@ but the reverse can also be done."

(defun edraw-color-picker-set-output-format-color-name-usage-auto ()
(interactive)
(edraw-color-picker-set-serializer-options
(edraw-color-picker-at-input-or-error (edraw-this-command-event))
:color-name-usage nil))
(edraw-color-picker-set-serializer-options nil :color-name-usage nil))

(defun edraw-color-picker-set-output-format-color-name-usage-enabled ()
(interactive)
(edraw-color-picker-set-serializer-options
(edraw-color-picker-at-input-or-error (edraw-this-command-event))
:color-name-usage 'enabled))
(edraw-color-picker-set-serializer-options nil :color-name-usage 'enabled))

(defun edraw-color-picker-set-output-format-color-name-usage-disabled ()
(interactive)
(edraw-color-picker-set-serializer-options
(edraw-color-picker-at-input-or-error (edraw-this-command-event))
:color-name-usage 'disabled))
(edraw-color-picker-set-serializer-options nil :color-name-usage 'disabled))

(defun edraw-color-picker-set-output-format-css-percentage-unit-auto ()
(interactive)
(edraw-color-picker-set-serializer-options nil :css-percentage-unit nil))

(defun edraw-color-picker-set-output-format-css-percentage-unit-none ()
(interactive)
(edraw-color-picker-set-serializer-options nil :css-percentage-unit 'none))

(defun edraw-color-picker-set-output-format-css-percentage-unit-percent ()
(interactive)
(edraw-color-picker-set-serializer-options nil :css-percentage-unit "%"))

(defun edraw-color-picker-set-output-format-css-angle-unit-auto ()
(interactive)
(edraw-color-picker-set-serializer-options nil :css-angle-unit nil))

(defun edraw-color-picker-set-output-format-css-angle-unit-none ()
(interactive)
(edraw-color-picker-set-serializer-options nil :css-angle-unit 'none))

(defun edraw-color-picker-set-output-format-css-angle-unit-deg ()
(interactive)
(edraw-color-picker-set-serializer-options nil :css-angle-unit "deg"))

(defun edraw-color-picker-set-output-format-css-angle-unit-rad ()
(interactive)
(edraw-color-picker-set-serializer-options nil :css-angle-unit "rad"))

(defun edraw-color-picker-set-output-format-css-angle-unit-grad ()
(interactive)
(edraw-color-picker-set-serializer-options nil :css-angle-unit "grad"))

(defun edraw-color-picker-set-output-format-css-angle-unit-turn ()
(interactive)
(edraw-color-picker-set-serializer-options nil :css-angle-unit "turn"))


(defun edraw-color-picker-set-output-format-css-auto ()
(interactive)
(edraw-color-picker-set-serializer-options
(edraw-color-picker-at-input-or-error (edraw-this-command-event))
nil
:css-prefer-color-syntax nil
:css-prefer-color-function nil))

(defun edraw-color-picker-set-output-format-css-hex ()
(interactive)
(edraw-color-picker-set-serializer-options
(edraw-color-picker-at-input-or-error (edraw-this-command-event))
nil
:css-prefer-color-syntax 'css-hex-color
:css-prefer-color-function nil))

(defun edraw-color-picker-set-output-format-css-rgb ()
(interactive)
(edraw-color-picker-set-serializer-options
(edraw-color-picker-at-input-or-error (edraw-this-command-event))
nil
:css-prefer-color-syntax 'css-color-function
:css-prefer-color-function "rgb"))

(defun edraw-color-picker-set-output-format-css-hsl ()
(interactive)
(edraw-color-picker-set-serializer-options
(edraw-color-picker-at-input-or-error (edraw-this-command-event))
nil
:css-prefer-color-syntax 'css-color-function
:css-prefer-color-function "hsl"))

(defun edraw-color-picker-set-output-format-css-hwb ()
(interactive)
(edraw-color-picker-set-serializer-options
(edraw-color-picker-at-input-or-error (edraw-this-command-event))
nil
:css-prefer-color-syntax 'css-color-function
:css-prefer-color-function "hwb"))

(defun edraw-color-picker-set-output-format-css-lab ()
(interactive)
(edraw-color-picker-set-serializer-options
(edraw-color-picker-at-input-or-error (edraw-this-command-event))
nil
:css-prefer-color-syntax 'css-color-function
:css-prefer-color-function "lab"))

(defun edraw-color-picker-set-output-format-css-lch ()
(interactive)
(edraw-color-picker-set-serializer-options
(edraw-color-picker-at-input-or-error (edraw-this-command-event))
nil
:css-prefer-color-syntax 'css-color-function
:css-prefer-color-function "lch"))

(defun edraw-color-picker-set-output-format-css-oklab ()
(interactive)
(edraw-color-picker-set-serializer-options
(edraw-color-picker-at-input-or-error (edraw-this-command-event))
nil
:css-prefer-color-syntax 'css-color-function
:css-prefer-color-function "oklab"))

(defun edraw-color-picker-set-output-format-css-oklch ()
(interactive)
(edraw-color-picker-set-serializer-options
(edraw-color-picker-at-input-or-error (edraw-this-command-event))
nil
:css-prefer-color-syntax 'css-color-function
:css-prefer-color-function "oklch"))

Expand All @@ -2306,6 +2376,20 @@ but the reverse can also be done."
(define-key km (kbd "FC-") '("Auto" . edraw-color-picker-set-output-format-color-name-usage-auto))
(define-key km (kbd "FC0") '("None" . edraw-color-picker-set-output-format-color-name-usage-disabled))
(define-key km (kbd "FC1") '("None" . edraw-color-picker-set-output-format-color-name-usage-enabled))

(define-key km (kbd "F%") (cons "Percentage Unit" (make-sparse-keymap)))
(define-key km (kbd "F%-") '("Auto" . edraw-color-picker-set-output-format-css-percentage-unit-auto))
(define-key km (kbd "F%0") '("None" . edraw-color-picker-set-output-format-css-percentage-unit-none))
(define-key km (kbd "F%%") '("%" . edraw-color-picker-set-output-format-css-percentage-unit-percent))

(define-key km (kbd "FA") (cons "Angle Unit" (make-sparse-keymap)))
(define-key km (kbd "FA-") '("Auto" . edraw-color-picker-set-output-format-css-angle-unit-auto))
(define-key km (kbd "FA0") '("None" . edraw-color-picker-set-output-format-css-angle-unit-none))
(define-key km (kbd "FAd") '("deg" . edraw-color-picker-set-output-format-css-angle-unit-deg))
(define-key km (kbd "FAr") '("rad" . edraw-color-picker-set-output-format-css-angle-unit-rad))
(define-key km (kbd "FAg") '("grad" . edraw-color-picker-set-output-format-css-angle-unit-grad))
(define-key km (kbd "FAt") '("turn" . edraw-color-picker-set-output-format-css-angle-unit-turn))

(define-key km (kbd "F-") '("Auto" . edraw-color-picker-set-output-format-css-auto))
(define-key km (kbd "F#") '("HEX" . edraw-color-picker-set-output-format-css-hex))
(define-key km (kbd "Fr") '("RGB" . edraw-color-picker-set-output-format-css-rgb))
Expand Down
69 changes: 43 additions & 26 deletions edraw-color.el
Original file line number Diff line number Diff line change
Expand Up @@ -1439,9 +1439,11 @@ Signals an error if there is a syntax or other problem, never returns nil."
(setq value (max 0.0 (min 1.0 value)))
(concat
(pcase unit
('nil (edraw-color-number-to-string
value
(or (plist-get options :css-decimal-places-alpha) 3)))
((or 'nil 'none 'no-unit)
(setq unit nil)
(edraw-color-number-to-string
value
(or (plist-get options :css-decimal-places-alpha) 3)))
("%" (edraw-color-number-to-string (* value 100.0) 1))
(_ (error "Unexpected unit: %s" unit)))
unit))
Expand All @@ -1457,9 +1459,11 @@ Signals an error if there is a syntax or other problem, never returns nil."

(concat
(pcase unit
('nil (edraw-color-number-to-string
(* (or number-ref 1.0) value)
(plist-get arg-syntax :decimal-places-number)))
((or 'nil 'none 'no-unit)
(setq unit nil)
(edraw-color-number-to-string
(* (or number-ref 1.0) value)
(plist-get arg-syntax :decimal-places-number)))
("%" (edraw-color-number-to-string
(/ (* 100.0 value) (or percent-ref 1.0))
(or (plist-get options :decimal-places-percent)
Expand Down Expand Up @@ -1494,6 +1498,17 @@ Signals an error if there is a syntax or other problem, never returns nil."
;; TEST: (edraw-color-css-make-number-unit 120.1 "grad" 255.0) => "133grad"
;; TEST: (edraw-color-css-make-number-unit 120.1 "turn" 255.0) => "0.334turn"

(defun edraw-color-css-arg-unit (arg-syntax arg-props options)
(or
(if (eq (plist-get arg-syntax :value-type) 'angle)
(plist-get options :css-angle-unit)
(plist-get options :css-percentage-unit))
(plist-get arg-syntax :unit) ;; forced unit
(plist-get arg-props :unit)
(plist-get
options
(plist-get arg-syntax :default-unit-keyword))))

(defun edraw-color-css-make-alpha (alpha modern arg-props options)
(when (< alpha 1.0)
(concat
Expand All @@ -1507,9 +1522,10 @@ Signals an error if there is a syntax or other problem, never returns nil."
(plist-get options :css-spaces-after-slash)
(plist-get options :css-spaces-after-comma)))
;; Argument
(let ((unit (or (plist-get arg-props :unit)
(plist-get options :css-default-alpha-unit))))
(edraw-color-css-make-alpha-number alpha unit options))
(edraw-color-css-make-alpha-number
alpha
(edraw-color-css-arg-unit nil arg-props options)
options)
(plist-get arg-props :post-spaces))))

(defun edraw-color-css-make-color-function-impl
Expand Down Expand Up @@ -1542,12 +1558,10 @@ Signals an error if there is a syntax or other problem, never returns nil."
target-fname) ;; @todo Keep upcase?
"("
(cl-loop with unified-unit = (when unify-unit-p
(or (when args-for-this-fun-p
(plist-get (cdr (car args)) :unit))
(plist-get
options
(plist-get (car arg-syntax-list)
:default-unit-keyword))))
(edraw-color-css-arg-unit
(car arg-syntax-list)
(when args-for-this-fun-p (cdr (car args)))
options))
for i from 0
for rest-values on (funcall color-to-value-list-function color)
for value = (car rest-values)
Expand All @@ -1558,12 +1572,10 @@ Signals an error if there is a syntax or other problem, never returns nil."
for unit = (if unify-unit-p
unified-unit
;; Modern syntax allows different units for each arg
(or (plist-get arg-syntax :unit) ;; forced unit
(when args-for-this-fun-p
(plist-get arg-props :unit))
(plist-get
options
(plist-get arg-syntax :default-unit-keyword))))
(edraw-color-css-arg-unit
arg-syntax
(when args-for-this-fun-p arg-props)
options))
concat
(concat
;; Argument separator for legacy syntax
Expand Down Expand Up @@ -1654,7 +1666,8 @@ Signals an error if there is a syntax or other problem, never returns nil."
#'edraw-color-to-hsl-list
`((:default-unit-keyword
:css-default-hue-unit
:decimal-places-number 0)
:decimal-places-number 0
:value-type angle)
(:default-unit-keyword
:css-default-saturation-unit
:number-ref 100.0 :percent-ref 1.0
Expand All @@ -1671,7 +1684,8 @@ Signals an error if there is a syntax or other problem, never returns nil."
`((:default-unit-keyword
:css-default-hue-unit
:decimal-places-number 0
:decimal-places-deg 0)
:decimal-places-deg 0
:value-type angle)
(:unit
"%"
:decimal-places-number
Expand All @@ -1698,7 +1712,8 @@ Signals an error if there is a syntax or other problem, never returns nil."
color options "hwb" #'edraw-color-to-hwb-list
'((:default-unit-keyword
:css-default-hue-unit
:decimal-places-number 0)
:decimal-places-number 0
:value-type angle)
(:default-unit-keyword
:css-default-lightness-unit
:number-ref 100.0
Expand Down Expand Up @@ -1745,7 +1760,8 @@ Signals an error if there is a syntax or other problem, never returns nil."
:decimal-places-number 2)
(:default-unit-keyword
:css-default-hue-unit
:decimal-places-number 0))))
:decimal-places-number 0
:value-type angle))))

(defun edraw-color-css-make-oklab (color &optional options)
;; Lab ( https://www.w3.org/TR/css-color-4/#specifying-oklab-oklch )
Expand Down Expand Up @@ -1782,7 +1798,8 @@ Signals an error if there is a syntax or other problem, never returns nil."
:decimal-places-number 5)
(:default-unit-keyword
:css-default-hue-unit
:decimal-places-number 0))))
:decimal-places-number 0
:value-type angle))))

(defun edraw-color-css-make-color-function (color &optional options)
(let* ((fname (or (plist-get options :css-prefer-color-function)
Expand Down
Loading

0 comments on commit f6b0f43

Please sign in to comment.