]> git.donarmstrong.com Git - lilypond.git/commitdiff
MusicXML: Convert colors in text markup using \with-color #(rgb-color ...)
authorReinhold Kainhofer <reinhold@kainhofer.com>
Tue, 23 Oct 2007 00:07:35 +0000 (02:07 +0200)
committerReinhold Kainhofer <reinhold@kainhofer.com>
Tue, 23 Oct 2007 00:07:35 +0000 (02:07 +0200)
Now that we have the rgb-color scheme function in git, make use of it in
text markup conversion. In MusicXML, the color is given as #AARRGGBB
or #RRGGBB, we simply ignore the alpha channel, extract the r, g and b
values and scale them to the 0.0 - 1.0 range

input/regression/musicxml/16a-Text-PJB.xml
scripts/musicxml2ly.py

index c50c6c44c42e6150b1601d396a6af05386fe2408..8e52b8d21c4ab87b7e67f53640391bee441e6b1d 100644 (file)
@@ -9,7 +9,7 @@
                </encoding>
        </identification>
        <part-list>
-               <score-part id="P1"><part-name>MIDI Track 1</part-name></score-part>
+               <score-part id="P1"><part-name></part-name></score-part>
        </part-list>
        <part id="P1">
                <measure number="1">
                                <type>whole</type>
                                <staff>1</staff>
                        </note>
-                       <direction placement="below">
-                               <direction-type>
-                                       <words default-x="1" default-y="15" font-size="small" font-weight="bold"> 
-                                               Bold, Small
-                                       </words>
-                               </direction-type>
-                               <staff>1</staff>
-                       </direction>
+                        <direction placement="below">
+                            <direction-type>
+                                <words default-x="1" default-y="15" font-size="small" font-weight="bold"> 
+                                    Bold, Small
+                                </words>
+                            </direction-type>
+                            <staff>1</staff>
+                        </direction>
+                        <direction placement="below">
+                            <direction-type>
+                                <words default-x="1" default-y="-15" font-size="small" color="#FF8000"> 
+                                    Normal, Small, Colored, Below
+                                </words>
+                            </direction-type>
+                            <staff>1</staff>
+                        </direction>
                </measure>
        </part>
 </score-partwise>
index 47501b1766bd0192cb80b924015718761ad38c27..1f0dd06adbdb162f7d98144d2b952b867924d6b8 100644 (file)
@@ -620,6 +620,23 @@ def musicxml_dynamics_to_lily_event (dynentry):
     event.type = dynentry.get_name ()
     return event
 
+# Convert single-color two-byte strings to numbers 0.0 - 1.0
+def hexcolorval_to_nr (hex_val):
+    try:
+        v = int (hex_val, 16)
+        if v == 255:
+            v = 256
+        return v / 256.
+    except ValueError:
+        return 0.
+
+def hex_to_color (hex_val):
+    res = re.match (r'#([0-9a-f][0-9a-f]|)([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$', hex_val, re.IGNORECASE)
+    if res:
+        return map (lambda x: hexcolorval_to_nr (x), res.group (2,3,4))
+    else:
+        return None
+
 def musicxml_words_to_lily_event (words):
     event = musicexp.TextEvent ()
     text = words.get_text ()
@@ -628,15 +645,21 @@ def musicxml_words_to_lily_event (words):
     event.text = text
 
     if hasattr (words, 'default-y'):
-        if getattr (words, 'default-y') > 0:
-            event.force_direction = 1
-        else:
-            event.force_direction = -1
+        offset = getattr (words, 'default-y')
+        try:
+            off = string.atoi (offset)
+            if off > 0:
+                event.force_direction = 1
+            else:
+                event.force_direction = -1
+        except ValueError:
+            event.force_direction = 0
 
     if hasattr (words, 'font-weight'):
         font_weight = { "normal": '', "bold": '\\bold' }.get (getattr (words, 'font-weight'), '')
         if font_weight:
             event.markup += font_weight
+
     if hasattr (words, 'font-size'):
         size = getattr (words, 'font-size')
         font_size = {
@@ -650,15 +673,15 @@ def musicxml_words_to_lily_event (words):
         }.get (size, '')
         if font_size:
             event.markup += font_size
-    #if hasattr (words, 'color'):
-    #    color = getattr (words, 'color')
-    #    # TODO: In MusicXML, colors are represented as ARGB colors, in lilypond
-    #    #       as x-color. How can I convert from RGB to x-color???
-    #    font_color = {
-    #      #
-    #    }
+
+    if hasattr (words, 'color'):
+        color = getattr (words, 'color')
+        rgb = hex_to_color (color)
+        if rgb:
+            event.markup += "\\with-color #(rgb-color %s %s %s)" % (rgb[0], rgb[1], rgb[2])
+
     if hasattr (words, 'font-style'):
-        font_style = { "italic": '\\italic' }.get (getattr (words, 'font-style'),'')
+        font_style = { "italic": '\\italic' }.get (getattr (words, 'font-style'), '')
         if font_style:
             event.markup += font_style