
    ccO#                     R    d dl Z d dlmZ ddlmZ ddlmZ ddlmZ  G d de      Zy)	    N)Gdk   )AbstractClassicTool)utilities_get_magic_path)utilities_get_rgba_for_xyc                   |     e Zd Zd Z fdZd Zd Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd Zd Z xZS )	ToolPaintc                 |    t         |   dt        d      d|       d | _        d| _        | j                  dd       y )NpaintPaintztool-paint-symbolicF
paint_algoreplace)super__init____magic_pathuse_sizeadd_tool_action_enum)selfwindowkwargs	__class__s      4/usr/lib/python3/dist-packages/drawing/tool_paint.pyr   zToolPaint.__init__   s:    '7AgJ(=vF$$-L)4    c                     t        d      S )NzPainting options)r   )r   s    r   get_options_labelzToolPaint.get_options_label"   s    	
	r   c                     | j                  d      }|dk(  rt        d      gS |dk(  rt        d      gS | j                  dz   t        d      z   }| j                  dz   t        d      z   }||gS )	Nr   clippingz5Click on an area to replace its color by transparencywholez(Click on the canvas to entirely paint itz - zMay not work for complex shapesz4It will not work well if the area's edges are blurry)get_option_valuer   label)r   r   label_warning1label_warning2s       r   get_editing_tipszToolPaint.get_editing_tips%   s    $$\2*:DE
FFW78
99::%*K(LL.::% +J )K K.
.	))r   c                 >    | j                  |j                  ||       y )N)set_common_valuesbutton)r   eventsurfaceevent_xevent_ys        r   on_press_on_areazToolPaint.on_press_on_area3   s    w8r   c                    |dk  s+||j                         kD  s|dk  s||j                         kD  ry t        |||      | _        | j	                  d      dk(  rt        |||| j                  d      | _        n4| j	                  d      dk(  rt        |||| j                  d      | _        n	 | j                  ||      }| j                  |       y )Nr   r   fillr   r      )
	get_width
get_heightr   	old_colorr    r   r   r   build_operationapply_operation)r   r(   r)   r*   r+   	operations         r   on_release_on_areazToolPaint.on_release_on_area6   s    q[Gg//11q[Gg0022	,WgwG$.	<(F2.w/6QH4\*i7.w/6QH4 ""7G4)y!r   c                     | j                   | j                  d      | j                  | j                  | j                  | j
                  d}|S )Nr   )tool_idalgonew_rgba	antialiasold_rgbapath)idr    
main_color_use_antialiasr2   r   )r   xyr5   s       r   r3   zToolPaint.build_operationL   sH    gg  . ##~~	) 
r   c                     | j                  |       |d   dk(  r| j                  |       y |d   dk(  r| j                  |       y |d   dk(  r| j                  |       y | j	                  |       y )Nr9   r   r   r.   )start_tool_operation_op_replace	_op_whole_op_fill_op_clipping)r   r5   s     r   do_tool_operationzToolPaint.do_tool_operationY   sk    I&v)#IG#>>)F"==Yr   c                 h    | j                         } |j                  |d     |j                          y)z4Paint the entire image regardless of existing pixelsr:   N)get_contextset_source_rgbar   r   r5   cairo_contexts      r   rF   zToolPaint._op_wholeg   s1    ""$--:!67r   c                     |d   y| j                         } |j                  |d     |j                  |d          |j                          y)zxSimple but ugly, and it's relying on the precision of the provided
		path whose creation is based on shitty heurisctics.r=   Nr:   )rK   rL   append_pathr.   rM   s      r   rG   zToolPaint._op_fillm   sV     v	""$--:!67If-. r   c           
         |d   y| j                         }t        j                  |      }|d   }|j                  dddd       |j	                  |d          |j                  t        j                  j                         |j                  d       |j                  t        j                  j                         |j                          | j                         j                  t        j                   |dd|j#                         |j%                                      d}d	|z  }||k  rt'        d|d   |z         }t'        d|d
   |z         }t'        d|d   |z         }	t)        t+        d|            }t)        t+        d|            }t)        t+        d|	            }	| j-                  |||	       |d
z   }||k  r| j/                          | j1                         }
|
j	                  |d          |
j                  t        j                  j2                         |
j                  dddd       |
j                  d       |j                  t        j                  j                         |
j                          |
j5                          |
j                  t        j                  j6                         t        j8                  |
| j                         j:                  dd       |
j=                          | j?                          |
j                  t        j                  j@                          |
j                  |d     |
j	                  |d          |
j                  d       |
j                          |
j5                          y)zAlgorithmically less ugly than `_op_fill`, but the replacing strategy
		itself doesn't handle (semi-) transparent colors correctly. Looks fine
		when there is no alpha at all and the anti-aliasing is off.r=   Nr<      g      ?   r   
   r   r/      r:   )!get_surfacecairoContextrL   rP   set_operatorOperatorDEST_INset_line_widthset_antialias	AntialiasNONEstroke_preserve	get_imageset_temp_pixbufr   pixbuf_get_from_surfacer0   r1   maxintmin_replace_temp_with_alpharestore_pixbufrK   CLEARr.   OVERcairo_set_source_pixbuftemp_pixbufr   non_destructive_show_modif	DEST_OVER)r   r5   r)   rN   r<   	TOLERANCEiredgreenbluecairo_context2s              r   rE   zToolPaint._op_replace|   s    v	 '--(-z"(S#s3If-.U^^334q!eoo223! ..""3#>#>wAw002G4F4F4H$J K )9n!	I	QA	3q(1+a- 5
a!Q
4	Sc]	3s35
c#tn
4  eT2s1 	
I ##%. Yv./enn223  c34"eoo223  " enn112n'+~~'7'C'CQK!!# enn667 .  )J"78Yv./"  "r   c                     |d   }|d   }|d   }|d   }d}| j                  ||||       | j                          | j                          y)z?Replace the color with transparency by adding an alpha channel.r<   r   r   r/   N)	_clip_redri   rn   )r   r5   r<   r0g0b0margins          r   rH   zToolPaint._op_clipping   sV    z"({"{"{"&..R$!!#r   c                 |    t        d|z  |dz         D ]'  }||z   }|dk  s|dk\  s| j                  ||||       ) y NrU   r   rR   r   )range_clip_green)r   r{   rx   ry   rz   rq   rs          r   rw   zToolPaint._clip_red   sJ    ffqj) (a	Av13h16VQB'(r   c                 |    t        d|z  |dz         D ]'  }||z   }|dk  s|dk\  s| j                  ||||       ) y r}   )r~   
_clip_blue)r   r{   r   ry   rz   rq   gs          r   r   zToolPaint._clip_green   sH    ffqj) &a	Av13h16OOFAq"%&r   c                 z    t        d|z  |dz         D ]&  }||z   }|dk  s|dk\  s| j                  |||       ( y r}   )r~   _replace_main_with_alpha)r   r{   r   r   rz   rq   bs          r   r   zToolPaint._clip_blue   sH    ffqj) +a	Av13h16!!!Q*+r   c                     | j                         j                  d|||      }| j                         j                  |       y NT)get_main_pixbuf	add_alpharb   set_main_pixbufr   rr   rs   rt   
new_pixbufs        r   r   z"ToolPaint._replace_main_with_alpha   s6    ##%//c5$G*.."":.r   c                     | j                         j                  j                  d|||      }| j                         j                  |       y r   )rb   rm   r   rc   r   s        r   rh   z"ToolPaint._replace_temp_with_alpha   s:    ~~++55dCM*.."":.r   )__name__
__module____qualname____gtype_name__r   r   r$   r,   r6   r3   rI   rF   rG   rE   rH   rw   r   r   r   rh   __classcell__)r   s   @r   r	   r	      sZ    5
*9",
 KZ
$(&+//r   r	   )	rX   gi.repositoryr   abstract_classic_toolr   utilities_pathsr   r   r	    r   r   <module>r      s%   $   6 5 6W/# W/r   