a
    «ˆcÛ‚  ã                   @   s¢   d Z ddlmZ ddlmZ dd„ Zdd„ Zdd	„ ZG d
d„ dƒZG dd„ dƒZ	G dd„ dƒZ
G dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )aä   The common code for a gradient editor for `tvtk.LookupTables` and
`tvtk.VolumeProperty` color transfer functions.  Most of the code is
independent of tvtk however.

The toolkit specific code is in toolkit specific files.  This code is
distributed under the conditions of the BSD license.

This code was originally written by Gerald Knizia <cgk.d@gmx.net> and
later modified by Prabhu Ramachandran for tvtk and MayaVi2.

Copyright (c) 2005-2020, Gerald Knizia and Prabhu Ramachandran
é    )Úsplitext)Útvtkc                 C   s   d| |  ||  S )zƒlinearly interpolate between arguments arg0 and arg1.

       The weight f is from [0..1], with f=0 giving arg0 and f=1 giving arg1é   © )Zarg0Zarg1Úfr   r   ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/tvtk/util/gradient_editor.pyÚlerp   s    r   c           	      C   sÖ   t | ||fƒ}t| ||fƒ}d}||krŒ| |krR| |krR|d|| ||   9 }n:||krt|d||  ||   9 }n|d| | ||   9 }|dk rœ|d7 }|dkr¬|d8 }|dkrÂ|| | }nd}|}||||fS )zñConvert color from RGBA to HSVA.

    input: r,g,b,a are from [0..1]
    output: h,s,v,a are from [0..1] (h will never be 1.0)

    See http://en.wikipedia.org/wiki/HSV_color_space
    Only difference: hue range is [0..1) here, not [0..360).gUUUUUUÅ?r   é   é   ç      ð?)ÚmaxÚmin)	ÚrÚgÚbÚaZmax_compZmin_compÚhÚsÚvr   r   r   Úrgba_to_hsva   s$    r   c                 C   s   ||||f\}}}}| d }|dk r0||||fS t |d ƒ}|d | }	|d|  }
|d|	|   }|dd|	 |   }d|kr’|||
  }}}n‚d|kr¬|||
  }}}nhd|krÆ|
||  }}}nNd|krà|
||  }}}n4d|krú||
|  }}}nd	|k�r||
|  }}}||||fS )
zÛConvert color from HSVA to RGBA.

    input: h,s,v,a are from [0..1]
    output: r,g,b,a are from [0..1]

    See http://en.wikipedia.org/wiki/HSV_color_space
    Only difference: hue range is [0..1) here, not [0..360).g     €v@g-Cëâ6?g      N@r   r   r	   é   r
   é   )Úint)Zh_r   r   r   r   r   r   r   Zhue_slice_indexZhue_partialÚpÚqÚtr   r   r   Úhsva_to_rgba9   s,    
r   c                   @   sP   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚColorzŒRepresents a color and provides means of automatic conversion between
    HSV(A) and RGB(A) color spaces. The color is stored in HSVA space.c                 C   s
   d| _ d S )N)ç        r   ç      à?r   ©Úhsva©Úselfr   r   r   Ú__init___   s    zColor.__init__c                 C   s   |   |||d¡ d S )Nr   )Úset_rgba)r#   r   r   r   r   r   r   Úset_rgbb   s    zColor.set_rgbc                 C   s   t ||||ƒ| _d S ©N)r   r!   )r#   r   r   r   r   r   r   r   r%   e   s    zColor.set_rgbac                 C   s6   |   ¡ }t|d d ƒt|d d ƒt|d d ƒfS )zWreturns a tuple (r,g,b) of 3 integers in range [0..255] representing
        the color.r   éÿ   r   r	   )Úget_rgbar   )r#   Úrgbar   r   r   Ú
get_rgb255h   s    zColor.get_rgb255c                 C   s   | j \}}}}t||||ƒS r'   )r!   r   ©r#   r   r   r   r   r   r   r   r)   n   s    zColor.get_rgbac                 C   s   | j S r'   r    r"   r   r   r   Úget_hsvar   s    zColor.get_hsvac                 C   s   ||||f| _ d S r'   r    r,   r   r   r   Úset_hsvau   s    zColor.set_hsvac                 C   sr   t |jd |jd |ƒ}t |jd |jd |ƒ}t |jd |jd |ƒ}t |jd |jd |ƒ}||||f| _dS )z¹Set self to result of linear interpolation between colors A and
           B in HSVA space.

           The weight f is from [0..1], with f=0 giving A and f=1 giving
           color B.r   r   r	   r   N)r   r!   )r#   r   ÚAÚBr   r   r   r   r   r   r   Úset_lerpx   s
    zColor.set_lerpN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   r&   r%   r+   r)   r-   r.   r1   r   r   r   r   r   \   s   r   c                   @   s*   e Zd ZdZd
dd„Zdd„ Zdd„ Zd	S )ÚColorControlPointzâA control point represents a fixed position in the gradient
    and its assigned color. A control point can have indifferent
    color channels in hsv space, i.e. channels, on which its
    presence does not impose any effect.Fc                 C   s8   t ƒ | _d| _|| _d|kr.d| _|  |¡ nd| _d S )Nr   r   Úrgb)r   ÚcolorÚposÚfixedÚactive_channelsÚactivate_channels)r#   r;   r:   r   r   r   r$   �   s    zColorControlPoint.__init__c                 C   s&   |D ]}|| j vr|  j |7  _ qdS )z=NewChannels: string consisting of the new color channel namesN©r;   )r#   Znew_channelsÚcr   r   r   r<   œ   s    
z#ColorControlPoint.activate_channelsc                 C   s   t t|dƒdƒ| _d S )Nr   r   )r   r   r9   ©r#   r   r   r   r   Úset_pos¢   s    zColorControlPoint.set_posN)F)r2   r3   r4   r5   r$   r<   r@   r   r   r   r   r6   ˆ   s   
r6   c                   @   s’   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zd$dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#S )%ÚGradientTablea.  this class represents a logical gradient table, i.e. an array
    of colors and the means to control it via control points

    This class (unlike the GradientTableOld) does not support scaling
    and uses VTK's ColorTransferFunction and PiecewiseFunction to
    perform the actual interpolation.
    c                 C   sÜ   || _ t ¡ | _zd| j_W n ty.   Y n0 t ¡ | _tddd�}| 	d¡ |j
 ddd¡ tddd�}| 	d¡ |j
 ddd¡ ||g| _tdd�}| 	d	¡ |j
 dd	d¡ |  |¡ d
| _d| _d | _|  ¡  d S )N)r   r   Tr!   )r:   r;   r   r   Úhsvr=   gš™™™™™Ù?Ú r   )Úsizer   ZColorTransferFunctionÚtableÚrangeÚ	ExceptionZPiecewiseFunctionÚalphar6   r@   r8   r&   Úcontrol_pointsÚinsert_control_pointÚscaling_function_stringÚscaling_function_parameterZscaling_functionÚupdate)r#   Únum_entriesZleft_control_pointZright_control_pointZmid_control_pointr   r   r   r$   ²   s,    







zGradientTable.__init__c                 C   s,   | j  |¡\}}}| j |¡}t||||ƒS )zJreturn (h,s,v,a) tuple in self.table_hsva for fraction f in
        [0,1].)rE   Ú	get_colorrH   Ú	get_valuer   ©r#   r   r   r   r   r   r   r   r   Úget_color_hsvaÞ   s    zGradientTable.get_color_hsvac                 C   s*   | j  |¡\}}}| j |¡}||||fS )zEreturn (r,g,b,a) tuple in self.table for fraction f in
        [0,1].)rE   rO   rH   rP   rQ   r   r   r   rO   å   s    zGradientTable.get_colorc                 C   s   t ƒ }|  |¡}|j|Ž  |S )zsreturn a Color object representing the color which is lies at
        position f \in [0..1] in the current gradient)r   rR   r.   )r#   r   ÚresultÚer   r   r   Úget_pos_colorì   s    

zGradientTable.get_pos_colorc                 C   s
   |   |¡S )zÙreturn a (r,g,b,a) color representing the color which is lies at
        position f \in [0..1] in the current gradient. if f is outside the
        [0..1] interval, the result will be clamped to this
        interval.)rO   r?   r   r   r   Úget_pos_rgba_color_lerpedô   s    z'GradientTable.get_pos_rgba_color_lerpedc                 C   s   |  j |g7  _ |  ¡  dS )zoInsert a new control point into the table. Does sort the control
        points, but does NOT update the table.N)rI   Úsort_control_points)r#   Ú	new_pointr   r   r   rJ   û   s    z"GradientTable.insert_control_pointc                 C   s   | j jdd„ d� dS )z¨Sort control points by position. Call this if the position of
        any control point was changed externally. The control point array
        always has to be sorted.c                 S   s   | j S r'   )r9   )Úxr   r   r   Ú<lambda>  ó    z3GradientTable.sort_control_points.<locals>.<lambda>)ÚkeyN)rI   Úsortr"   r   r   r   rW     s    z!GradientTable.sort_control_pointsc           	      C   st   | j }| j}| ¡  | ¡  | jD ]L}|j}|j ¡ \}}}}|jdkrX| ||||¡ d|jv r"| 	||¡ q"dS )z¤Recalculate the gradient table from the control points. The
        colors are interpolated linearly between each two control
        points in hsva space.
        r   N)
rE   rH   Úremove_all_pointsrI   r9   r8   r-   r;   Úadd_hsv_pointÚ	add_point)	r#   rE   rH   ÚpointrY   r   r   r   r   r   r   r   rM     s    


zGradientTable.updateé   c                 C   s>   ||_ t|ƒD ]*}t|ƒ|d  }|  |¡}| ||¡ qdS )z]Store current color table in `vtk_table`, an instance of
        `tvtk.LookupTable`.
        r   N)Únumber_of_table_valuesrF   ÚfloatrO   Zset_table_value)r#   Ú	vtk_tablerN   Úidxr   r*   r   r   r   Ústore_to_vtk_lookup_table  s
    
z'GradientTable.store_to_vtk_lookup_tablec                 C   s°   |j }| ¡  | ¡ }| ¡  |\}}z||f|_W n tyF   Y n0 || }| jD ]T}||j|  }	|j ¡ \}
}}}|j	dkr”| 
|	|
||¡ d|j	v rV| |	|¡ qVdS )z”Given a `tvtk.VolumeProperty` and a scalar range to map
        values into, this sets the CTF based on the current control
        points.
        r   N)Úrgb_transfer_functionr^   Úget_scalar_opacityrF   rG   rI   r9   r8   r-   r;   r_   r`   )r#   Úvolume_propZscalar_rangeÚctfÚotfÚs1Ús2rD   ra   rY   r   r   r   r   r   r   r   Ústore_to_vtk_volume_prop$  s"    


z&GradientTable.store_to_vtk_volume_propc                 C   s  |j }| ¡ }|j}|dks J ‚|jdks.J ‚|j\}}t|| ƒ}||d  }g }	d}
t|dƒrfd}
dgd }t|ƒD ]®}|
rœ|j| }| |¡\}}}n | ||¡ |dd… \}}}}| 	|¡}|dksÚ||d krèt
d	dd
�}nt
ddd
�}|j ||||¡ || | }| |¡ |	 |¡ qx|j}||d  }dgd }d}
t|dƒ�rXd}
td|d ƒD ]‚}|
�r||j| }n| ||¡ |dd… \}}| |¡\}}}t
ddd
�}|j ||||¡ || | }| |¡ |	 |¡ �qf|	| _|  ¡  |  ¡  dS )a¯  Given a vtkVolumeProperty, this initializes the control
        points of the gradient table.  This works best when a
        ctf.ColorTransferFunction and PiecewiseFunction are used.

        Note that it is not as easy to setup the control points from a
        LUT because the LUT may end up having more than the size of
        the table editor here.  It also usually does not make sense to
        do this with a LUT.
        r   FÚnodesTr   é   Nr
   r!   )r;   r:   rB   r	   r   )rh   ri   rD   rF   rd   Úhasattrrp   rO   Zget_node_valuerP   r6   r8   r%   r@   ÚappendrI   rW   rM   )r#   rj   rk   rl   rD   rm   rn   ZscaleZdsZnew_ctl_ptsZ	has_nodesZ	_ctf_dataÚirY   r   r   r   r   Úptr9   Z	_otf_datar   r   r   Úload_from_vtk_volume_prop=  s^    







z'GradientTable.load_from_vtk_volume_propc                 C   s   t ‚dS )zRecompile the scaling function.N©ÚNotImplementedErrorr"   r   r   r   Úscaling_parameters_changed�  s    z(GradientTable.scaling_parameters_changedc                 C   s   t ‚dS )z-Set the 'a' parameter of the scaling functionNrw   )r#   Znew_parameterr   r   r   Úset_scaling_function_parameter…  s    z,GradientTable.set_scaling_function_parameterc                 C   s   t ‚dS )zgSet scaling function. new_function_string is a string describing the
        function, e.g. 'x**(4*a)' Nrw   )r#   Znew_function_stringr   r   r   Úset_scaling_function‰  s    z"GradientTable.set_scaling_functionc                 C   s   t |ƒ\}}| ¡ dv rd}|| }|d }|d }|d }t|dƒ}| d¡ | d| j ¡ | d	| j ¡ | d
¡ | jD ]N}| d|j|j|j	|j
 ¡ d |j
 ¡ d |j
 ¡ d |j
 ¡ d f ¡ q€| ¡  t ¡ }	|  |	¡ t|dƒ}|	j}
| d|
 ¡ t|
ƒD ]6}|	 |¡}| d|d |d |d |d f ¡ �q| ¡  zddl}W n t�yp   Y nŒ0 d}| d|
|f¡}t|
ƒD ]`}|	 |¡\}}}}t|ƒD ]>}| ||ftd| ƒtd| ƒtd| ƒtd| ƒf¡ �q¬�qŽ| |d¡ dS )z½Save control point set into a new file FileName. It is not checked
        whether the file already exists. Further writes out a VTK .lut file
        and a .jpg file showing the gradients.)ú.lutú.jpgz.jpegú.gradrC   r~   r|   r}   ÚwzV 2.0 Color Gradient File
zScalingFunction: %s
zScalingParameter: %s
z,ControlPoints: (pos fixed bindings h s v a)
z  %s %s %s %s %s %s %s
r   r   r	   r   zLOOKUP_TABLE UnnamedTable %s
z%.4f %.4f %.4f %.4f
Né@   ZRGBAr(   ZJPEG)r   ÚlowerÚopenÚwriterK   rL   rI   r9   r:   r;   r8   r-   Úcloser   ÚLookupTablerg   rc   rF   Zget_table_valueÚImageÚImportErrorÚnewZputpixelr   Úsave)r#   Ú	file_nameZ	path_baseÚextZfile_name_gradZfile_name_lutZfile_name_jpgÚfileÚcontrol_pointre   Z
num_colorsrf   Úentryr†   ZNyZimZnxr   r   r   r   Únyr   r   r   r‰   Ž  sR    



ý



*(
ÿzGradientTable.savec                 C   st  t |dƒ}| ¡ }t| ¡ d ƒd }|dkrt| ¡  ¡ }| ¡ }t|ƒdkrZ|d | _nd| _t| ¡ d ƒ| _nd| _d| _| ¡  g }| ¡ }t|ƒdkr¤�qR| ¡ }	t|	ƒd	k rÌd
}
|
|7 }
t|
ƒ‚tdd�}| 	t|	d ƒ¡ d|	d k|_
|	d |_t|	d ƒt|	d ƒt|	d ƒt|	d ƒf\}}}}|j ||||¡ | |¡ qŒ| ¡  || _|  ¡  |  ¡  dS )zQLoad control point set from file FileName and recalculate gradient
        table.r   r   gñhãˆµøä>gš™™™™™ñ?r	   rC   r   r   é   z%gradient file format broken at line:
r=   ÚTruer   r
   r   rq   N)r‚   Úreadlinerd   ÚsplitÚlenrK   rL   Ú
ValueErrorr6   r@   r:   r;   r8   r.   rs   r„   rI   rW   rM   )r#   rŠ   rŒ   Zversion_tagÚversionZfunction_line_splitZparameter_lineZnew_control_pointsÚcur_lineÚargsÚmsgrX   r   r   r   r   r   r   r   ÚloadÎ  sF    


ÿzGradientTable.loadN)rb   )r2   r3   r4   r5   r$   rR   rO   rU   rV   rJ   rW   rM   rg   ro   rv   ry   rz   r{   r‰   rš   r   r   r   r   rA   ©   s"   ,

D@rA   c                   @   sT   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚChannelBasec                 C   s"   || _ || _|| _|| _|| _dS )z%arguments documented in function bodyN)ÚcontrolÚnameÚ	rgb_colorÚindexÚmode)r#   Zfunction_controlr�   rž   Zchannel_indexZchannel_moder   r   r   r$      s
    zChannelBase.__init__c                 C   s*   | j dkr| ¡ | j S | ¡ | j S dS )zSReturn height value of the current channel for the given color.
        Range: 0..1rB   N)r    r-   rŸ   r)   ©r#   r8   r   r   r   rP     s    
zChannelBase.get_valuec                 C   s"   t d| jjd d|  |¡   ƒS )zPReturn height index of channel value of Color.
        Range: [1..ControlHeight]r   r   )r   rœ   ÚheightrP   r¡   r   r   r   Úget_value_index  s    zChannelBase.get_value_indexc                 C   s$   t dtddt|ƒ| jjd   ƒƒS )z%Get value in [0..1] of height index yr   r   r   )r   r   rd   rœ   r¢   )r#   Úyr   r   r   Úget_index_value  s    zChannelBase.get_index_valuec                 C   sÐ   | j dkrv| ¡ d | ¡ d | ¡ d | ¡ d g}||| j< |d dkrTd|d< | |d |d |d |d ¡ nV| ¡ d | ¡ d | ¡ d | ¡ d g}||| j< | |d |d |d |d ¡ dS )zfColor will be modified: NewValue.. will be set to the color
        channel that ``*self`` represents.rB   r   r   r	   r   gwJëÿï?N)r    r-   rŸ   r.   r)   r%   )r#   r8   Znew_value_on_this_channelr!   r*   r   r   r   Ú	set_value  s    
ÿ
"ÿ
zChannelBase.set_valuec                 C   s   |   ||  |¡¡ dS )zˆColor will be modified: the value assigned to the height index
        y will be set to the color channel of Color ``*self`` represents.N)r¦   r¥   )r#   r8   r¤   r   r   r   Úset_value_index/  s    zChannelBase.set_value_indexc                 C   s   t || jjd  ƒS )z0Return x-index for gradient position f in [0..1]r   )r   rœ   Úwidthr?   r   r   r   Úget_pos_index4  s    zChannelBase.get_pos_indexc                 C   s   d| | j jd  S )zSReturn gradient position f in [0..1] for x-index Idx in
        [0..ControlWidth-1]r   r   )rœ   r¨   )r#   rf   r   r   r   Úget_index_pos8  s    zChannelBase.get_index_posc                 C   s   t ‚dS )zšPaint current channel into Canvas (a canvas of a function control
        object).

        This should be overridden to do the actual painting.

        Nrw   )r#   Zpainterr   r   r   Úpaint=  s    zChannelBase.paintN)r2   r3   r4   r$   rP   r£   r¥   r¦   r§   r©   rª   r«   r   r   r   r   r›   ÿ  s   r›   c                   @   s2   e Zd ZdZdZeZddd„Zdd„ Zd	d
„ Z	dS )ÚFunctionControlz«Widget which displays a rectangular regions on which hue, sat, val
    or rgb values can be modified. An function control can have one or more
    attached color channels.r
   Néd   c           	   	   C   sÚ  ddddddddœ| _ || _|| _|| _|| _|| _g | _| j}|D �]}|d	krp|  j|| d	d
ddƒg7  _qD|dkr–|  j|| ddddƒg7  _qD|dkr¼|  j|| ddddƒg7  _qD|dkrâ|  j|| dd
ddƒg7  _qD|dk�r
|  j|| ddddƒg7  _qD|dk�r2|  j|| ddddƒg7  _qD|dkrD|  j|| ddddƒg7  _qDd| _| jD ]}|  j|j	7  _�qdd	|v �sšd|v �sšd|v �rÀdD ] }|| jv�rž|  j|7  _�qž|dk�rÐd| _d| _
dS )a\  Initialize a function control widget on tkframe master.

        Parameters:
        -----------

        master: The master widget.  Note that this widget *must* have
             the methods specified in the `AbstractGradientEditorWidget`
             interface.

        on_table_changed: Callback function taking a bool argument of meaning
            'FinalUpdate'. FinalUpdate is true if a control point is dropped,
            created or removed and false if the update is due to a control point
            currently beeing dragged (but not yet dropped)

        color_space: String which specifies the channels painted on this control.
             May be any combination of h,s,v,r,g,b,a in which each channel
             occurs only once.

        set_status_text: a callback used to set the status text
             when using the editor.
        ÚREDÚGREENÚBLUEÚHUEZ
SATURATIONZVALUEÚALPHA)r   r   r   r   r   r   r   r   )r(   r   r   r   r7   r   )r   r(   r   r   r   )r   r   r(   r	   r   rB   r   r   r   )r   r   r   r   rC   N)Ztext_mapÚmasterrE   Úgradient_tabler¨   r¢   ÚchannelsÚChannelFactoryZactive_channels_stringr�   Zcur_drag)	r#   r³   r´   Zcolor_spacer¨   r¢   ZChannelr>   Úchannelr   r   r   r$   T  sH    þ




zFunctionControl.__init__c                 C   s†   | j D ]z}| jjD ]l}|j|jvr$q| |j¡}| |j¡}|}|du rL|}|| d || d  | j	d kr||g    S qqdS )z‚Check if a control point lies near (x,y) or near x if y is None.
        returns [channel, control point] if found, None otherwiseNr	   )
rµ   rE   rI   r�   r;   r©   r9   r£   r8   Úcontrol_pt_click_tolerance)r#   rY   r¤   r·   r�   Zpoint_xZpoint_yZy_r   r   r   Úfind_control_pointš  s    
"z"FunctionControl.find_control_pointc                 C   s   | j  ¡  | j |¡ dS )z´Called internally in the control if the configuration of the attached
        gradient table has changed due to actions of this control.

        Forwards the update/change notice.N)rE   rM   r³   Úon_gradient_table_changed©r#   Úfinal_updater   r   r   Útable_config_changed¬  s    
z$FunctionControl.table_config_changed)NNNr­   r­   )
r2   r3   r4   r5   r¸   r›   r¶   r$   r¹   r½   r   r   r   r   r¬   I  s     ÿ
Fr¬   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚAbstractGradientEditorc                 C   s   t ‚dS )z/ Update the gradient table and vtk lookuptable.Nrw   r»   r   r   r   rº   ¿  s    z0AbstractGradientEditor.on_gradient_table_changedc                 C   s   t ‚dS )z4Set the status on the status widget if you have one.Nrw   ©r#   r™   r   r   r   Úset_status_textÃ  s    z&AbstractGradientEditor.set_status_textc                 C   s   t ‚dS )z%Return the CTF or LUT's scalar range.Nrw   r"   r   r   r   Úget_table_rangeÇ  s    z&AbstractGradientEditor.get_table_rangeN)r2   r3   r4   rº   rÀ   rÁ   r   r   r   r   r¾   ¾  s   r¾   c                   @   sB   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )ÚGradientEditorWidgetz8A Gradient Editor widget that can be used anywhere.
    Nc              
   C   sœ   |du rg d¢}|| _ d| _d| _| j| _d| _t| jƒ| _|| _t|t	j
ƒrVd| _nd| _| j |¡ || _g | _d| _d	d
ddddddddœ	}|| _dS )a±  

        Parameters:
        -----------

        vtk_table : the `tvtk.LookupTable` or `tvtk.VolumeProperty` object
                    to set.

        on_change_color_table : A callback called when the color table
                                changes.

        colors : list of 'rgb', 'hsv', 'h', 's', 'v', 'a'
                 (Default : ['rgb', 'hsv', 'a'])

                 'rgb' creates one panel to edit Red, Green and Blue
                 colors.

                 'hsv' creates one panel to edit Hue, Saturation and
                 Value.

                 'h', 's', 'v', 'r', 'g', 'b', 'a' separately
                 specified creates different panels for each.
        N)r7   rB   r   i,  é2   éP   TFzFLeft click: move control points
Right click: add/remove control points)rC   ZRGB)z)Hue: Red; Saturation: Green; Value: Blue
ZHSV)rC   r±   )rC   ZSAT)rC   ZVAL)rC   r®   )rC   r¯   )rC   r°   )rC   r²   )	r7   rB   r   r   r   r   r   r   r   )ÚcolorsZgradient_preview_widthZgradient_preview_heightZchannel_function_widthZchannel_function_heightrA   r´   Úvtk_color_tableÚ
isinstancer   r…   Úvtk_table_is_lutrv   Úon_change_color_tableÚfunction_controlsZtooltip_textÚeditor_data)r#   r³   re   rÉ   rÅ   rË   r   r   r   r$   Ò  s6    õzGradientEditorWidget.__init__c                 C   s   t ‚d S r'   rw   r¿   r   r   r   rÀ     s    z$GradientEditorWidget.set_status_textc                 C   sj   | j D ]}| ¡  q| j ¡  |rf| j}| jr<| j |¡ n|  ¡ }| j ||¡ | j	}|durf|ƒ  dS )z1 Update the gradient table and vtk lookuptable...N)
rÊ   rM   Zgradient_controlrÆ   rÈ   r´   rg   rÁ   ro   rÉ   )r#   r¼   rœ   re   ÚrngÚcbr   r   r   rº     s    


z.GradientEditorWidget.on_gradient_table_changedc                 C   s    | j }| jr|jS | ¡ jS d S r'   )rÆ   rÈ   Ztable_rangeri   rF   )r#   re   r   r   r   rÁ   0  s    z$GradientEditorWidget.get_table_rangec                 C   s,   t |ƒdkrdS | j |¡ | jdd� dS )z?Set the state of the color table using the given file.
        r   NT)r¼   )r”   r´   rš   rº   ©r#   rŠ   r   r   r   rš   7  s    zGradientEditorWidget.loadc                 C   s    t |ƒdkrdS | j |¡ dS )aL  Store the color table to the given file.  This actually
        generates 3 files, a '.grad', a '.lut' file and a '.jpg' file.
        The .lut file can be used to setup a lookup table.  The .grad
        file is used to set the state of the gradient table and the
        JPG file is an image of the how the lut will look.
        r   N)r”   r´   r‰   rÎ   r   r   r   r‰   ?  s    zGradientEditorWidget.save)NNNN)
r2   r3   r4   r5   r$   rÀ   rº   rÁ   rš   r‰   r   r   r   r   rÂ   Ï  s     ÿ
CrÂ   N)r5   Úos.pathr   Ztvtk.apir   r   r   r   r   r6   rA   Úobjectr›   r¬   r¾   rÂ   r   r   r   r   Ú<module>   s   #,!  XJu