PNG generation ignoring color

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

PNG generation ignoring color

Chris Spencer
How do you retain note color for PNG and SVG generation?

I have the following Lilypond file test.ly:

\version "2.18"
color = #(define-music-function (parser location color) (string?) #{
        \once \override NoteHead #'color = #(x11-color color)
        \once \override Stem #'color = #(x11-color color)
        \once \override Rest #'color = #(x11-color color)
        \once \override Beam #'color = #(x11-color color)
     #})

\header {
 tagline = ""
  title = "Test1"

  }

\score  {

      << \new Staff  = xawdxfwebzffzxy { \key a \major
             \time 4/4
             r 2
             \color "#00FF00" e' 4
             \color "#00FF00" e' 4
             \bar "|"  %{ end measure 1 %}
             \color "#00FF00" fis' 4
             \color "#00FF00" e' 2.
             \bar "|"  %{ end measure 2 %}
             r 2
             \color "#00FF00" fis' 4
             \color "#00FF00" e' 4
             \bar "|"  %{ end measure 3 %}
             \color "#00FF00" fis' 4
             \color "#00FF00" a' 2.
             \break
             \bar "|"  %{ end measure 4 %}
             r 2
             \color "#00FF00" b' 4
             \color "#00FF00" b' 4
             \bar "|"  %{ end measure 5 %}
             \color "#00FF00" cis'' 4
             \color "#00FF00" b' 2.
             \bar "|"  %{ end measure 6 %}
             \color "#00FF00" fis' 4
             \color "#00FF00" fis' 4
             \color "#00FF00" fis' 4
             \color "#00FF00" e' 4
             \bar "|"  %{ end measure 7 %}
             \color "#00FF00" fis' 4
             \color "#00FF00" a' 2.
             \break
             \bar "|"  %{ end measure 8 %}
             r 2
             \color "#00FF00" e' 4
             \color "#00FF00" e' 4
             \bar "|"  %{ end measure 9 %}
             \color "#00FF00" fis' 4
             \color "#00FF00" e' 2.
             \bar "|"  %{ end measure 10 %}
             \color "#00FF00" fis' 4
             \color "#00FF00" a' 4
             \color "#00FF00" a' 4
             \color "#00FF00" cis'' 4
             \bar "|"  %{ end measure 11 %}
             \color "#00FF00" a' 1
             \break
             \bar "|"  %{ end measure 12 %}
             \color "#00FF00" b' 4
             \color "#00FF00" b' 4
             \color "#00FF00" b' 4
             \color "#00FF00" b' 4
             \bar "|"  %{ end measure 13 %}
             \color "#00FF00" cis'' 4
             \color "#00FF00" b' 2.
             \bar "|"  %{ end measure 14 %}
             \color "#00FF00" fis' 4
             \color "#00FF00" a' 4
             \color "#00FF00" a' 4
             \color "#00FF00" b' 4
             \bar "|"  %{ end measure 15 %}
             \color "#00FF00" a' 2
             \break
             \bar "|."  %{ end measure 16 %}
              }


        >>

  }

\paper {
  indent = 0\cm
}
\layout {
  \context {
    \RemoveEmptyStaffContext
    \override VerticalAxisGroup #'remove-first = ##t
  }
 }

and although I've specified all notes be green, running `lilypond
--png test.ly` results in a black and white image.

Why is this? Why are the color statements being ignored?

Regards,
Chris

Reply | Threaded
Open this post in threaded view
|

Re: PNG generation ignoring color

David Kastrup
Chris Spencer <[hidden email]> writes:

> How do you retain note color for PNG and SVG generation?
>
> I have the following Lilypond file test.ly:
>
> \version "2.18"
> color = #(define-music-function (parser location color) (string?) #{
>         \once \override NoteHead #'color = #(x11-color color)
>         \once \override Stem #'color = #(x11-color color)
>         \once \override Rest #'color = #(x11-color color)
>         \once \override Beam #'color = #(x11-color color)
>      #})
>
> \header {
>  tagline = ""
>   title = "Test1"
>
>   }
>
> \score  {
>
>       << \new Staff  = xawdxfwebzffzxy { \key a \major
>              \time 4/4
>              r 2
>              \color "#00FF00" e' 4


> and although I've specified all notes be green, running `lilypond
> --png test.ly` results in a black and white image.
>
> Why is this? Why are the color statements being ignored?

Like the warning LilyPond prints out indicates, the X11 #rrggbb
convention is not supported for specifying colors.  x11-color instead
supports _named_ colors like "green".

--
David Kastrup

Reply | Threaded
Open this post in threaded view
|

Re: PNG generation ignoring color

Valentin Villenave-3
On 5/16/20, David Kastrup <[hidden email]> wrote:
> Like the warning LilyPond prints out indicates, the X11 #rrggbb
> convention is not supported for specifying colors.  x11-color instead
> supports _named_ colors like "green".

Chris, you *could* simply change the definition or your function as follows:

%%%%

color =
#(define-music-function (parser location color) (string?)
   (define (hexa->decimal x)
     (let ((num (string->number x 16)))
       (if (= 0 num)
           num
           (exact->inexact
            (/ 255 num)))))
   (let* ((str (string-take
                (if (string-prefix? "#" color)
                    (string-drop color 1)
                    color)
                6))
          (r (string-take str 2))
          (g (string-drop (string-drop-right str 2) 2))
          (b (string-take-right str 2))
          (translated-color
           (map hexa->decimal
                (list r g b)))
          (lily-color (primitive-eval
                       (cons rgb-color translated-color))))
     #{
       %\override Staff.StaffSymbol.color = #lily-color
       \override Staff.LedgerLineSpanner.color = #lily-color
       \override Staff.Clef.color = #lily-color
       \override Staff.KeySignature.color = #lily-color
       \override Staff.TimeSignature.color = #lily-color
       %% Remove Staff. below to only affect individual voices:
       \override Staff.NoteHead.color = #lily-color
       \override Staff.Stem.color = #lily-color
       \override Staff.Flag.color = #lily-color
       \override Staff.Beam.color = #lily-color
       \override Staff.Accidental.color = #lily-color
       \override Staff.AccidentalCautionary.color = #lily-color
       \override Staff.Rest.color = #lily-color
       \override Staff.MultiMeasureRest.color = #lily-color
       \override Staff.MultiMeasureRestText.color = #lily-color
       \override Staff.MultiMeasureRestNumber.color = #lily-color
       \override Staff.Dots.color = #lily-color
       \override Staff.Slur.color = #lily-color
       \override Staff.PhrasingSlur.color = #lily-color
       \override Staff.Tie.color = #lily-color
       \override Staff.Script.color = #lily-color
       \override Staff.TextScript.color = #lily-color
       \override Staff.TupletBracket.color = #lily-color
       \override Staff.TupletNumber.color = #lily-color
       \override Staff.DynamicText.color = #lily-color
       \override Staff.DynamicTextSpanner.color = #lily-color
       \override Staff.DynamicLineSpanner.color = #lily-color
       \override Staff.Hairpin.color = #lily-color
     #}))

%%%%

Cheers,
-- V.