
    3j_                    v   d Z ddlmZ ddlmZ ddlZddlZddlZddl	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ  ej                   d
      Z edg d      Zd Z G d dej(                        Z G d dej(                        Zd Zd Z G d dej2                        Zedk(  rddlZ ej8                  e       yy)zV
Functions for finding external modules, converting colors to Matplotlib colors, etc.
    )annotations)
namedtupleN)_missingImport)common)environment)exceptions21)pitchzgraph.utilitiesExtendedModules
matplotlibAxes3Dcollectionspatchespltc                     dt         v rt        d      ddl} 	 ddlm} ddlm} ddlm	} ddl
m} t        | ||||      S # t
        $ r d}t        j                  d       Y Cw xY w)	z
    this is done inside a function, so that the slow import of matplotlib is not done
    in ``from music21 import *`` unless it's actually needed.

    Returns a namedtuple: (matplotlib, Axes3D, collections, patches, plt)
    r   z2could not find matplotlib, graphing is not allowedr   N)r   zmpl_toolkits.mplot3d.Axes3D could not be imported -- likely cause is an old version of six.py (< 1.9.0) on your system somewhere)r   )r   )r   GraphExceptionr   mpl_toolkits.mplot3dr   ImportErrorenvironLocalwarnr   r   matplotlib.pyplotpyplotr
   r   s        D/DATA/.local/lib/python3.12/site-packages/music21/graph/utilities.pygetExtendedModulesr   #   ss     ~%@B 	B
/ '" $:v{GSII  
I	

s   A    A#"A#c                      e Zd Zy)r   N__name__
__module____qualname__     r   r   r   E       r!   r   c                      e Zd Zy)PlotStreamExceptionNr   r    r!   r   r$   r$   I   r"   r!   r$   c                    t        | t              s| S t        j                  j	                         D ])  \  }}|dk7  s|| v s|dv s| j                  ||      }  | S  | S )uu  
    Changes a label possibly containing a modifier such as "-" or "#" into
    a unicode string.

    >>> graph.utilities.accidentalLabelToUnicode('B-4')
    'B♭4'

    Since matplotlib's default fonts do not support double sharps or double flats,
    etc. these are converted as best we can.

    >>> graph.utilities.accidentalLabelToUnicode('B--4')
    'B♭♭4'
     )-#)
isinstancestrr	   unicodeFromModifieritemsreplace)labelmodifier
unicodeAccs      r   accidentalLabelToUnicoder1   M   sf     eS! % 9 9 ? ? A*r>h%/H
4JMM(J7EL !B Lr!   c           	     *   t        j                  |       r| | | g} t        | t              rr| d   dk(  rt	        j
                  |       S | j                         j                  dd      } t        |       dk(  rdddd	d
dddd}	 ||    } 	 t	        j                  |       S t        j                  |       rd}| D ]  }|dk  s	d} n |rmt        |       dk(  r| d   | d   | d   g} t        j                  t         t        t        t        f   t!        d | D                    }t	        j"                  |      S t	        j$                  t!        |             S t        d|        # t        $ r t        d|        w xY w# t        $ r t        d|        w xY w)aH  
    Convert any specification of a color to a hexadecimal color used by matplotlib.

    >>> graph.utilities.getColor('red')
    '#ff0000'
    >>> graph.utilities.getColor('r')
    '#ff0000'
    >>> graph.utilities.getColor('Steel Blue')
    '#4682b4'
    >>> graph.utilities.getColor('#f50')
    '#ff5500'
    >>> graph.utilities.getColor([0.5, 0.5, 0.5])
    '#808080'
    >>> graph.utilities.getColor(0.8)
    '#cccccc'
    >>> graph.utilities.getColor([0.8])
    '#cccccc'
    >>> graph.utilities.getColor([255, 255, 255])
    '#ffffff'

    Invalid colors raise GraphExceptions:

    >>> graph.utilities.getColor('l')
    Traceback (most recent call last):
    music21.graph.utilities.GraphException: invalid color abbreviation: l

    >>> graph.utilities.getColor('chalkywhitebutsortofgreenish')
    Traceback (most recent call last):
    music21.graph.utilities.GraphException: invalid color name: chalkywhitebutsortofgreenish

    >>> graph.utilities.getColor(True)
    Traceback (most recent call last):
    music21.graph.utilities.GraphException: invalid color specification: True
    r   r(    r&      bluegreenredcyanmagentayellowblackwhite)bgrcmykwzinvalid color abbreviation: zinvalid color name: FTc              3  >   K   | ]  }t        |d z        dz     yw)d   %N)r*   ).0xs     r   	<genexpr>zgetColor.<locals>.<genexpr>   s!     =`Z_UVc!c'lS>PZ_s   zinvalid color specification: )r   isNumr)   r*   	webcolorsnormalize_hexlowerr-   lenKeyErrorr   name_to_hex
ValueError
isListLiketcasttuplergb_percent_to_hex
rgb_to_hex)colorcolorMappercentsubcolorStrLists        r   getColorr^   f   s   H ||Eu%%8s? **511%%c2.u:?#$"#&%$$&HM 	A((// 
		5	!CQw  5zQq58U1X666%S#"6=`Z_=`8`aL//==''e55
8@
AA+  M$'CE7%KLLM  	A #7w!?@@	As   E E: E7:Fc                      e Zd Zd Zy)Testc                    | j                  t        g d      d       | j                  t        d      d       | j                  t        d      d       | j                  t        d      d       y )N)      ?rb   rb   z#808080rb      z#ffffffz
Steel Bluez#4682b4)assertEqualr^   )selfs    r   
testColorszTest.testColors   sT    /2I>#	2#	2,/;r!   N)r   r   r   rf   r    r!   r   r`   r`      s    <r!   r`   __main__)__doc__
__future__r   r   r   typingrT   unittestrL   music21.baser   music21r   r   r   r	   Environmentr   r
   r   Music21Exceptionr   r$   r1   r^   TestCaser`   r   mainTestr    r!   r   <module>rr      s    # "    (     &{&&'89 .VXJD	\22 		,77 	2MB`<8 < zGT r!   