<commit_before>#------------------------------------------------------------------------------
#
#  Copyright (c) 2005, Enthought, Inc.
#  All rights reserved.
#  
#  This software is provided without warranty under the terms of the BSD
#  license included in enthought/LICENSE.txt and may be redistributed only
#  under the conditions described in the aforementioned license.  The license
#  is also available online at http://www.enthought.com/licenses/BSD.txt
#
#  Thanks for using Enthought open source!
#------------------------------------------------------------------------------

""" Defines the various color editors for the Wx user interface toolkit.
"""

#-------------------------------------------------------------------------------
#  Imports:
#-------------------------------------------------------------------------------

import wx
import wx.combo

from enthought.traits.api import List, TraitError
from enthought.traits.ui.editors.color_editor \
    import ToolkitEditorFactory as BaseToolkitEditorFactory

from editor_factory import SimpleEditor as BaseSimpleEditor
from editor_factory import ReadonlyEditor as BaseReadonlyEditor
from editor_factory import TextEditor as BaseTextEditor

from color_trait import w3c_color_database
from helper import TraitsUIPanel

# Version dependent imports (ColourPtr not defined in wxPython 2.5):
try:
    ColorTypes = (wx.Colour, wx.ColourPtr)
except:
    ColorTypes = wx.Colour

#---------------------------------------------------------------------------
#  The Wx ToolkitEditorFactory class.
#---------------------------------------------------------------------------

class ToolkitEditorFactory(BaseToolkitEditorFactory):
    """ Wx editor factory for color editors.
    """
    
    def to_wx_color ( self, editor ):
        """ Gets the wxPython color equivalent of the object trait.
        """
        if self.mapped:
            attr = getattr( editor.object, editor.name + '_' )
        else:
            attr = getattr( editor.object, editor.name )
           
        if isinstance(attr, tuple):
            attr = wx.Colour( *[ int( round( c * 255.0 ) )
                             for c in attr ] )
        return attr
           
         
    #---------------------------------------------------------------------------
    #  Gets the application equivalent of a wxPython value:
    #---------------------------------------------------------------------------
        
    def from_wx_color ( self, color ):
        """ Gets the application equivalent of a wxPython value.
        """
        return color.Red(), color.Green(), color.Blue()
           
    #---------------------------------------------------------------------------
    #  Returns the text representation of a specified color value:
    #---------------------------------------------------------------------------
      
    def str_color ( self, color ):
        """ Returns the text representation of a specified color value.
        """
        if isinstance( color, ColorTypes ):
            alpha = color.Alpha()
            if alpha == 255:
                return "rgb(%d,%d,%d)" % (
                        color.Red(), color.Green(), color.Blue() )
   
            return "rgb(%d,%d,%d,%d)" % (
                    color.Red(), color.Green(), color.Blue(), alpha )
               
        return str(color)


#-------------------------------------------------------------------------------
#  'ColorComboBox' class:
#-------------------------------------------------------------------------------

class ColorComboBox(wx.combo.OwnerDrawnComboBox):
    def OnDrawItem(self, dc, rect, item, flags):

        color_name = self.GetString(item)
        color = w3c_color_database.Find(color_name)
#        color = wx.Colour()
#        color.SetFromName(color_name)

        
        r = wx.Rect(rect.x, rect.y, rect.width, rect.height)
        r.Deflate(3, 0)
        
        dc.DrawText(color_name, r.x + 3, 
                    r.y + (r.height - dc.GetCharHeight())/2)
        
        swatch_size = r.height - 2
        
        brush = wx.Brush(color)
        dc.SetBrush(brush)
        dc.DrawRectangle(r.x + r.width - swatch_size, r.y+1,
                         swatch_size, swatch_size)
    
#-------------------------------------------------------------------------------
#  'SimpleColorEditor' class:
#-------------------------------------------------------------------------------

class SimpleColorEditor ( BaseSimpleEditor ):
    """ Simple style of color editor, which displays a text field whose 
    background color is the color value. Selecting the text field displays
    a dialog box for selecting a new color value.
    """
    
    #---------------------------------------------------------------------------
    #  Invokes the pop-up editor for an object trait:
    #---------------------------------------------------------------------------
    
    choices = List()
    
    def _choices_default(self):
        """ by default, uses the W3C 16 color names.
        """
        return ['aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 
                'lime', 'maroon', 'navy', 'olive', 'purple', 'red', 
                'silver', 'teal', 'white', 'yellow'] 
 
    def init ( self, parent ):
        """
        Finishes initializing the editor by creating the underlying widget.
        """
        current_color = self.factory.to_wx_color(self)
        current_color_name = current_color.GetAsString()
        
        self.control = ColorComboBox(parent, -1, current_color_name,
                                wx.Point( 0, 0 ), wx.Size( -1, -1 ), self.choices,
                                style = wx.wx.CB_READONLY)
        
        self.control.Bind(wx.EVT_COMBOBOX, self.color_selected)                
        return

    #---------------------------------------------------------------------------
    #  Updates the editor when the object trait changes external to the editor:
    #---------------------------------------------------------------------------

    def update_editor ( self ):
        """ Updates the editor when the object trait changes externally to the 
            editor.
        """
        current_color = self.factory.to_wx_color(self)
        self.control.SetValue(self.string_value(current_color))

    def color_selected(self, event):
        """
        Event for when color is selected
        """
        
        color_name = self.choices[event.Selection]
        try:
            color = w3c_color_database.Find(color_name)
            self.value = color
        except ValueError:
            pass
            
        return

    def string_value ( self, color ):
        """ Returns the text representation of a specified color value.
        """
        color_name = w3c_color_database.FindName(color)
        if color_name != '':
            return color_name<commit_msg>ENH - added 'custom' color to simple color editor<commit_after>#------------------------------------------------------------------------------
#
#  Copyright (c) 2005, Enthought, Inc.
#  All rights reserved.
#  
#  This software is provided without warranty under the terms of the BSD
#  license included in enthought/LICENSE.txt and may be redistributed only
#  under the conditions described in the aforementioned license.  The license
#  is also available online at http://www.enthought.com/licenses/BSD.txt
#
#  Thanks for using Enthought open source!
#------------------------------------------------------------------------------

""" Defines the various color editors for the Wx user interface toolkit.
"""

#-------------------------------------------------------------------------------
#  Imports:
#-------------------------------------------------------------------------------

import wx
import wx.combo

from enthought.traits.api import List, TraitError
from enthought.traits.ui.editors.color_editor \
    import ToolkitEditorFactory as BaseToolkitEditorFactory

from editor_factory import SimpleEditor as BaseSimpleEditor
from editor_factory import ReadonlyEditor as BaseReadonlyEditor
from editor_factory import TextEditor as BaseTextEditor

from color_trait import w3c_color_database
from helper import TraitsUIPanel

# Version dependent imports (ColourPtr not defined in wxPython 2.5):
try:
    ColorTypes = (wx.Colour, wx.ColourPtr)
except:
    ColorTypes = wx.Colour

#---------------------------------------------------------------------------
#  The Wx ToolkitEditorFactory class.
#---------------------------------------------------------------------------

class ToolkitEditorFactory(BaseToolkitEditorFactory):
    """ Wx editor factory for color editors.
    """
    
    def to_wx_color ( self, editor ):
        """ Gets the wxPython color equivalent of the object trait.
        """
        if self.mapped:
            attr = getattr( editor.object, editor.name + '_' )
        else:
            attr = getattr( editor.object, editor.name )
           
        if isinstance(attr, tuple):
            attr = wx.Colour( *[ int( round( c * 255.0 ) )
                             for c in attr ] )
        return attr
           
         
    #---------------------------------------------------------------------------
    #  Gets the application equivalent of a wxPython value:
    #---------------------------------------------------------------------------
        
    def from_wx_color ( self, color ):
        """ Gets the application equivalent of a wxPython value.
        """
        return color.Red(), color.Green(), color.Blue()
           
    #---------------------------------------------------------------------------
    #  Returns the text representation of a specified color value:
    #---------------------------------------------------------------------------
      
    def str_color ( self, color ):
        """ Returns the text representation of a specified color value.
        """
        if isinstance( color, ColorTypes ):
            alpha = color.Alpha()
            if alpha == 255:
                return "rgb(%d,%d,%d)" % (
                        color.Red(), color.Green(), color.Blue() )
   
            return "rgb(%d,%d,%d,%d)" % (
                    color.Red(), color.Green(), color.Blue(), alpha )
               
        return str(color)


#-------------------------------------------------------------------------------
#  'ColorComboBox' class:
#-------------------------------------------------------------------------------

class ColorComboBox(wx.combo.OwnerDrawnComboBox):
    def OnDrawItem(self, dc, rect, item, flags):

        r = wx.Rect(rect.x, rect.y, rect.width, rect.height)
        r.Deflate(3, 0)
        swatch_size = r.height - 2

        color_name = self.GetString(item)
        
        dc.DrawText(color_name, r.x + 3, 
                    r.y + (r.height - dc.GetCharHeight())/2)
                
        if color_name == 'custom':
            swatch = wx.Rect(r.x + r.width - swatch_size, r.y+1,
                             swatch_size, swatch_size)
            dc.GradientFillLinear(swatch, wx.Colour(255,255,0), 
                                  wx.Colour(0,0,255))
        else:
            color = w3c_color_database.Find(color_name)
                        
            brush = wx.Brush(color)
            dc.SetBrush(brush)
            dc.DrawRectangle(r.x + r.width - swatch_size, r.y+1,
                             swatch_size, swatch_size)
    
#-------------------------------------------------------------------------------
#  'SimpleColorEditor' class:
#-------------------------------------------------------------------------------

class SimpleColorEditor ( BaseSimpleEditor ):
    """ Simple style of color editor, which displays a text field whose 
    background color is the color value. Selecting the text field displays
    a dialog box for selecting a new color value.
    """
    
    #---------------------------------------------------------------------------
    #  Invokes the pop-up editor for an object trait:
    #---------------------------------------------------------------------------
    
    choices = List()
    
    def _choices_default(self):
        """ by default, uses the W3C 16 color names.
        """
        return ['aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 
                'lime', 'maroon', 'navy', 'olive', 'purple', 'red', 
                'silver', 'teal', 'white', 'yellow', 'custom'] 
 
    def init ( self, parent ):
        """
        Finishes initializing the editor by creating the underlying widget.
        """
        current_color = self.factory.to_wx_color(self)
        current_color_name = current_color.GetAsString()
        
        self.control = ColorComboBox(parent, -1, current_color_name,
                                wx.Point( 0, 0 ), wx.Size( -1, -1 ), self.choices,
                                style = wx.wx.CB_READONLY)
        
        self.control.Bind(wx.EVT_COMBOBOX, self.color_selected)                
        return

    #---------------------------------------------------------------------------
    #  Updates the editor when the object trait changes external to the editor:
    #---------------------------------------------------------------------------

    def update_editor ( self ):
        """ Updates the editor when the object trait changes externally to the 
            editor.
        """
        current_color = self.factory.to_wx_color(self)
        self.control.SetValue(self.string_value(current_color))

    def color_selected(self, event):
        """
        Event for when color is selected
        """
        
        color_name = self.choices[event.Selection]
        
        if color_name == 'custom':
            color_dialog = wx.ColourDialog(self.control)
            color_dialog.ShowModal()
            
            color = color_dialog.GetColourData().GetColour()
            self.value = color
        else:
            try:
                color = w3c_color_database.Find(color_name)
                self.value = color
            except ValueError:
                pass
            
        return

    def string_value ( self, color ):
        """ Returns the text representation of a specified color value.
        """
        color_name = w3c_color_database.FindName(color)
        if color_name != '':
            return color_name
{'fraction_non_alphanumeric': 0.2691344280545505, 'fraction_numerical': 0.005218480378513777, 'mean_word_length': 1.1532584269662922, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4026555', 'n_tokens_mistral': 3437, 'n_tokens_neox': 3304, 'n_words': 943}