U
    祡c                     @   s   d dl mZ d dlmZ d dlmZmZmZ ddlm	Z	 erHd dl
mZ G dd dZed	krd d
lZd dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZ G dd dZe Zee  d
S )    )sqrt)	lru_cache)SequenceTupleTYPE_CHECKING   )ColorTripletTablec                   @   sn   e Zd ZdZeeeeef  dddZeedddZ	dd	d
dZ
eddeeeef edddZdS )PalettezA palette of available colors.)colorsc                 C   s
   || _ d S N)_colors)selfr    r   0/tmp/pip-unpacked-wheel-qkhzhrww/rich/palette.py__init__   s    zPalette.__init__)numberreturnc                 C   s   t | j|  S r   )r   r   )r   r   r   r   r   __getitem__   s    zPalette.__getitem__r
   )r   c              
   C   s   ddl m} ddlm} ddlm} ddlm} |dddd	t| j	 d
ddd}t
| j	D ]2\}}|t|t||d||j| dd q\|S )Nr   ColorStyle)Textr	   indexZRGBr   r   z colorsTright)titlecaptionZ	highlightZcaption_justifyz                )bgcolor)style)
rich.colorr   
rich.styler   Z	rich.textr   
rich.tabler
   lenr   	enumerateZadd_rowstrreprfrom_rgb)r   r   r   r   r
   tabler   colorr   r   r   __rich__   s(    	zPalette.__rich__i   )maxsize)r*   r   c                    sL   |\t  | jjttd fdd}ttt| j|d}|S )zFind a color from a palette that most closely matches a given color.

        Args:
            color (Tuple[int, int, int]): RGB components in range 0 > 255.

        Returns:
            int: Index of closes matching color.
        )r   r   c                    sj   | \}}}| d }| }| }| } d| | | d? d| |  d| | | d?  S )zGet the distance to a color.   i         i  r   )r   Zred2Zgreen2Zblue2Zred_meanZredZgreenZblue_sqrtZblue1Z	get_colorZgreen1Zred1r   r   get_color_distance:   s    
z)Palette.match.<locals>.get_color_distance)key)r   r   r   intfloatminranger$   )r   r*   r2   Z	min_indexr   r0   r   match,   s    

zPalette.matchN)__name__
__module____qualname____doc__r   r   r4   r   r   r   r+   r   r8   r   r   r   r   r      s   r   __main__N)Iterabler   )ConsoleConsoleOptions)Segmentr   c                   @   s"   e Zd Zeeee dddZdS )ColorBox)consoleoptionsr   c                 c   s   |j jd }td|D ]}t|jD ]}||j }||d  }t||d\}}	}
t||d| d  d\}}}t|d |	d |
d }t|d |d |d }tdt	||dV  q$t
 V  qd S )	N   r   r   g      ?r-      u   ▄)r*   r   )sizeheightr7   	max_widthcolorsysZ
hls_to_rgbr   r(   rA   r   line)r   rC   rD   rH   yxhlZr1Zg1Zb1Zr2Zg2Zb2r   r*   r   r   r   __rich_console__T   s    
 zColorBox.__rich_console__N)r9   r:   r;   r?   r@   r>   rA   rP   r   r   r   r   rB   S   s    rB   )mathr   	functoolsr   typingr   r   r   Zcolor_tripletr   r#   r
   r   r9   rJ   r>   r!   r   Zrich.consoler?   r@   Zrich.segmentrA   r"   r   rB   rC   printr   r   r   r   <module>   s    @