rougier / svg-lib

Emacs SVG libraries for creatings tags, icons and bars
GNU General Public License v3.0
343 stars 31 forks source link

svg-lib-icon: Don't draw background if `:background nil` is set #34

Closed hedyhli closed 1 year ago

hedyhli commented 1 year ago

Hi, thanks for your useful library. I believe there is a feature that should be added to svg-lib-icon to make it more customizable.

kind-icon gives icons next to candidates in emacs completion popups, which are fetched with svg-lib-icon. It would be great to have this function support not drawing a background to the fetched icons, so that the resulting SVG icons can adapt to the popup background, depending on whether the user is currently selecting it.

See this issue: jdtsmith/kind-icon#38.

Below is the result currently, svg-lib-icon drawing a background on the "(x)" icon.

toggle image

In the linked issue @jdtsmith also suggests a patch, which makes svg-lib-icon not draw a background if the user has set :background nil:

toggle ```diff @@ -212,10 +212,11 @@ "Convert Emacs COLOR-NAME to #rrggbb form. If COLOR-NAME is unknown to Emacs, then return COLOR-NAME as-is." - (let ((rgb-color (color-name-to-rgb color-name))) - (if rgb-color - (apply #'color-rgb-to-hex (append rgb-color '(2))) - color-name))) + (when color-name + (let ((rgb-color (color-name-to-rgb color-name))) + (if rgb-color + (apply #'color-rgb-to-hex (append rgb-color '(2))) + color-name)))) ;; SVG Library style build from partial specification @@ -528,11 +529,12 @@ (when (>= stroke 0.25) (svg-rectangle svg box-x box-y box-width box-height :fill foreground :rx radius)) - (svg-rectangle svg (+ box-x (/ stroke 2.0)) - (+ box-y (/ stroke 2.0)) - (- box-width stroke) - (- box-height stroke) - :fill background :rx (- radius (/ stroke 2.0))) + (when background + (svg-rectangle svg (+ box-x (/ stroke 2.0)) + (+ box-y (/ stroke 2.0)) + (- box-width stroke) + (- box-height stroke) + :fill background :rx (- radius (/ stroke 2.0)))) (dolist (item (xml-get-children (car root) 'path)) (let* ((attrs (xml-node-attributes item)) ```

Here is what the popup would look like if svg-lib-icon supported transparency:

toggle image (both screenshots used are provided by jdtsmith)

I believe it would be a nice addition to svg-lib to support fetching transparent icons so packages that use svg-lib can have more extensibility, as I'm sure the transparency option would be useful for other packages too.

rougier commented 1 year ago

Much better indeed. Could you (or @jdtsmith) make a PR?