
    rhM                    x   S r SSKJr  SSKJr  SSKrSSKrSSKrSSK	J
r
  SSKJr  SSKJr  SSKJr  SS	KJr  \R                   " S
5      r\" S/ SQ5      rS r " S S\R(                  5      r " S S\R(                  5      rS rS r " S S\R2                  5      r\S:X  a  SSKr\R8                  " \5        gg)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pltnetworkxc                     S[         ;   a  [        S5      eSSKn  SSKJn  SSKJn  SSKJ	n  SSK
Jn   SSKn[        XX#XE5      $ ! [
         a    Sn[        R                  S5         NGf = f! [
         a    Sn NBf = f)	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, networkx)
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
   r   s         Q/home/james-whalen/.local/lib/python3.13/site-packages/music21/graph/utilities.pygetExtendedModulesr   $   s     ~%@B 	B
/ '" $ :{SSS'  
I	

   s"   A A, !A)(A),A;:A;c                      \ rS rSrSrg)r   L    N__name__
__module____qualname____firstlineno____static_attributes__r       r   r   r   L       r%   r   c                      \ rS rSrSrg)PlotStreamExceptionP   r   Nr   r   r%   r   r(   r(   P   r&   r%   r(   c                    [        U [        5      (       d  U $ [        R                  R	                  5        H.  u  pUS:w  d  M  X;   d  M  US;   d  M  U R                  X5      n   U $    U $ )uQ  
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   accidentalLabelToUnicoder6   T   sc     eS!! % 9 9 ? ? Ar>h/H
4JMM(7EL !B Lr%   c           	     l   [         R                  " U 5      (       a  X U /n [        U [        5      (       au  U S   S:X  a  [        R
                  " U 5      $ U R                  5       R                  SS5      n [        U 5      S:X  a  SSSS	S
SSSS.n X   n  [        R                  " U 5      $ [         R                  " U 5      (       a  SnU  H  nUS:  d  M  Sn  O   U(       ap  [        U 5      S:X  a  U S   U S   U S   /n [        R                  " [         [        [        [        4   [!        S U  5       5      5      n[        R"                  " U5      $ [        R$                  " [!        U 5      5      $ [        SU  35      e! [         a    [        SU  35      ef = f! [         a    [        SU  35      ef = f)a  
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  D   #    U  H  n[        US -  5      S-   v   M     g7f)d   %N)r/   ).0xs     r   	<genexpr>getColor.<locals>.<genexpr>   s      =`Z_UVc!c'lS>PZ_s    zinvalid color specification: )r   isNumr.   r/   	webcolorsnormalize_hexlowerr2   lenKeyErrorr   name_to_hex
ValueError
isListLiketcasttuplergb_percent_to_hex
rgb_to_hex)colorcolorMappercentsubcolorStrLists        r   getColorrd   m   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> F >FF3c                      \ rS rSrS rSrg)Test   c                    U R                  [        / SQ5      S5        U R                  [        S5      S5        U R                  [        S5      S5        U R                  [        S5      S5        g )N)      ?ri   ri   z#808080ri      z#ffffffz
Steel Bluez#4682b4)assertEqualrd   )selfs    r   
testColorsTest.testColors   sT    /2I>#	2#	2,/;r%   r   N)r    r!   r"   r#   rm   r$   r   r%   r   rf   rf      s    <r%   rf   __main__)__doc__
__future__r   r   r   typingrZ   unittestrR   music21.baser   music21r   r   r   r	   Environmentr   r
   r   Music21Exceptionr   r(   r6   rd   TestCaserf   r    mainTestr   r%   r   <module>rz      s    # "    (     &&'89 .bd#TP	\22 		,77 	2MB`<8 < zT r%   