Module CubeColourDialog
[hide private]
[frames] | no frames]

Source Code for Module CubeColourDialog

   1  # --------------------------------------------------------------------------- # 
   2  # CUBECOLOURDIALOG Widget wxPython IMPLEMENTATION 
   3  # 
   4  # Python Code By: 
   5  # 
   6  # Andrea Gavana, @ 16 Aug 2007 
   7  # Latest Revision: 16 Aug 2007, 20.00 GMT 
   8  # 
   9  # 
  10  # TODO List 
  11  # 
  12  # 1. Find A Way To Reduce Flickering On The 2 ColourPanels; 
  13  # 
  14  # 2. See Why wx.GCDC Doesn't Work As I Thought (!). It Looks Slow As A Turtle, 
  15  #    But Probably I Am Doing Something Wrong While Painting The Alpha Textures. 
  16  # 
  17  # 
  18  # For All Kind Of Problems, Requests Of Enhancements And Bug Reports, Please 
  19  # Write To Me At: 
  20  # 
  21  # andrea.gavana@gmail.com 
  22  # gavana@kpo.kz 
  23  # 
  24  # Or, Obviously, To The wxPython Mailing List!!! 
  25  # 
  26  # 
  27  # End Of Comments 
  28  # --------------------------------------------------------------------------- # 
  29   
  30  """ 
  31  The CubeColourDialog is an alternative implementation of wx.ColourDialog, and it 
  32  offers different functionalities with respect to the default wxPython one. It 
  33  can be used as a replacement of wx.ColourDialog with exactly the same syntax and 
  34  methods. 
  35   
  36  Some features: 
  37   
  38    - RGB components may be controlled using spin controls or with mouse gestures 
  39      on a 3D RGB cube, with the 3 components laying on the X, Y, Z axes; 
  40    - HSB components may be controlled using spin controls or with mouse gestures 
  41      on a 2D colour wheel; 
  42    - Brightness has its own vertical slider to play with; 
  43    - The colour alpha channel can be controlled using another vertical slider, or 
  44      via spin control; 
  45    - The colour alpha channel controls can be completely hidden at startup or the 
  46      choice to use the alpha channel can be left to the user while playing with the 
  47      dialog, via a simple wx.CheckBox; 
  48    - The "old colour" and "new colour" are displayed in two small custom panel, 
  49      which support alpha transparency and texture; 
  50    - CubeColourDialog displays also the HTML colour code in hexadecimal format; 
  51    - When available, a corresponding "Web Safe" colour is generated using a 500 
  52      web colours "database" (a dictionary inside the widget source code). Web Safe 
  53      colours are recognized by all the browsers; 
  54    - When available, a corresponding "HTML name" for the selected colour is displayed, 
  55      by using the same 500 web colours "database"; 
  56    - When available, a corresponding "Microsoft Access Code" for the selected colour 
  57      is displayed, by using the same 500 web colours "database". 
  58       
  59  And much more. 
  60   
  61   
  62  License And Version: 
  63   
  64  CubeColourDialog is freeware and distributed under the wxPython license.  
  65   
  66  Latest Revision: Andrea Gavana @ 16 Aug 2007, 20.00 GMT 
  67   
  68  Version 0.1. 
  69   
  70   
  71  @undocumented: Vertex, Left, Top, Right, colourAttributes, colourMaxValues, 
  72                 checkColour, HTMLCodes 
  73                  
  74   
  75  """ 
  76   
  77  __docformat__ = "epytext" 
  78   
  79   
  80  #---------------------------------------------------------------------- 
  81  # Beginning Of CUBECOLOURDIALOG wxPython Code 
  82  #---------------------------------------------------------------------- 
  83   
  84  import wx 
  85  import string 
  86  import colorsys 
  87   
  88  from math import pi, sin, cos, sqrt, atan2 
  89   
  90  # Show the alpha control in the dialog 
  91  CCD_SHOW_ALPHA = 1 
  92  """ Show the alpha control in the dialog. """ 
  93   
  94  # Radius of the HSB colour wheel 
  95  RADIUS = 100 
  96  """ Radius of the HSB colour wheel. """ 
  97   
  98  # Width of the mouse-controlled colour pointer 
  99  RECT_WIDTH = 5 
 100  """ Width of the mouse-controlled colour pointer. """ 
 101   
 102  # Dictionary keys for the RGB colour cube 
 103  RED, GREEN, BLUE = 0, 1, 2 
 104  """ Dictionary keys for the RGB colour cube. """ 
 105   
 106  Vertex = wx.Point(95, 109) 
 107  Top = wx.Point(95, 10) 
 108  Left = wx.Point(16, 148) 
 109  Right = wx.Point(174, 148) 
 110   
 111  colourAttributes = ["r", "g", "b", "h", "s", "v"] 
 112  colourMaxValues = [255, 255, 255, 359, 255, 255] 
 113  checkColour = wx.Colour(200, 200, 200) 
 114   
 115  HTMLCodes = {'#B0171F': ['Indian red', '2037680', ''], 
 116               '#DC143C': ['Crimson', '3937500', '#CC0033'], 
 117               '#FFB6C1': ['Lightpink', '12695295', '#FFCCCC'], 
 118               '#FFAEB9': ['Lightpink 1', '12168959', ''], 
 119               '#EEA2AD': ['Lightpink 2', '11379438', ''], 
 120               '#CD8C95': ['Lightpink 3', '9800909', ''], 
 121               '#8B5F65': ['Lightpink 4', '6643595', ''], 
 122               '#FFC0CB': ['Pink', '13353215', '#FFCCCC'], 
 123               '#FFB5C5': ['Pink 1', '12957183', ''], 
 124               '#EEA9B8': ['Pink 2', '12102126', ''], 
 125               '#CD919E': ['Pink 3', '10392013', ''], 
 126               '#8B636C': ['Pink 4', '7103371', ''], 
 127               '#DB7093': ['Palevioletred', '9662683', '#CC6699'], 
 128               '#FF82AB': ['Palevioletred 1', '11240191', ''], 
 129               '#EE799F': ['Palevioletred 2', '10451438', ''], 
 130               '#CD6889': ['Palevioletred 3', '9005261', ''], 
 131               '#8B475D': ['Palevioletred 4', '6113163', ''], 
 132               '#FFF0F5': ['Lavenderblush 1 (lavenderblush)', '16118015', '#FFFFFF'], 
 133               '#EEE0E5': ['Lavenderblush 2', '15065326', ''], 
 134               '#CDC1C5': ['Lavenderblush 3', '12960205', ''], 
 135               '#8B8386': ['Lavenderblush 4', '8815499', ''], 
 136               '#FF3E96': ['Violetred 1', '9846527', ''], 
 137               '#EE3A8C': ['Violetred 2', '9190126', ''], 
 138               '#CD3278': ['Violetred 3', '7877325', ''], 
 139               '#8B2252': ['Violetred 4', '5382795', ''], 
 140               '#FF69B4': ['Hotpink', '11823615', '#FF66CC'], 
 141               '#FF6EB4': ['Hotpink 1', '11824895', ''], 
 142               '#EE6AA7': ['Hotpink 2', '10971886', ''], 
 143               '#CD6090': ['Hotpink 3', '9461965', ''], 
 144               '#8B3A62': ['Hotpink 4', '6437515', ''], 
 145               '#872657': ['Raspberry', '5711495', ''], 
 146               '#FF1493': ['Deeppink 1 (deeppink)', '9639167', '#FF0099'], 
 147               '#EE1289': ['Deeppink 2', '8983278', ''], 
 148               '#CD1076': ['Deeppink 3', '7737549', ''], 
 149               '#8B0A50': ['Deeppink 4', '5245579', ''], 
 150               '#FF34B3': ['Maroon 1', '11744511', ''], 
 151               '#EE30A7': ['Maroon 2', '10957038', ''], 
 152               '#CD2990': ['Maroon 3', '9447885', ''], 
 153               '#8B1C62': ['Maroon 4', '6429835', ''], 
 154               '#C71585': ['Mediumvioletred', '8721863', '#CC0066'], 
 155               '#D02090': ['Violetred', '9445584', ''], 
 156               '#DA70D6': ['Orchid', '14053594', '#CC66CC'], 
 157               '#FF83FA': ['Orchid 1', '16417791', ''], 
 158               '#EE7AE9': ['Orchid 2', '15301358', ''], 
 159               '#CD69C9': ['Orchid 3', '13199821', ''], 
 160               '#8B4789': ['Orchid 4', '8996747', ''], 
 161               '#D8BFD8': ['Thistle', '14204888', '#CCCCCC'], 
 162               '#FFE1FF': ['Thistle 1', '16769535', ''], 
 163               '#EED2EE': ['Thistle 2', '15651566', ''], 
 164               '#CDB5CD': ['Thistle 3', '13481421', ''], 
 165               '#8B7B8B': ['Thistle 4', '9141131', ''], 
 166               '#FFBBFF': ['Plum 1', '16759807', ''], 
 167               '#EEAEEE': ['Plum 2', '15642350', ''], 
 168               '#CD96CD': ['Plum 3', '13473485', ''], 
 169               '#8B668B': ['Plum 4', '9135755', ''], 
 170               '#DDA0DD': ['Plum', '14524637', '#CC99CC'], 
 171               '#EE82EE': ['Violet', '15631086', '#FF99FF'], 
 172               '#FF00FF': ['Magenta (fuchsia)', '16711935', '#FF00FF'], 
 173               '#EE00EE': ['Magenta 2', '15597806', ''], 
 174               '#CD00CD': ['Magenta 3', '13435085', ''], 
 175               '#8B008B': ['Magenta 4 (darkmagenta)', '9109643', '#990099'], 
 176               '#800080': ['Purple', '8388736', '#990099'], 
 177               '#BA55D3': ['Mediumorchid', '13850042', '#CC66CC'], 
 178               '#E066FF': ['Mediumorchid 1', '16738016', ''], 
 179               '#D15FEE': ['Mediumorchid 2', '15622097', ''], 
 180               '#B452CD': ['Mediumorchid 3', '13456052', ''], 
 181               '#7A378B': ['Mediumorchid 4', '9123706', ''], 
 182               '#9400D3': ['Darkviolet', '13828244', '#9900CC'], 
 183               '#9932CC': ['Darkorchid', '13382297', '#9933CC'], 
 184               '#BF3EFF': ['Darkorchid 1', '16727743', ''], 
 185               '#B23AEE': ['Darkorchid 2', '15612594', ''], 
 186               '#9A32CD': ['Darkorchid 3', '13447834', ''], 
 187               '#68228B': ['Darkorchid 4', '9118312', ''], 
 188               '#4B0082': ['Indigo', '8519755', '#330099'], 
 189               '#8A2BE2': ['Blueviolet', '14822282', '#9933FF'], 
 190               '#9B30FF': ['Purple 1', '16724123', ''], 
 191               '#912CEE': ['Purple 2', '15608977', ''], 
 192               '#7D26CD': ['Purple 3', '13444733', ''], 
 193               '#551A8B': ['Purple 4', '9116245', ''], 
 194               '#9370DB': ['Mediumpurple', '14381203', '#9966CC'], 
 195               '#AB82FF': ['Mediumpurple 1', '16745131', ''], 
 196               '#9F79EE': ['Mediumpurple 2', '15628703', ''], 
 197               '#8968CD': ['Mediumpurple 3', '13461641', ''], 
 198               '#5D478B': ['Mediumpurple 4', '9127773', ''], 
 199               '#483D8B': ['Darkslateblue', '9125192', '#333399'], 
 200               '#8470FF': ['Lightslateblue', '16740484', ''], 
 201               '#7B68EE': ['Mediumslateblue', '15624315', '#6666FF'], 
 202               '#6A5ACD': ['Slateblue', '13458026', '#6666CC'], 
 203               '#836FFF': ['Slateblue 1', '16740227', ''], 
 204               '#7A67EE': ['Slateblue 2', '15624058', ''], 
 205               '#6959CD': ['Slateblue 3', '13457769', ''], 
 206               '#473C8B': ['Slateblue 4', '9124935', ''], 
 207               '#F8F8FF': ['Ghostwhite', '16775416', '#FFFFFF'], 
 208               '#E6E6FA': ['Lavender', '16443110', '#FFFFFF'], 
 209               '#0000FF': ['Blue', '16711680', '#0000FF'], 
 210               '#0000EE': ['Blue 2', '15597568', ''], 
 211               '#0000CD': ['Blue 3 (mediumblue)', '13434880', '#0000CC'], 
 212               '#00008B': ['Blue 4 (darkblue)', '9109504', '#000099'], 
 213               '#000080': ['Navy', '8388608', '#000099'], 
 214               '#191970': ['Midnightblue', '7346457', '#000066'], 
 215               '#3D59AB': ['Cobalt', '11229501', ''], 
 216               '#4169E1': ['Royalblue', '14772545', '#3366CC'], 
 217               '#4876FF': ['Royalblue 1', '16741960', ''], 
 218               '#436EEE': ['Royalblue 2', '15625795', ''], 
 219               '#3A5FCD': ['Royalblue 3', '13459258', ''], 
 220               '#27408B': ['Royalblue 4', '9125927', ''], 
 221               '#6495ED': ['Cornflowerblue', '15570276', '#6699FF'], 
 222               '#B0C4DE': ['Lightsteelblue', '14599344', '#99CCCC'], 
 223               '#CAE1FF': ['Lightsteelblue 1', '16769482', ''], 
 224               '#BCD2EE': ['Lightsteelblue 2', '15651516', ''], 
 225               '#A2B5CD': ['Lightsteelblue 3', '13481378', ''], 
 226               '#6E7B8B': ['Lightsteelblue 4', '9141102', ''], 
 227               '#778899': ['Lightslategray', '10061943', '#669999'], 
 228               '#708090': ['Slategray', '9470064', '#669999'], 
 229               '#C6E2FF': ['Slategray 1', '16769734', ''], 
 230               '#B9D3EE': ['Slategray 2', '15651769', ''], 
 231               '#9FB6CD': ['Slategray 3', '13481631', ''], 
 232               '#6C7B8B': ['Slategray 4', '9141100', ''], 
 233               '#1E90FF': ['Dodgerblue 1 (dodgerblue)', '16748574', '#3399FF'], 
 234               '#1C86EE': ['Dodgerblue 2', '15631900', ''], 
 235               '#1874CD': ['Dodgerblue 3', '13464600', ''], 
 236               '#104E8B': ['Dodgerblue 4', '9129488', ''], 
 237               '#F0F8FF': ['Aliceblue', '16775408', '#FFFFFF'], 
 238               '#4682B4': ['Steelblue', '11829830', '#3399CC'], 
 239               '#63B8FF': ['Steelblue 1', '16758883', ''], 
 240               '#5CACEE': ['Steelblue 2', '15641692', ''], 
 241               '#4F94CD': ['Steelblue 3', '13472847', ''], 
 242               '#36648B': ['Steelblue 4', '9135158', ''], 
 243               '#87CEFA': ['Lightskyblue', '16436871', '#99CCFF'], 
 244               '#B0E2FF': ['Lightskyblue 1', '16769712', ''], 
 245               '#A4D3EE': ['Lightskyblue 2', '15651748', ''], 
 246               '#8DB6CD': ['Lightskyblue 3', '13481613', ''], 
 247               '#607B8B': ['Lightskyblue 4', '9141088', ''], 
 248               '#87CEFF': ['Skyblue 1', '16764551', ''], 
 249               '#7EC0EE': ['Skyblue 2', '15646846', ''], 
 250               '#6CA6CD': ['Skyblue 3', '13477484', ''], 
 251               '#4A708B': ['Skyblue 4', '9138250', ''], 
 252               '#87CEEB': ['Skyblue', '15453831', '#99CCFF'], 
 253               '#00BFFF': ['Deepskyblue 1 (deepskyblue)', '16760576', '#00CCFF'], 
 254               '#00B2EE': ['Deepskyblue 2', '15643136', ''], 
 255               '#009ACD': ['Deepskyblue 3', '13474304', ''], 
 256               '#00688B': ['Deepskyblue 4', '9136128', ''], 
 257               '#33A1C9': ['Peacock', '13214003', ''], 
 258               '#ADD8E6': ['Lightblue', '15128749', '#99CCFF'], 
 259               '#BFEFFF': ['Lightblue 1', '16773055', ''], 
 260               '#B2DFEE': ['Lightblue 2', '15654834', ''], 
 261               '#9AC0CD': ['Lightblue 3', '13484186', ''], 
 262               '#68838B': ['Lightblue 4', '9143144', ''], 
 263               '#B0E0E6': ['Powderblue', '15130800', '#CCCCFF'], 
 264               '#98F5FF': ['Cadetblue 1', '16774552', ''], 
 265               '#8EE5EE': ['Cadetblue 2', '15656334', ''], 
 266               '#7AC5CD': ['Cadetblue 3', '13485434', ''], 
 267               '#53868B': ['Cadetblue 4', '9143891', ''], 
 268               '#00F5FF': ['Turquoise 1', '16774400', ''], 
 269               '#00E5EE': ['Turquoise 2', '15656192', ''], 
 270               '#00C5CD': ['Turquoise 3', '13485312', ''], 
 271               '#00868B': ['Turquoise 4', '9143808', ''], 
 272               '#5F9EA0': ['Cadetblue', '10526303', '#669999'], 
 273               '#00CED1': ['Darkturquoise', '13749760', '#00CCCC'], 
 274               '#F0FFFF': ['Azure 1 (azure)', '16777200', '#FFFFFF'], 
 275               '#E0EEEE': ['Azure 2', '15658720', ''], 
 276               '#C1CDCD': ['Azure 3', '13487553', ''], 
 277               '#838B8B': ['Azure 4', '9145219', ''], 
 278               '#E0FFFF': ['Lightcyan 1 (lightcyan)', '16777184', '#CCFFFF'], 
 279               '#D1EEEE': ['Lightcyan 2', '15658705', ''], 
 280               '#B4CDCD': ['Lightcyan 3', '13487540', ''], 
 281               '#7A8B8B': ['Lightcyan 4', '9145210', ''], 
 282               '#BBFFFF': ['Paleturquoise 1', '16777147', ''], 
 283               '#AEEEEE': ['Paleturquoise 2 (paleturquoise)', '15658670', ''], 
 284               '#96CDCD': ['Paleturquoise 3', '13487510', ''], 
 285               '#668B8B': ['Paleturquoise 4', '9145190', ''], 
 286               '#2F4F4F': ['Darkslategray', '5197615', '#336666'], 
 287               '#97FFFF': ['Darkslategray 1', '16777111', ''], 
 288               '#8DEEEE': ['Darkslategray 2', '15658637', ''], 
 289               '#79CDCD': ['Darkslategray 3', '13487481', ''], 
 290               '#528B8B': ['Darkslategray 4', '9145170', ''], 
 291               '#00FFFF': ['Cyan / aqua', '16776960', '#00FFFF'], 
 292               '#00EEEE': ['Cyan 2', '15658496', ''], 
 293               '#00CDCD': ['Cyan 3', '13487360', ''], 
 294               '#008B8B': ['Cyan 4 (darkcyan)', '9145088', '#009999'], 
 295               '#008080': ['Teal', '8421376', '#009999'], 
 296               '#48D1CC': ['Mediumturquoise', '13422920', '#33CCCC'], 
 297               '#20B2AA': ['Lightseagreen', '11186720', '#339999'], 
 298               '#03A89E': ['Manganeseblue', '10397699', ''], 
 299               '#40E0D0': ['Turquoise', '13688896', '#33CCCC'], 
 300               '#808A87': ['Coldgrey', '8882816', ''], 
 301               '#00C78C': ['Turquoiseblue', '9225984', ''], 
 302               '#7FFFD4': ['Aquamarine 1 (aquamarine)', '13959039', '#66FFCC'], 
 303               '#76EEC6': ['Aquamarine 2', '13037174', ''], 
 304               '#66CDAA': ['Aquamarine 3 (mediumaquamarine)', '11193702', '#66CC99'], 
 305               '#458B74': ['Aquamarine 4', '7637829', ''], 
 306               '#00FA9A': ['Mediumspringgreen', '10156544', '#00FF99'], 
 307               '#F5FFFA': ['Mintcream', '16449525', '#FFFFFF'], 
 308               '#00FF7F': ['Springgreen', '8388352', '#00FF66'], 
 309               '#00EE76': ['Springgreen 1', '7794176', ''], 
 310               '#00CD66': ['Springgreen 2', '6737152', ''], 
 311               '#008B45': ['Springgreen 3', '4557568', ''], 
 312               '#3CB371': ['Mediumseagreen', '7451452', '#33CC66'], 
 313               '#54FF9F': ['Seagreen 1', '10485588', ''], 
 314               '#4EEE94': ['Seagreen 2', '9760334', ''], 
 315               '#43CD80': ['Seagreen 3', '8441155', ''], 
 316               '#2E8B57': ['Seagreen 4 (seagreen)', '5737262', '#339966'], 
 317               '#00C957': ['Emeraldgreen', '5753088', ''], 
 318               '#BDFCC9': ['Mint', '13237437', ''], 
 319               '#3D9140': ['Cobaltgreen', '4231485', ''], 
 320               '#F0FFF0': ['Honeydew 1 (honeydew)', '15794160', '#FFFFFF'], 
 321               '#E0EEE0': ['Honeydew 2', '14741216', ''], 
 322               '#C1CDC1': ['Honeydew 3', '12701121', ''], 
 323               '#838B83': ['Honeydew 4', '8620931', ''], 
 324               '#8FBC8F': ['Darkseagreen', '9419919', '#99CC99'], 
 325               '#C1FFC1': ['Darkseagreen 1', '12713921', ''], 
 326               '#B4EEB4': ['Darkseagreen 2', '11857588', ''], 
 327               '#9BCD9B': ['Darkseagreen 3', '10210715', ''], 
 328               '#698B69': ['Darkseagreen 4', '6916969', ''], 
 329               '#98FB98': ['Palegreen', '10025880', '#99FF99'], 
 330               '#9AFF9A': ['Palegreen 1', '10157978', ''], 
 331               '#90EE90': ['Palegreen 2 (lightgreen)', '9498256', '#99FF99'], 
 332               '#7CCD7C': ['Palegreen 3', '8179068', ''], 
 333               '#548B54': ['Palegreen 4', '5540692', ''], 
 334               '#32CD32': ['Limegreen', '3329330', '#33CC33'], 
 335               '#228B22': ['Forestgreen', '2263842', '#339933'], 
 336               '#00FF00': ['Green 1 (lime)', '65280', '#00FF00'], 
 337               '#00EE00': ['Green 2', '60928', ''], 
 338               '#00CD00': ['Green 3', '52480', ''], 
 339               '#008B00': ['Green 4', '35584', ''], 
 340               '#008000': ['Green', '32768', '#009900'], 
 341               '#006400': ['Darkgreen', '25600', '#006600'], 
 342               '#308014': ['Sapgreen', '1343536', ''], 
 343               '#7CFC00': ['Lawngreen', '64636', '#66FF00'], 
 344               '#7FFF00': ['Chartreuse 1 (chartreuse)', '65407', '#66FF00'], 
 345               '#76EE00': ['Chartreuse 2', '61046', ''], 
 346               '#66CD00': ['Chartreuse 3', '52582', ''], 
 347               '#458B00': ['Chartreuse 4', '35653', ''], 
 348               '#ADFF2F': ['Greenyellow', '3145645', '#99FF33'], 
 349               '#CAFF70': ['Darkolivegreen 1', '7405514', ''], 
 350               '#BCEE68': ['Darkolivegreen 2', '6876860', ''], 
 351               '#A2CD5A': ['Darkolivegreen 3', '5950882', ''], 
 352               '#6E8B3D': ['Darkolivegreen 4', '4033390', ''], 
 353               '#556B2F': ['Darkolivegreen', '3107669', '#666633'], 
 354               '#6B8E23': ['Olivedrab', '2330219', '#669933'], 
 355               '#C0FF3E': ['Olivedrab 1', '4128704', ''], 
 356               '#B3EE3A': ['Olivedrab 2', '3862195', ''], 
 357               '#9ACD32': ['Olivedrab 3 (yellowgreen)', '3329434', '#99CC33'], 
 358               '#698B22': ['Olivedrab 4', '2263913', ''], 
 359               '#FFFFF0': ['Ivory 1 (ivory)', '15794175', '#FFFFFF'], 
 360               '#EEEEE0': ['Ivory 2', '14741230', ''], 
 361               '#CDCDC1': ['Ivory 3', '12701133', ''], 
 362               '#8B8B83': ['Ivory 4', '8620939', ''], 
 363               '#F5F5DC': ['Beige', '14480885', '#FFFFCC'], 
 364               '#FFFFE0': ['Lightyellow 1 (lightyellow)', '14745599', '#FFFFFF'], 
 365               '#EEEED1': ['Lightyellow 2', '13758190', ''], 
 366               '#CDCDB4': ['Lightyellow 3', '11849165', ''], 
 367               '#8B8B7A': ['Lightyellow 4', '8031115', ''], 
 368               '#FAFAD2': ['Lightgoldenrodyellow', '13826810', '#FFFFCC'], 
 369               '#FFFF00': ['Yellow 1 (yellow)', '65535', '#FFFF00'], 
 370               '#EEEE00': ['Yellow 2', '61166', ''], 
 371               '#CDCD00': ['Yellow 3', '52685', ''], 
 372               '#8B8B00': ['Yellow 4', '35723', ''], 
 373               '#808069': ['Warmgrey', '6914176', ''], 
 374               '#808000': ['Olive', '32896', '#999900'], 
 375               '#BDB76B': ['Darkkhaki', '7059389', '#CCCC66'], 
 376               '#FFF68F': ['Khaki 1', '9434879', ''], 
 377               '#EEE685': ['Khaki 2', '8775406', ''], 
 378               '#CDC673': ['Khaki 3', '7587533', ''], 
 379               '#8B864E': ['Khaki 4', '5146251', ''], 
 380               '#F0E68C': ['Khaki', '9234160', ''], 
 381               '#EEE8AA': ['Palegoldenrod', '11200750', '#FFFF99'], 
 382               '#FFFACD': ['Lemonchiffon 1 (lemonchiffon)', '13499135', '#FFFFCC'], 
 383               '#EEE9BF': ['Lemonchiffon 2', '12577262', ''], 
 384               '#CDC9A5': ['Lemonchiffon 3', '10865101', ''], 
 385               '#8B8970': ['Lemonchiffon 4', '7375243', ''], 
 386               '#FFEC8B': ['Lightgoldenrod 1', '9170175', ''], 
 387               '#EEDC82': ['Lightgoldenrod 2', '8576238', ''], 
 388               '#CDBE70': ['Lightgoldenrod 3', '7388877', ''], 
 389               '#8B814C': ['Lightgoldenrod 4', '5013899', ''], 
 390               '#E3CF57': ['Banana', '5754851', ''], 
 391               '#FFD700': ['Gold 1 (gold)', '55295', '#FFCC00'], 
 392               '#EEC900': ['Gold 2', '51694', ''], 
 393               '#CDAD00': ['Gold 3', '44493', ''], 
 394               '#8B7500': ['Gold 4', '30091', ''], 
 395               '#FFF8DC': ['Cornsilk 1 (cornsilk)', '14481663', '#FFFFCC'], 
 396               '#EEE8CD': ['Cornsilk 2', '13494510', ''], 
 397               '#CDC8B1': ['Cornsilk 3', '11651277', ''], 
 398               '#8B8878': ['Cornsilk 4', '7899275', ''], 
 399               '#DAA520': ['Goldenrod', '2139610', '#CC9933'], 
 400               '#FFC125': ['Goldenrod 1', '2474495', ''], 
 401               '#EEB422': ['Goldenrod 2', '2274542', ''], 
 402               '#CD9B1D': ['Goldenrod 3', '1940429', ''], 
 403               '#8B6914': ['Goldenrod 4', '1337739', ''], 
 404               '#B8860B': ['Darkgoldenrod', '755384', '#CC9900'], 
 405               '#FFB90F': ['Darkgoldenrod 1', '1030655', ''], 
 406               '#EEAD0E': ['Darkgoldenrod 2', '962030', ''], 
 407               '#CD950C': ['Darkgoldenrod 3', '824781', ''], 
 408               '#8B6508': ['Darkgoldenrod 4', '550283', ''], 
 409               '#FFA500': ['Orange 1 (orange)', '42495', '#FF9900'], 
 410               '#EE9A00': ['Orange 2', '39662', ''], 
 411               '#CD8500': ['Orange 3', '34253', ''], 
 412               '#8B5A00': ['Orange 4', '23179', ''], 
 413               '#FFFAF0': ['Floralwhite', '15792895', '#FFFFFF'], 
 414               '#FDF5E6': ['Oldlace', '15136253', '#FFFFFF'], 
 415               '#F5DEB3': ['Wheat', '11788021', '#FFCCCC'], 
 416               '#FFE7BA': ['Wheat 1', '12249087', ''], 
 417               '#EED8AE': ['Wheat 2', '11458798', ''], 
 418               '#CDBA96': ['Wheat 3', '9878221', ''], 
 419               '#8B7E66': ['Wheat 4', '6717067', ''], 
 420               '#FFE4B5': ['Moccasin', '11920639', '#FFCCCC'], 
 421               '#FFEFD5': ['Papayawhip', '14020607', '#FFFFCC'], 
 422               '#FFEBCD': ['Blanchedalmond', '13495295', '#FFFFCC'], 
 423               '#FFDEAD': ['Navajowhite 1 (navajowhite)', '11394815', '#FFCC99'], 
 424               '#EECFA1': ['Navajowhite 2', '10604526', ''], 
 425               '#CDB38B': ['Navajowhite 3', '9155533', ''], 
 426               '#8B795E': ['Navajowhite 4', '6191499', ''], 
 427               '#FCE6C9': ['Eggshell', '13231868', ''], 
 428               '#D2B48C': ['Tan', '9221330', '#CCCC99'], 
 429               '#9C661F': ['Brick', '2057884', ''], 
 430               '#FF9912': ['Cadmiumyellow', '1219071', ''], 
 431               '#FAEBD7': ['Antiquewhite', '14150650', '#FFFFCC'], 
 432               '#FFEFDB': ['Antiquewhite 1', '14413823', ''], 
 433               '#EEDFCC': ['Antiquewhite 2', '13426670', ''], 
 434               '#CDC0B0': ['Antiquewhite 3', '11583693', ''], 
 435               '#8B8378': ['Antiquewhite 4', '7897995', ''], 
 436               '#DEB887': ['Burlywood', '8894686', '#CCCC99'], 
 437               '#FFD39B': ['Burlywood 1', '10212351', ''], 
 438               '#EEC591': ['Burlywood 2', '9553390', ''], 
 439               '#CDAA7D': ['Burlywood 3', '8235725', ''], 
 440               '#8B7355': ['Burlywood 4', '5600139', ''], 
 441               '#FFE4C4': ['Bisque 1 (bisque)', '12903679', '#FFFFCC'], 
 442               '#EED5B7': ['Bisque 2', '12047854', ''], 
 443               '#CDB79E': ['Bisque 3', '10401741', ''], 
 444               '#8B7D6B': ['Bisque 4', '7044491', ''], 
 445               '#E3A869': ['Melon', '6924515', ''], 
 446               '#ED9121': ['Carrot', '2200045', ''], 
 447               '#FF8C00': ['Darkorange', '36095', '#FF9900'], 
 448               '#FF7F00': ['Darkorange 1', '32767', ''], 
 449               '#EE7600': ['Darkorange 2', '30446', ''], 
 450               '#CD6600': ['Darkorange 3', '26317', ''], 
 451               '#8B4500': ['Darkorange 4', '17803', ''], 
 452               '#FF8000': ['Orange', '33023', ''], 
 453               '#FFA54F': ['Tan 1', '5219839', ''], 
 454               '#EE9A49': ['Tan 2', '4823790', ''], 
 455               '#CD853F': ['Tan 3 (peru)', '4163021', '#CC9933'], 
 456               '#8B5A2B': ['Tan 4', '2841227', ''], 
 457               '#FAF0E6': ['Linen', '15134970', '#FFFFFF'], 
 458               '#FFDAB9': ['Peachpuff 1 (peachpuff)', '12180223', '#FFCCCC'], 
 459               '#EECBAD': ['Peachpuff 2', '11389934', ''], 
 460               '#CDAF95': ['Peachpuff 3', '9809869', ''], 
 461               '#8B7765': ['Peachpuff 4', '6649739', ''], 
 462               '#FFF5EE': ['Seashell 1 (seashell)', '15660543', '#FFFFFF'], 
 463               '#EEE5DE': ['Seashell 2', '14607854', ''], 
 464               '#CDC5BF': ['Seashell 3', '12568013', ''], 
 465               '#8B8682': ['Seashell 4', '8554123', ''], 
 466               '#F4A460': ['Sandybrown', '6333684', '#FF9966'], 
 467               '#C76114': ['Rawsienna', '1335751', ''], 
 468               '#D2691E': ['Chocolate', '1993170', '#CC6633'], 
 469               '#FF7F24': ['Chocolate 1', '2392063', ''], 
 470               '#EE7621': ['Chocolate 2', '2193134', ''], 
 471               '#CD661D': ['Chocolate 3', '1926861', ''], 
 472               '#8B4513': ['Chocolate 4 (saddlebrown)', '1262987', '#993300'], 
 473               '#292421': ['Ivoryblack', '2171945', ''], 
 474               '#FF7D40': ['Flesh', '4226559', ''], 
 475               '#FF6103': ['Cadmiumorange', '221695', ''], 
 476               '#8A360F': ['Burntsienna', '997002', ''], 
 477               '#A0522D': ['Sienna', '2970272', '#996633'], 
 478               '#FF8247': ['Sienna 1', '4686591', ''], 
 479               '#EE7942': ['Sienna 2', '4356590', ''], 
 480               '#CD6839': ['Sienna 3', '3762381', ''], 
 481               '#8B4726': ['Sienna 4', '2508683', ''], 
 482               '#FFA07A': ['Lightsalmon 1 (lightsalmon)', '8036607', '#FF9966'], 
 483               '#EE9572': ['Lightsalmon 2', '7509486', ''], 
 484               '#CD8162': ['Lightsalmon 3', '6455757', ''], 
 485               '#8B5742': ['Lightsalmon 4', '4347787', ''], 
 486               '#FF7F50': ['Coral', '5275647', '#FF6666'], 
 487               '#FF4500': ['Orangered 1 (orangered)', '17919', '#FF3300'], 
 488               '#EE4000': ['Orangered 2', '16622', ''], 
 489               '#CD3700': ['Orangered 3', '14285', ''], 
 490               '#8B2500': ['Orangered 4', '9611', ''], 
 491               '#5E2612': ['Sepia', '1189470', ''], 
 492               '#E9967A': ['Darksalmon', '8034025', '#FF9966'], 
 493               '#FF8C69': ['Salmon 1', '6917375', ''], 
 494               '#EE8262': ['Salmon 2', '6456046', ''], 
 495               '#CD7054': ['Salmon 3', '5533901', ''], 
 496               '#8B4C39': ['Salmon 4', '3755147', ''], 
 497               '#FF7256': ['Coral 1', '5665535', ''], 
 498               '#EE6A50': ['Coral 2', '5270254', ''], 
 499               '#CD5B45': ['Coral 3', '4545485', ''], 
 500               '#8B3E2F': ['Coral 4', '3096203', ''], 
 501               '#8A3324': ['Burntumber', '2372490', ''], 
 502               '#FF6347': ['Tomato 1 (tomato)', '4678655', '#FF6633'], 
 503               '#EE5C42': ['Tomato 2', '4349166', ''], 
 504               '#CD4F39': ['Tomato 3', '3755981', ''], 
 505               '#8B3626': ['Tomato 4', '2504331', ''], 
 506               '#FA8072': ['Salmon', '7504122', '#FF9966'], 
 507               '#FFE4E1': ['Mistyrose 1 (mistyrose)', '14804223', '#FFCCFF'], 
 508               '#EED5D2': ['Mistyrose 2', '13817326', ''], 
 509               '#CDB7B5': ['Mistyrose 3', '11909069', ''], 
 510               '#8B7D7B': ['Mistyrose 4', '8093067', ''], 
 511               '#FFFAFA': ['Snow 1 (snow)', '16448255', '#FFFFFF'], 
 512               '#EEE9E9': ['Snow 2', '15329774', ''], 
 513               '#CDC9C9': ['Snow 3', '13224397', ''], 
 514               '#8B8989': ['Snow 4', '9013643', ''], 
 515               '#BC8F8F': ['Rosybrown', '9408444', '#CC9999'], 
 516               '#FFC1C1': ['Rosybrown 1', '12698111', ''], 
 517               '#EEB4B4': ['Rosybrown 2', '11842798', ''], 
 518               '#CD9B9B': ['Rosybrown 3', '10197965', ''], 
 519               '#8B6969': ['Rosybrown 4', '6908299', ''], 
 520               '#F08080': ['Lightcoral', '8421616', '#FF9999'], 
 521               '#CD5C5C': ['Indianred', '6053069', '#CC6666'], 
 522               '#FF6A6A': ['Indianred 1', '6974207', ''], 
 523               '#EE6363': ['Indianred 2', '6513646', ''], 
 524               '#8B3A3A': ['Indianred 4', '3816075', ''], 
 525               '#CD5555': ['Indianred 3', '5592525', ''], 
 526               '#A52A2A': ['Brown', '2763429', '#993333'], 
 527               '#FF4040': ['Brown 1', '4210943', ''], 
 528               '#EE3B3B': ['Brown 2', '3881966', ''], 
 529               '#CD3333': ['Brown 3', '3355597', ''], 
 530               '#8B2323': ['Brown 4', '2302859', ''], 
 531               '#B22222': ['Firebrick', '2237106', '#993333'], 
 532               '#FF3030': ['Firebrick 1', '3158271', ''], 
 533               '#EE2C2C': ['Firebrick 2', '2895086', ''], 
 534               '#CD2626': ['Firebrick 3', '2500301', ''], 
 535               '#8B1A1A': ['Firebrick 4', '1710731', ''], 
 536               '#FF0000': ['Red 1 (red)', '255', '#FF0000'], 
 537               '#EE0000': ['Red 2', '238', ''], 
 538               '#CD0000': ['Red 3', '205', ''], 
 539               '#8B0000': ['Red 4 (darkred)', '139', '#990000'], 
 540               '#800000': ['Maroon', '128', '#990000'], 
 541               '#8E388E': ['Sgi beet', '9320590', ''], 
 542               '#7171C6': ['Sgi slateblue', '13005169', ''], 
 543               '#7D9EC0': ['Sgi lightblue', '12623485', ''], 
 544               '#388E8E': ['Sgi teal', '9342520', ''], 
 545               '#71C671': ['Sgi chartreuse', '7456369', ''], 
 546               '#8E8E38': ['Sgi olivedrab', '3706510', ''], 
 547               '#C5C1AA': ['Sgi brightgray', '11190725', ''], 
 548               '#C67171': ['Sgi salmon', '7434694', ''], 
 549               '#555555': ['Sgi darkgray', '5592405', ''], 
 550               '#1E1E1E': ['Sgi gray 12', '1973790', ''], 
 551               '#282828': ['Sgi gray 16', '2631720', ''], 
 552               '#515151': ['Sgi gray 32', '5329233', ''], 
 553               '#5B5B5B': ['Sgi gray 36', '5987163', ''], 
 554               '#848484': ['Sgi gray 52', '8684676', ''], 
 555               '#8E8E8E': ['Sgi gray 56', '9342606', ''], 
 556               '#AAAAAA': ['Sgi lightgray', '11184810', ''], 
 557               '#B7B7B7': ['Sgi gray 72', '12040119', ''], 
 558               '#C1C1C1': ['Sgi gray 76', '12698049', ''], 
 559               '#EAEAEA': ['Sgi gray 92', '15395562', ''], 
 560               '#F4F4F4': ['Sgi gray 96', '16053492', ''], 
 561               '#FFFFFF': ['White', '16777215', '#FFFFFF'], 
 562               '#F5F5F5': ['White smoke (gray)', '16119285', '#FFFFFF'], 
 563               '#DCDCDC': ['Gainsboro', '14474460', '#CCCCCC'], 
 564               '#D3D3D3': ['Lightgrey', '13882323', '#CCCCCC'], 
 565               '#C0C0C0': ['Silver', '12632256', '#CCCCCC'], 
 566               '#A9A9A9': ['Darkgray', '11119017', '#999999'], 
 567               '#808080': ['Gray', '8421504', ''], 
 568               '#696969': ['Dimgray (gray 42)', '6908265', '#666666'], 
 569               '#000000': ['Black', '0', '#000000'], 
 570               '#FCFCFC': ['Gray 99', '16579836', ''], 
 571               '#FAFAFA': ['Gray 98', '16448250', ''], 
 572               '#F7F7F7': ['Gray 97', '16250871', ''], 
 573               '#F2F2F2': ['Gray 95', '15921906', ''], 
 574               '#F0F0F0': ['Gray 94', '15790320', ''], 
 575               '#EDEDED': ['Gray 93', '15592941', ''], 
 576               '#EBEBEB': ['Gray 92', '15461355', ''], 
 577               '#E8E8E8': ['Gray 91', '15263976', ''], 
 578               '#E5E5E5': ['Gray 90', '15066597', ''], 
 579               '#E3E3E3': ['Gray 89', '14935011', ''], 
 580               '#E0E0E0': ['Gray 88', '14737632', ''], 
 581               '#DEDEDE': ['Gray 87', '14606046', ''], 
 582               '#DBDBDB': ['Gray 86', '14408667', ''], 
 583               '#D9D9D9': ['Gray 85', '14277081', ''], 
 584               '#D6D6D6': ['Gray 84', '14079702', ''], 
 585               '#D4D4D4': ['Gray 83', '13948116', ''], 
 586               '#D1D1D1': ['Gray 82', '13750737', ''], 
 587               '#CFCFCF': ['Gray 81', '13619151', ''], 
 588               '#CCCCCC': ['Gray 80', '13421772', ''], 
 589               '#C9C9C9': ['Gray 79', '13224393', ''], 
 590               '#C7C7C7': ['Gray 78', '13092807', ''], 
 591               '#C4C4C4': ['Gray 77', '12895428', ''], 
 592               '#C2C2C2': ['Gray 76', '12763842', ''], 
 593               '#BFBFBF': ['Gray 75', '12566463', ''], 
 594               '#BDBDBD': ['Gray 74', '12434877', ''], 
 595               '#BABABA': ['Gray 73', '12237498', ''], 
 596               '#B8B8B8': ['Gray 72', '12105912', ''], 
 597               '#B5B5B5': ['Gray 71', '11908533', ''], 
 598               '#B3B3B3': ['Gray 70', '11776947', ''], 
 599               '#B0B0B0': ['Gray 69', '11579568', ''], 
 600               '#ADADAD': ['Gray 68', '11382189', ''], 
 601               '#ABABAB': ['Gray 67', '11250603', ''], 
 602               '#A8A8A8': ['Gray 66', '11053224', ''], 
 603               '#A6A6A6': ['Gray 65', '10921638', ''], 
 604               '#A3A3A3': ['Gray 64', '10724259', ''], 
 605               '#A1A1A1': ['Gray 63', '10592673', ''], 
 606               '#9E9E9E': ['Gray 62', '10395294', ''], 
 607               '#9C9C9C': ['Gray 61', '10263708', ''], 
 608               '#999999': ['Gray 60', '10066329', ''], 
 609               '#969696': ['Gray 59', '9868950', ''], 
 610               '#949494': ['Gray 58', '9737364', ''], 
 611               '#919191': ['Gray 57', '9539985', ''], 
 612               '#8F8F8F': ['Gray 56', '9408399', ''], 
 613               '#8C8C8C': ['Gray 55', '9211020', ''], 
 614               '#8A8A8A': ['Gray 54', '9079434', ''], 
 615               '#878787': ['Gray 53', '8882055', ''], 
 616               '#858585': ['Gray 52', '8750469', ''], 
 617               '#828282': ['Gray 51', '8553090', ''], 
 618               '#7F7F7F': ['Gray 50', '8355711', ''], 
 619               '#7D7D7D': ['Gray 49', '8224125', ''], 
 620               '#7A7A7A': ['Gray 48', '8026746', ''], 
 621               '#787878': ['Gray 47', '7895160', ''], 
 622               '#757575': ['Gray 46', '7697781', ''], 
 623               '#737373': ['Gray 45', '7566195', ''], 
 624               '#707070': ['Gray 44', '7368816', ''], 
 625               '#6E6E6E': ['Gray 43', '7237230', ''], 
 626               '#6B6B6B': ['Gray 42', '7039851', ''], 
 627               '#696969': ['Dimgray (gray 42)', '6908265', '#666666'], 
 628               '#666666': ['Gray 40', '6710886', ''], 
 629               '#636363': ['Gray 39', '6513507', ''], 
 630               '#616161': ['Gray 38', '6381921', ''], 
 631               '#5E5E5E': ['Gray 37', '6184542', ''], 
 632               '#5C5C5C': ['Gray 36', '6052956', ''], 
 633               '#595959': ['Gray 35', '5855577', ''], 
 634               '#575757': ['Gray 34', '5723991', ''], 
 635               '#545454': ['Gray 33', '5526612', ''], 
 636               '#525252': ['Gray 32', '5395026', ''], 
 637               '#4F4F4F': ['Gray 31', '5197647', ''], 
 638               '#4D4D4D': ['Gray 30', '5066061', ''], 
 639               '#4A4A4A': ['Gray 29', '4868682', ''], 
 640               '#474747': ['Gray 28', '4671303', ''], 
 641               '#454545': ['Gray 27', '4539717', ''], 
 642               '#424242': ['Gray 26', '4342338', ''], 
 643               '#404040': ['Gray 25', '4210752', ''], 
 644               '#3D3D3D': ['Gray 24', '4013373', ''], 
 645               '#3B3B3B': ['Gray 23', '3881787', ''], 
 646               '#383838': ['Gray 22', '3684408', ''], 
 647               '#363636': ['Gray 21', '3552822', ''], 
 648               '#333333': ['Gray 20', '3355443', ''], 
 649               '#303030': ['Gray 19', '3158064', ''], 
 650               '#2E2E2E': ['Gray 18', '3026478', ''], 
 651               '#2B2B2B': ['Gray 17', '2829099', ''], 
 652               '#292929': ['Gray 16', '2697513', ''], 
 653               '#262626': ['Gray 15', '2500134', ''], 
 654               '#242424': ['Gray 14', '2368548', ''], 
 655               '#212121': ['Gray 13', '2171169', ''], 
 656               '#1F1F1F': ['Gray 12', '2039583', ''], 
 657               '#1C1C1C': ['Gray 11', '1842204', ''], 
 658               '#1A1A1A': ['Gray 10', '1710618', ''], 
 659               '#171717': ['Gray 9', '1513239', ''], 
 660               '#141414': ['Gray 8', '1315860', ''], 
 661               '#121212': ['Gray 7', '1184274', ''], 
 662               '#0F0F0F': ['Gray 6', '986895', ''], 
 663               '#0D0D0D': ['Gray 5', '855309', ''], 
 664               '#0A0A0A': ['Gray 4', '657930', ''], 
 665               '#080808': ['Gray 3', '526344', ''], 
 666               '#050505': ['Gray 2', '328965', ''], 
 667               '#030303': ['Gray 1', '197379', ''], 
 668               } 
 669   
 670   
671 -def rad2deg(x):
672 """ Transforms radians into degrees. """ 673 return 180.0*x/pi
674
675 -def deg2rad(x):
676 """ Transforms degrees into radians. """ 677 return x*pi/180.0
678
679 -def toscale(x):
680 """ Normalize a value as a function of the radius. """ 681 return x*RADIUS/255.0
682
683 -def scaletomax(x):
684 """ Normalize a value as a function of the radius. """ 685 return x*255.0/RADIUS
686
687 -def rgb2html(colour):
688 """ Transforms a RGB triplet into an html hex string. """ 689 hexColour = "#%02x%02x%02x"%(colour.r, colour.g, colour.b) 690 return hexColour.upper()
691 692
693 -def Slope(pt1, pt2):
694 """ Calculates the slope of the line connecting 2 points. """ 695 696 y = float(pt2.y - pt1.y) 697 x = float(pt2.x - pt1.x) 698 699 if x: 700 return y/x 701 else: 702 return None
703 704
705 -def Intersection(line1, line2):
706 """ Calculates the intersection point between 2 lines. """ 707 708 if line1.slope == line2.slope: 709 710 # Parallel lines, no intersection 711 return wx.Point(0, 0) 712 713 elif line1.slope is None: 714 715 # First Line is vertical, eqn is x=0 716 # Put x = 0 in second line eqn to get y 717 x = line1.x 718 y = line2.slope*x + line2.c 719 720 elif line2.slope is None: 721 722 # second line is vertical Equation of line is x=0 723 # Put x = 0 in first line eqn to get y 724 x = line2.x 725 y = line1.slope*line2.x + line1.c 726 727 else: 728 729 y = ((line1.c*line2.slope) - (line2.c*line1.slope))/(line2.slope - line1.slope) 730 x = (y - line1.c)/line1.slope 731 732 733 return wx.Point(int(x), int(y))
734 735
736 -def FindC(line):
737 """ Internal function. """ 738 739 if line.slope is None: 740 c = line.y 741 else: 742 c = line.y - line.slope*line.x 743 744 return c
745 746
747 -def PointOnLine(pt1, pt2, length, maxLen):
748 """ Internal function. """ 749 750 a = float(length) 751 752 if pt2.x != pt1.x: 753 754 m = float((pt2.y - pt1.y))/(pt2.x - pt1.x) 755 m2 = m*m 756 a2 = a*a 757 c = pt1.y - m*pt1.x 758 c2 = c*c 759 760 A = 1.0 761 762 x = pt1.x 763 764 B = 2.0 * pt1.x 765 766 x *= x 767 C = x - a2/(m2 + 1) 768 769 x = (B + sqrt(B*B - (4.0*A*C)))/(2.0*A) 770 y = m*x + c 771 pt = wx.Point(int(x), int(y)) 772 773 if Distance(pt, pt1) > maxLen or Distance(pt, pt2) > maxLen: 774 775 x = (B - sqrt(B*B - (4.0*A*C)))/(2.0*A) 776 y = m*x + c 777 pt = wx.Point(int(x), int(y)) 778 779 780 else: 781 782 a2 = a*a 783 y = sqrt(a2) 784 x = 0.0 785 pt = wx.Point(int(x), int(y)) 786 pt.x += pt1.x 787 pt.y += pt1.y 788 789 if Distance(pt, pt1) > maxLen or Distance(pt, pt2) > maxLen: 790 791 y = -1.0*y 792 pt = wx.Point(int(x), int(y)) 793 pt.x += pt1.x 794 pt.y += pt1.y 795 796 return pt
797 798
799 -def Distance(pt1, pt2):
800 """ Returns the distance between 2 points. """ 801 802 distance = sqrt((pt1.x - pt2.x)**2.0 + (pt1.y - pt2.y)**2.0) 803 return int(distance)
804 805
806 -def AngleFromPoint(pt, center):
807 """ 808 Returns the angle between the x-axis and the line connecting the center and 809 the point pt. 810 """ 811 812 y = -1*(pt.y - center.y) 813 x = pt.x - center.x 814 if x == 0 and y == 0: 815 816 return 0.0 817 818 else: 819 820 return atan2(y, x)
821 822
823 -def PtFromAngle(angle, sat, center):
824 """ 825 Given the angle with respect to the x-axis, returns the point based on 826 the saturation value. 827 """ 828 829 angle = deg2rad(angle) 830 sat = toscale(sat) 831 832 x = sat*cos(angle) 833 y = sat*sin(angle) 834 835 pt = wx.Point(int(x), -int(y)) 836 pt.x += center.x 837 pt.y += center.y 838 839 return pt
840 841
842 -def RestoreOldDC(dc, oldPen, oldBrush, oldMode):
843 """ Restores the old settings for a wx.DC. """ 844 845 dc.SetPen(oldPen) 846 dc.SetBrush(oldBrush) 847 dc.SetLogicalFunction(oldMode)
848 849
850 -def DrawCheckerBoard(dc, rect, checkColour, box=5):
851 """ 852 Draws a checkerboard on a wx.DC. 853 Used for the Alpha channel control and the colour panels. 854 """ 855 856 y = rect.y 857 checkPen = wx.Pen(checkColour) 858 checkBrush = wx.Brush(checkColour) 859 860 dc.SetPen(checkPen) 861 dc.SetBrush(checkBrush) 862 dc.SetClippingRect(rect) 863 864 while y < rect.height: 865 x = box*((y/box)%2) + 2 866 while x < rect.width: 867 dc.DrawRectangle(x, y, box, box) 868 x += box*2 869 y += box
870 871 872
873 -class Colour(wx.Colour):
874 """ 875 This is a subclass of wx.Colour, which adds Hue, Saturation and Brightness 876 capability to the base class. It contains also methods to convert RGB triplets 877 into HSB triplets and vice-versa. 878 """ 879
880 - def __init__(self, colour):
881 """ 882 Default class constructor. 883 884 colour is a standard wxPython colour. 885 """ 886 887 wx.Colour.__init__(self) 888 889 self.r = colour.Red() 890 self.g = colour.Green() 891 self.b = colour.Blue() 892 self.alpha = colour.Alpha() 893 894 self.ToHSV()
895 896
897 - def ToRGB(self):
898 """ Converts a HSV triplet into a RGB triplet. """ 899 900 maxVal = self.v 901 delta = (maxVal*self.s)/255.0 902 minVal = maxVal - delta 903 904 hue = float(self.h) 905 906 if self.h > 300 or self.h <= 60: 907 908 self.r = maxVal 909 910 if self.h > 300: 911 912 self.g = int(minVal) 913 hue = (hue - 360.0)/60.0 914 self.b = int(-(hue*delta - minVal)) 915 916 else: 917 918 self.b = int(minVal) 919 hue = hue/60.0 920 self.g = int(hue*delta + minVal) 921 922 elif self.h > 60 and self.h < 180: 923 924 self.g = int(maxVal) 925 926 if self.h < 120: 927 928 self.b = int(minVal) 929 hue = (hue/60.0 - 2.0)*delta 930 self.r = int(minVal - hue) 931 932 else: 933 934 self.r = int(minVal) 935 hue = (hue/60.0 - 2.0)*delta 936 self.b = int(minVal + hue) 937 938 939 else: 940 941 self.b = int(maxVal) 942 943 if self.h < 240: 944 945 self.r = int(minVal) 946 hue = (hue/60.0 - 4.0)*delta 947 self.g = int(minVal - hue) 948 949 else: 950 951 self.g = int(minVal) 952 hue = (hue/60.0 - 4.0)*delta 953 self.r = int(minVal + hue)
954 955
956 - def ToHSV(self):
957 """ Converts a RGB triplet into a HSV triplet. """ 958 959 minVal = float(min(self.r, min(self.g, self.b))) 960 maxVal = float(max(self.r, max(self.g, self.b))) 961 delta = maxVal - minVal 962 963 self.v = int(maxVal) 964 965 if abs(delta) < 1e-6: 966 967 self.h = self.s = 0 968 969 else: 970 971 temp = delta/maxVal 972 self.s = int(temp*255.0) 973 974 if self.r == int(maxVal): 975 976 temp = float(self.g-self.b)/delta 977 978 elif self.g == int(maxVal): 979 980 temp = 2.0 + (float(self.b-self.r)/delta) 981 982 else: 983 984 temp = 4.0 + (float(self.r-self.g)/delta) 985 986 temp *= 60 987 if temp < 0: 988 989 temp += 360 990 991 elif temp >= 360.0: 992 993 temp = 0 994 995 self.h = int(temp)
996 997
998 - def GetPyColour(self):
999 """ Returns the wxPython wx.Colour associated with this instance. """ 1000 1001 return wx.Colour(self.r, self.g, self.b, self.alpha)
1002 1003 1004
1005 -class LineDescription(object):
1006 """ Simple class to store description and constants for a line in 2D space. """ 1007
1008 - def __init__(self, x=0, y=0, slope=None, c=None):
1009 """ 1010 Default class constructor. 1011 Used internally. Do not call it in your code! 1012 """ 1013 1014 self.x = x 1015 self.y = y 1016 self.slope = slope 1017 self.c = c
1018 1019 1020
1021 -class BasePyControl(wx.PyControl):
1022 """ 1023 Base class used to hold common code for the HSB colour wheel and the RGB 1024 colour cube. 1025 """ 1026
1027 - def __init__(self, parent, bitmap=None):
1028 """ 1029 Default class constructor. 1030 Used internally. Do not call it in your code! 1031 """ 1032 1033 wx.PyControl.__init__(self, parent, style=wx.NO_BORDER) 1034 self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM) 1035 1036 self._bitmap = wx.Bitmap(bitmap, wx.BITMAP_TYPE_PNG) 1037 mask = wx.Mask(self._bitmap, wx.Colour(192, 192, 192)) 1038 self._bitmap.SetMask(mask) 1039 1040 self._mainDialog = wx.GetTopLevelParent(self) 1041 1042 self.Bind(wx.EVT_SIZE, self.OnSize) 1043 self.Bind(wx.EVT_PAINT, self.OnPaint) 1044 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) 1045 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) 1046 self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) 1047 self.Bind(wx.EVT_MOTION, self.OnMotion)
1048 1049
1050 - def OnPaint(self, event):
1051 """ Handles the wx.EVT_PAINT for L{BasePyControl}. """ 1052 1053 dc = wx.AutoBufferedPaintDC(self) 1054 dc.SetBackground(wx.Brush(self.GetParent().GetBackgroundColour())) 1055 1056 dc.Clear() 1057 dc.DrawBitmap(self._bitmap, 0, 0, True) 1058 1059 if self._mainDialog._initOver: 1060 self.DrawMarkers(dc)
1061 1062
1063 - def OnEraseBackground(self, event):
1064 """ Handles the wx.EVT_ERASE_BACKGROUND for L{BasePyControl}. """ 1065 1066 pass
1067 1068
1069 - def DrawMarkers(self, dc=None):
1070 """ Empty method. Must be overridden in subclasses. """ 1071 1072 pass
1073 1074
1075 - def DrawLines(self, dc):
1076 """ Empty method. Must be overridden in subclasses. """ 1077 1078 pass
1079 1080
1081 - def AcceptsFocusFromKeyboard(self):
1082 """ We do not accept focus from keyboard. """ 1083 1084 return False
1085 1086
1087 - def AcceptFocus(self):
1088 """ We do not accept mouse focus. """ 1089 1090 return False
1091 1092
1093 - def OnLeftDown(self, event):
1094 """ 1095 Handles the wx.EVT_LEFT_DOWN for L{BasePyControl}. 1096 Must be overridden in subclasses. 1097 """ 1098 1099 pass
1100 1101
1102 - def OnLeftUp(self, event):
1103 """ 1104 Handles the wx.EVT_LEFT_UP for L{BasePyControl}. 1105 Must be overridden in subclasses. 1106 """ 1107 1108 pass
1109 1110
1111 - def OnMotion(self, event):
1112 """ 1113 Handles the wx.EVT_MOTION for L{BasePyControl}. 1114 Must be overridden in subclasses. 1115 """ 1116 1117 pass
1118 1119
1120 - def OnSize(self, event):
1121 """ Handles the wx.EVT_SIZE event for L{BasePyControl}. """ 1122 1123 self.Refresh()
1124 1125
1126 - def DoGetBestSize(self):
1127 """ Returns the custom control best size (used by sizers). """ 1128 1129 return wx.Size(self._bitmap.GetWidth(), self._bitmap.GetHeight())
1130 1131 1132
1133 -class RGBCube(BasePyControl):
1134 """ 1135 Implements the drawing, mouse handling and sizing routines for the RGB 1136 cube colour. 1137 """ 1138
1139 - def __init__(self, parent):
1140 """ 1141 Default class constructor. 1142 Used internally. Do not call it in your code! 1143 """ 1144 1145 BasePyControl.__init__(self, parent, bitmap="RGBCube.png") 1146 self._index = -1
1147 1148
1149 - def DrawMarkers(self, dc=None):
1150 """ Draws the square markers used with mouse gestures. """ 1151 1152 if dc is None: 1153 dc = wx.ClientDC(self) 1154 1155 oldPen, oldBrush, oldMode = dc.GetPen(), dc.GetBrush(), dc.GetLogicalFunction() 1156 dc.SetPen(wx.WHITE_PEN) 1157 dc.SetBrush(wx.TRANSPARENT_BRUSH) 1158 dc.SetLogicalFunction(wx.XOR) 1159 1160 rects = [] 1161 blueLen = self._mainDialog._blueLen 1162 greenLen = self._mainDialog._greenLen 1163 redLen = self._mainDialog._redLen 1164 colour = self._mainDialog._colour 1165 1166 pt = [wx.Point() for i in xrange(3)] 1167 pt[0] = PointOnLine(Vertex, Top, (colour.r*redLen)/255, redLen) 1168 pt[1] = PointOnLine(Vertex, Left, (colour.g*greenLen)/255, greenLen) 1169 pt[2] = PointOnLine(Vertex, Right, (colour.b*blueLen)/255, blueLen) 1170 1171 for i in xrange(3): 1172 rect = wx.Rect(pt[i].x - RECT_WIDTH, pt[i].y - RECT_WIDTH, 2*RECT_WIDTH, 2*RECT_WIDTH) 1173 rects.append(rect) 1174 dc.DrawRectangleRect(rect) 1175 1176 self.DrawLines(dc) 1177 RestoreOldDC(dc, oldPen, oldBrush, oldMode) 1178 1179 self._rects = rects
1180 1181
1182 - def DrawLines(self, dc):
1183 """ Draws lines connecting the markers. """ 1184 1185 cuboid = self._mainDialog._cuboid 1186 1187 dc.DrawLinePoint(cuboid[1], cuboid[2]) 1188 dc.DrawLinePoint(cuboid[2], cuboid[3]) 1189 dc.DrawLinePoint(cuboid[3], cuboid[4]) 1190 dc.DrawLinePoint(cuboid[4], cuboid[5]) 1191 dc.DrawLinePoint(cuboid[5], cuboid[2]) 1192 1193 dc.DrawLinePoint(cuboid[5], cuboid[6]) 1194 dc.DrawLinePoint(cuboid[6], cuboid[7]) 1195 dc.DrawLinePoint(cuboid[7], cuboid[4]) 1196 1197 dc.DrawLinePoint(cuboid[1], cuboid[6])
1198 1199
1200 - def OnLeftDown(self, event):
1201 """ Handles the wx.EVT_LEFT_DOWN event for L{RGBCube}. """ 1202 1203 point = wx.Point(event.GetX(), event.GetY()) 1204 self._mouseIn = False 1205 1206 if self._rects[RED].Contains(point): 1207 self.CaptureMouse() 1208 self._mouseIn = True 1209 self._index = RED 1210 1211 elif self._rects[GREEN].Contains(point): 1212 self.CaptureMouse() 1213 self._mouseIn = True 1214 self._index = GREEN 1215 1216 elif self._rects[BLUE].Contains(point): 1217 self.CaptureMouse() 1218 self._mouseIn = True 1219 self._index = BLUE
1220 1221 1222
1223 - def OnLeftUp(self, event):
1224 """ Handles the wx.EVT_LEFT_UP event for L{RGBCube}. """ 1225 1226 if self.GetCapture(): 1227 self.ReleaseMouse() 1228 self._mouseIn = False
1229 1230
1231 - def OnMotion(self, event):
1232 """ Handles the wx.EVT_MOTION event for L{RGBCube}. """ 1233 1234 point = wx.Point(event.GetX(), event.GetY()) 1235 1236 if not (self.GetCapture() and self._mouseIn): 1237 event.Skip() 1238 return 1239 1240 bChange = False 1241 mainDialog = self._mainDialog 1242 colour = mainDialog._colour 1243 redLen, greenLen, blueLen = mainDialog._redLen, mainDialog._greenLen, mainDialog._blueLen 1244 1245 dc = wx.ClientDC(self) 1246 self.DrawMarkers(dc) 1247 1248 if self._index == RED: 1249 1250 if point.y > Vertex.y: 1251 point.y = Vertex.y 1252 1253 point.x = Vertex.x 1254 val = Distance(point, Vertex) 1255 if val > redLen: 1256 val = redLen 1257 1258 val = (float(val)/redLen)*255 1259 colour.r = int(val) 1260 1261 pt = PointOnLine(Vertex, Top, (colour.r*redLen)/255, redLen) 1262 self._rects[RED] = wx.Rect(pt.x - RECT_WIDTH, pt.y - RECT_WIDTH, 1263 2*RECT_WIDTH, 2*RECT_WIDTH) 1264 1265 bChange = True 1266 1267 elif self._index == GREEN: 1268 1269 if point.x > Vertex.x: 1270 point.x = Vertex.x 1271 1272 point.y = self._rects[GREEN].GetTop() + RECT_WIDTH 1273 val = Distance(point, Vertex) 1274 if val > greenLen: 1275 val = greenLen 1276 1277 val = (float(val)/greenLen)*255 1278 colour.g = int(val) 1279 1280 pt = PointOnLine(Vertex, Left, (colour.g*greenLen)/255, greenLen) 1281 self._rects[GREEN] = wx.Rect(pt.x - RECT_WIDTH, pt.y - RECT_WIDTH, 1282 2*RECT_WIDTH, 2*RECT_WIDTH) 1283 1284 bChange = True 1285 1286 elif self._index == BLUE: 1287 1288 if point.x < Vertex.x: 1289 point.x = Vertex.x 1290 1291 point.y = self._rects[BLUE].GetTop() + RECT_WIDTH 1292 val = Distance(point, Vertex) 1293 if val > blueLen: 1294 val = blueLen 1295 1296 val = (float(val)/blueLen)*255 1297 colour.b = int(val) 1298 1299 pt = PointOnLine(Vertex, Right, (colour.b*blueLen)/255, blueLen) 1300 self._rects[BLUE] = wx.Rect(pt.x - RECT_WIDTH, pt.y - RECT_WIDTH, 1301 2*RECT_WIDTH, 2*RECT_WIDTH) 1302 1303 bChange = True 1304 1305 if bChange: 1306 1307 mainDialog.CalcCuboid() 1308 self.DrawMarkers(dc) 1309 1310 colour.ToHSV() 1311 mainDialog.SetSpinVals() 1312 mainDialog.CalcRects() 1313 1314 mainDialog.DrawHSB() 1315 mainDialog.DrawBright() 1316 mainDialog.DrawAlpha()
1317 1318
1319 -class HSVWheel(BasePyControl):
1320 """ 1321 Implements the drawing, mouse handling and sizing routines for the HSV 1322 colour wheel. 1323 """ 1324
1325 - def __init__(self, parent):
1326 """ 1327 Default class constructor. 1328 Used internally. Do not call it in your code! 1329 """ 1330 1331 BasePyControl.__init__(self, parent, bitmap="HSVWheel.png") 1332 self._mouseIn = False
1333 1334
1335 - def DrawMarkers(self, dc=None):
1336 """ Draws the square markers used with mouse gestures. """ 1337 1338 if dc is None: 1339 dc = wx.ClientDC(self) 1340 1341 oldPen, oldBrush, oldMode = dc.GetPen(), dc.GetBrush(), dc.GetLogicalFunction() 1342 dc.SetPen(wx.WHITE_PEN) 1343 dc.SetBrush(wx.TRANSPARENT_BRUSH) 1344 dc.SetLogicalFunction(wx.XOR) 1345 1346 dc.DrawRectangleRect(self._mainDialog._currentRect) 1347 RestoreOldDC(dc, oldPen, oldBrush, oldMode)
1348 1349
1350 - def OnLeftDown(self, event):
1351 """ Handles the wx.EVT_LEFT_DOWN event for L{HSVWheel}. """ 1352 1353 point = wx.Point(event.GetX(), event.GetY()) 1354 self._mouseIn = False 1355 1356 if self.InCircle(point): 1357 self._mouseIn = True 1358 1359 if self._mouseIn: 1360 self.CaptureMouse() 1361 self.TrackPoint(point)
1362 1363
1364 - def OnLeftUp(self, event):
1365 """ Handles the wx.EVT_LEFT_UP event for L{HSVWheel}. """ 1366 1367 if self.GetCapture(): 1368 self.ReleaseMouse() 1369 self._mouseIn = False
1370 1371
1372 - def OnMotion(self, event):
1373 """ Handles the wx.EVT_MOTION event for L{HSVWheel}. """ 1374 1375 point = wx.Point(event.GetX(), event.GetY()) 1376 1377 if self.GetCapture() and self._mouseIn: 1378 self.TrackPoint(point)
1379 1380
1381 - def InCircle(self, pt):
1382 """ Returns whether a point is inside the HSV wheel or not. """ 1383 1384 return Distance(pt, self._mainDialog._centre) <= RADIUS
1385 1386
1387 - def TrackPoint(self, pt):
1388 """ Track a mouse event inside the HSV colour wheel. """ 1389 1390 if not self._mouseIn: 1391 return 1392 1393 dc = wx.ClientDC(self) 1394 self.DrawMarkers(dc) 1395 mainDialog = self._mainDialog 1396 colour = mainDialog._colour 1397 1398 colour.h = int(rad2deg(AngleFromPoint(pt, mainDialog._centre))) 1399 if colour.h < 0: 1400 colour.h += 360 1401 1402 colour.s = int(scaletomax(Distance(pt, mainDialog._centre))) 1403 if colour.s > 255: 1404 colour.s = 255 1405 1406 mainDialog.CalcRects() 1407 self.DrawMarkers(dc) 1408 colour.ToRGB() 1409 mainDialog.SetSpinVals() 1410 1411 mainDialog.CalcCuboid() 1412 mainDialog.DrawRGB() 1413 mainDialog.DrawBright() 1414 mainDialog.DrawAlpha()
1415 1416
1417 -class BaseLineCtrl(wx.PyControl):
1418 """ 1419 Base class used to hold common code for the Alpha channel control and the 1420 brightness palette control. 1421 """ 1422
1423 - def __init__(self, parent):
1424 """ 1425 Default class constructor. 1426 Used internally. Do not call it in your code! 1427 """ 1428 1429 wx.PyControl.__init__(self, parent, size=(20, 200), style=wx.NO_BORDER) 1430 self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM) 1431 1432 self._mainDialog = wx.GetTopLevelParent(self) 1433 1434 self.Bind(wx.EVT_SIZE, self.OnSize) 1435 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) 1436 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown) 1437 self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) 1438 self.Bind(wx.EVT_MOTION, self.OnMotion)
1439 1440
1441 - def OnEraseBackground(self, event):
1442 """ Handles the wx.EVT_ERASE_BACKGROUND for L{BaseLineCtrl}. """ 1443 1444 pass
1445 1446
1447 - def OnLeftDown(self, event):
1448 """ Handles the wx.EVT_LEFT_DOWN for L{BaseLineCtrl}. """ 1449 1450 point = wx.Point(event.GetX(), event.GetY()) 1451 brightRect = self.BuildRect() 1452 1453 if not brightRect.Contains(point): 1454 event.Skip() 1455 return 1456 1457 self.CaptureMouse() 1458 self.TrackPoint(point)
1459 1460
1461 - def OnLeftUp(self, event):
1462 """ Handles the wx.EVT_LEFT_UP for L{BaseLineCtrl}. """ 1463 1464 if self.GetCapture(): 1465 self.ReleaseMouse()
1466 1467
1468 - def OnMotion(self, event):
1469 """ Handles the wx.EVT_MOTION for L{BaseLineCtrl}. """ 1470 1471 point = wx.Point(event.GetX(), event.GetY()) 1472 1473 if self.GetCapture(): 1474 self.TrackPoint(point)
1475 1476
1477 - def OnSize(self, event):
1478 """ Handles the wx.EVT_SIZE for L{BaseLineCtrl}. """ 1479 1480 self.Refresh()
1481 1482
1483 - def DoGetBestSize(self):
1484 """ Returns the custom control best size (used by sizers). """ 1485 1486 return wx.Size(24, 208)
1487 1488
1489 - def BuildRect(self):
1490 """ Internal method. """ 1491 1492 brightRect = wx.Rect(*self.GetClientRect()) 1493 brightRect.x += 2 1494 brightRect.y += 4 1495 brightRect.width -= 4 1496 brightRect.height -= 8 1497 1498 return brightRect
1499 1500
1501 - def AcceptsFocusFromKeyboard(self):
1502 """ We do not accept focus from keyboard. """ 1503 1504 return False
1505 1506
1507 - def AcceptFocus(self):
1508 """ We do not accept mouse focus. """ 1509 1510 return False
1511 1512 1513
1514 -class BrightCtrl(BaseLineCtrl):
1515 """ 1516 Implements the drawing, mouse handling and sizing routines for the brightness 1517 palette control. 1518 """ 1519
1520 - def __init__(self, parent):
1521 """ 1522 Default class constructor. 1523 Used internally. Do not call it in your code! 1524 """ 1525 1526 BaseLineCtrl.__init__(self, parent) 1527 self.Bind(wx.EVT_PAINT, self.OnPaint)
1528 1529
1530 - def OnPaint(self, event):
1531 """ Handles the wx.EVT_PAINT event for L{BrightCtrl}. """ 1532 1533 dc = wx.AutoBufferedPaintDC(self) 1534 dc.SetBackground(wx.Brush(self.GetParent().GetBackgroundColour())) 1535 dc.Clear() 1536 1537 colour = self._mainDialog._colour.GetPyColour() 1538 brightRect = self.BuildRect() 1539 1540 target_red = colour.Red() 1541 target_green = colour.Green() 1542 target_blue = colour.Blue() 1543 1544 h, s, v = colorsys.rgb_to_hsv(target_red / 255.0, target_green / 255.0, 1545 target_blue / 255.0) 1546 v = 1.0 1547 vstep = 1.0/(brightRect.height-1) 1548 1549 for y_pos in range(brightRect.y, brightRect.height+brightRect.y): 1550 r, g, b = [c * 255.0 for c in colorsys.hsv_to_rgb(h, s, v)] 1551 colour = wx.Colour(int(r), int(g), int(b)) 1552 dc.SetPen(wx.Pen(colour, 1, wx.SOLID)) 1553 dc.DrawRectangle(brightRect.x, y_pos, brightRect.width, 1) 1554 v = v - vstep 1555 1556 dc.SetPen(wx.BLACK_PEN) 1557 dc.SetBrush(wx.TRANSPARENT_BRUSH) 1558 dc.DrawRectangleRect(brightRect) 1559 1560 self.DrawMarkers(dc)
1561 1562
1563 - def TrackPoint(self, pt):
1564 """ Tracks a mouse action inside the palette control. """ 1565 1566 brightRect = self.BuildRect() 1567 d = brightRect.GetBottom() - pt.y 1568 d *= 255 1569 d /= brightRect.height 1570 if d < 0: 1571 d = 0 1572 if d > 255: 1573 d = 255; 1574 1575 mainDialog = self._mainDialog 1576 colour = mainDialog._colour 1577 1578 mainDialog.DrawMarkers() 1579 colour.v = int(d) 1580 1581 colour.ToRGB() 1582 mainDialog.SetSpinVals() 1583 1584 mainDialog.CalcRects() 1585 mainDialog.CalcCuboid() 1586 mainDialog.DrawMarkers() 1587 mainDialog.DrawAlpha()
1588 1589
1590 - def DrawMarkers(self, dc=None):
1591 """ Draws square markers used with mouse gestures. """ 1592 1593 if dc is None: 1594 dc = wx.ClientDC(self) 1595 1596 colour = self._mainDialog._colour 1597 brightRect = self.BuildRect() 1598 1599 y = int(colour.v/255.0*brightRect.height) 1600 y = brightRect.GetBottom() - y 1601 brightMark = wx.Rect(brightRect.x-2, y-4, brightRect.width+4, 8) 1602 1603 oldPen, oldBrush, oldMode = dc.GetPen(), dc.GetBrush(), dc.GetLogicalFunction() 1604 dc.SetPen(wx.Pen(wx.WHITE, 2)) 1605 dc.SetBrush(wx.TRANSPARENT_BRUSH) 1606 dc.SetLogicalFunction(wx.XOR) 1607 1608 dc.DrawRectangleRect(brightMark) 1609 RestoreOldDC(dc, oldPen, oldBrush, oldMode)
1610 1611 1612
1613 -class AlphaCtrl(BaseLineCtrl):
1614 """ 1615 Implements the drawing, mouse handling and sizing routines for the alpha 1616 channel control. 1617 """ 1618
1619 - def __init__(self, parent):
1620 """ 1621 Default class constructor. 1622 Used internally. Do not call it in your code! 1623 """ 1624 1625 BaseLineCtrl.__init__(self, parent) 1626 self.Bind(wx.EVT_PAINT, self.OnPaint)
1627 1628
1629 - def OnPaint(self, event):
1630 """ Handles the wx.EVT_PAINT event for L{AlphaCtrl}. """ 1631 1632 pdc = wx.PaintDC(self) 1633 dc = wx.GCDC(pdc) 1634 1635 mem_dc = wx.MemoryDC() 1636 fullRect = self.GetClientRect() 1637 bmp = wx.EmptyBitmap(fullRect.width, fullRect.height) 1638 mem_dc.SelectObject(bmp) 1639 1640 rect = self.BuildRect() 1641 backBrush = wx.Brush(self.GetParent().GetBackgroundColour()) 1642 mem_dc.SetBackground(backBrush) 1643 mem_dc.Clear() 1644 1645 mem_dc.SetBrush(wx.WHITE_BRUSH) 1646 mem_dc.DrawRectangleRect(rect) 1647 1648 DrawCheckerBoard(mem_dc, rect, checkColour) 1649 self.DrawAlphaShading(mem_dc, rect) 1650 mem_dc.DestroyClippingRegion() 1651 1652 self.DrawMarkers(mem_dc) 1653 1654 mem_dc.SetBrush(wx.TRANSPARENT_BRUSH) 1655 mem_dc.SetPen(wx.BLACK_PEN) 1656 mem_dc.DrawRectangleRect(rect) 1657 1658 mem_dc.SelectObject(wx.NullBitmap) 1659 pdc.DrawBitmap(bmp, 0, 0)
1660 1661
1662 - def DrawAlphaShading(self, dc, rect):
1663 """ Draws the alpha shading on top of the checkerboard. """ 1664 1665 gcdc = wx.GCDC(dc) 1666 1667 colour = self._mainDialog._colour.GetPyColour() 1668 1669 alpha = 255.0 1670 vstep = 255.0*2/(rect.height-1) 1671 r, g, b = colour.Red(), colour.Green(), colour.Blue() 1672 1673 colour_gcdc = wx.Colour(r, g, b, alpha) 1674 gcdc.SetBrush(wx.TRANSPARENT_BRUSH) 1675 1676 for y_pos in range(rect.y, rect.height+rect.y, 2): 1677 colour_gcdc = wx.Colour(r, g, b, int(alpha)) 1678 gcdc.SetPen(wx.Pen(colour_gcdc, 1, wx.SOLID)) 1679 gcdc.DrawRectangle(rect.x, y_pos, rect.width, 2) 1680 alpha = alpha - vstep
1681 1682
1683 - def TrackPoint(self, pt):
1684 """ Tracks a mouse action inside the Alpha channel control. """ 1685 1686 alphaRect = self.BuildRect() 1687 d = alphaRect.GetBottom() - pt.y 1688 d *= 255 1689 d /= alphaRect.height 1690 if d < 0: 1691 d = 0 1692 if d > 255: 1693 d = 255 1694 1695 self._mainDialog._colour.alpha = int(d) 1696 self.Refresh() 1697 self._mainDialog.SetSpinVals()
1698 1699
1700 - def DrawMarkers(self, dc=None):
1701 """ Draws square markers used with mouse gestures. """ 1702 1703 if dc is None: 1704 dc = wx.ClientDC(self) 1705 1706 colour = self._mainDialog._colour 1707 alphaRect = self.BuildRect() 1708 1709 y = int(colour.alpha/255.0*alphaRect.height) 1710 y = alphaRect.GetBottom() - y 1711 alphaMark = wx.Rect(alphaRect.x-2, y-4, alphaRect.width+4, 8) 1712 1713 oldPen, oldBrush, oldMode = dc.GetPen(), dc.GetBrush(), dc.GetLogicalFunction() 1714 dc.SetPen(wx.Pen(wx.WHITE, 2)) 1715 dc.SetBrush(wx.TRANSPARENT_BRUSH) 1716 dc.SetLogicalFunction(wx.XOR) 1717 1718 dc.DrawRectangleRect(alphaMark) 1719 RestoreOldDC(dc, oldPen, oldBrush, oldMode)
1720 1721
1722 -class ColourPanel(wx.PyPanel):
1723 """ 1724 Simple custom class used to display "old" and "new" colour panels, with alpha 1725 blending capabilities. 1726 """ 1727
1728 - def __init__(self, parent, style=wx.SIMPLE_BORDER):
1729 """ 1730 Default class constructor. 1731 Used internally. Do not call it in your code! 1732 """ 1733 1734 wx.PyPanel.__init__(self, parent, style=style) 1735 self._mainDialog = wx.GetTopLevelParent(self) 1736 1737 self.Bind(wx.EVT_PAINT, self.OnPaint) 1738 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) 1739 self.Bind(wx.EVT_SIZE, self.OnSize)
1740 1741
1742 - def OnPaint(self, event):
1743 """ Handles the wx.EVT_PAINT event for L{ColourPanel}. """ 1744 1745 pdc = wx.PaintDC(self) 1746 dc = wx.GCDC(pdc) 1747 1748 mem_dc = wx.MemoryDC() 1749 rect = self.GetClientRect() 1750 bmp = wx.EmptyBitmap(rect.width, rect.height) 1751 mem_dc.SelectObject(bmp) 1752 1753 backBrush = wx.Brush(self.GetParent().GetBackgroundColour()) 1754 mem_dc.SetBackground(backBrush) 1755 mem_dc.Clear() 1756 1757 mem_dc.SetBrush(wx.WHITE_BRUSH) 1758 mem_dc.DrawRectangleRect(rect) 1759 1760 DrawCheckerBoard(mem_dc, rect, checkColour, box=10) 1761 1762 gcdc = wx.GCDC(mem_dc) 1763 colour_gcdc = wx.Colour(self._colour.r, self._colour.g, self._colour.b, self._colour.alpha) 1764 gcdc.SetBrush(wx.Brush(colour_gcdc)) 1765 gcdc.SetPen(wx.Pen(colour_gcdc)) 1766 gcdc.DrawRectangleRect(rect) 1767 1768 mem_dc.SelectObject(wx.NullBitmap) 1769 dc.DrawBitmap(bmp, 0, 0)
1770 1771
1772 - def OnEraseBackground(self, event):
1773 """ Handles the wx.EVT_ERASE_BACKGROUND event for L{ColourPanel}. """ 1774 1775 pass
1776 1777
1778 - def OnSize(self, event):
1779 """ Handles the wx.EVT_SIZE event for L{ColourPanel}. """ 1780 1781 self.Refresh()
1782 1783
1784 - def RefreshColour(self, colour):
1785 """ Refresh the panel after a colour/alpha change. """ 1786 1787 self._colour = colour 1788 self.Refresh()
1789 1790
1791 - def AcceptsFocusFromKeyboard(self):
1792 """ We do not accept focus from keyboard. """ 1793 1794 return False
1795 1796
1797 - def AcceptFocus(self):
1798 """ We do not accept mouse focus. """ 1799 1800 return False
1801 1802
1803 -class CubeColourDialog(wx.Dialog):
1804 1805 """ 1806 This is the CubeColourDialog main class implementation. 1807 """ 1808
1809 - def __init__(self, parent, colourData=None, style=CCD_SHOW_ALPHA):
1810 """ 1811 Default class constructor. 1812 1813 @param colourData: a wxPython wx.ColourData; 1814 @param style: can be either None or CCD_SHOW_ALPHA, depending if you want 1815 to hide the alpha channel control or not. 1816 """ 1817 1818 wx.Dialog.__init__(self, parent, id=wx.ID_ANY, title="CubeColourDialog: Choose Colour", 1819 pos=wx.DefaultPosition, size=(900, 900), style=wx.DEFAULT_DIALOG_STYLE|style) 1820 1821 if colourData: 1822 self._colourData = colourData 1823 else: 1824 self._colourData = wx.ColourData() 1825 self._colourData.SetColour(wx.Colour(128, 128, 128)) 1826 1827 self._colour = Colour(self._colourData.GetColour()) 1828 self._oldColour = Colour(self._colourData.GetColour()) 1829 1830 self._inMouse = False 1831 self._initOver = False 1832 self._inDrawAll = False 1833 1834 self.mainPanel = wx.Panel(self, -1) 1835 1836 self.hsvSizer_staticbox = wx.StaticBox(self.mainPanel, -1, "HSB") 1837 self.rgbValueSizer_staticbox = wx.StaticBox(self.mainPanel, -1, "RGB Values") 1838 self.hsvValueSizer_staticbox = wx.StaticBox(self.mainPanel, -1, "HSB Values") 1839 self.rgbSizer_staticbox = wx.StaticBox(self.mainPanel, -1, "RGB") 1840 self.alphaSizer_staticbox = wx.StaticBox(self.mainPanel, -1, "Alpha") 1841 self.alphaValueSizer_staticbox = wx.StaticBox(self.mainPanel, -1, "Alpha") 1842 1843 self.rgbBitmap = RGBCube(self.mainPanel) 1844 self.hsvBitmap = HSVWheel(self.mainPanel) 1845 self.brightCtrl = BrightCtrl(self.mainPanel) 1846 self.alphaCtrl = AlphaCtrl(self.mainPanel) 1847 1848 self.showAlpha = wx.CheckBox(self.mainPanel, -1, "Show Alpha Control") 1849 1850 self.okButton = wx.Button(self.mainPanel, -1, "Ok") 1851 self.cancelButton = wx.Button(self.mainPanel, -1, "Cancel") 1852 1853 self.oldColourPanel = ColourPanel(self.mainPanel, style=wx.SIMPLE_BORDER) 1854 self.newColourPanel = ColourPanel(self.mainPanel, style=wx.SIMPLE_BORDER) 1855 1856 self.redSpin = wx.SpinCtrl(self.mainPanel, -1, "180", min=0, max=255, 1857 style=wx.SP_ARROW_KEYS) 1858 self.greenSpin = wx.SpinCtrl(self.mainPanel, -1, "180", min=0, max=255, 1859 style=wx.SP_ARROW_KEYS) 1860 self.blueSpin = wx.SpinCtrl(self.mainPanel, -1, "180", min=0, max=255, 1861 style=wx.SP_ARROW_KEYS) 1862 self.hueSpin = wx.SpinCtrl(self.mainPanel, -1, "0", min=0, max=359, 1863 style=wx.SP_ARROW_KEYS) 1864 self.saturationSpin = wx.SpinCtrl(self.mainPanel, -1, "", min=0, max=255, 1865 style=wx.SP_ARROW_KEYS) 1866 self.brightnessSpin = wx.SpinCtrl(self.mainPanel, -1, "", min=0, max=255, 1867 style=wx.SP_ARROW_KEYS) 1868 self.alphaSpin = wx.SpinCtrl(self.mainPanel, -1, "", min=0, max=255, 1869 style=wx.SP_ARROW_KEYS) 1870 self.accessCode = wx.TextCtrl(self.mainPanel, -1, "", style=wx.TE_READONLY) 1871 self.htmlCode = wx.TextCtrl(self.mainPanel, -1, "", style=wx.TE_READONLY) 1872 self.webSafe = wx.TextCtrl(self.mainPanel, -1, "", style=wx.TE_READONLY) 1873 self.htmlName = wx.TextCtrl(self.mainPanel, -1, "", style=wx.TE_READONLY) 1874 1875 self.SetProperties() 1876 self.DoLayout() 1877 1878 self.spinCtrls = [self.redSpin, self.greenSpin, self.blueSpin, 1879 self.hueSpin, self.saturationSpin, self.brightnessSpin] 1880 1881 for spin in self.spinCtrls: 1882 spin.Bind(wx.EVT_SPINCTRL, self.OnSpinCtrl) 1883 1884 self.Bind(wx.EVT_SPINCTRL, self.OnAlphaSpin, self.alphaSpin) 1885 1886 self.Bind(wx.EVT_BUTTON, self.OnOk, self.okButton) 1887 self.Bind(wx.EVT_BUTTON, self.OnCancel, self.cancelButton) 1888 self.Bind(wx.EVT_CHECKBOX, self.OnShowAlpha) 1889 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) 1890 1891 self.Centre(wx.BOTH) 1892 1893 wx.CallAfter(self.InitDialog)
1894 1895
1896 - def SetProperties(self):
1897 """ Sets some initial properties (sizes, values). """ 1898 1899 self.okButton.SetDefault() 1900 self.oldColourPanel.SetMinSize((-1, 50)) 1901 self.newColourPanel.SetMinSize((-1, 50)) 1902 self.redSpin.SetMinSize((60, -1)) 1903 self.greenSpin.SetMinSize((60, -1)) 1904 self.blueSpin.SetMinSize((60, -1)) 1905 self.hueSpin.SetMinSize((60, -1)) 1906 self.saturationSpin.SetMinSize((60, -1)) 1907 self.brightnessSpin.SetMinSize((60, -1)) 1908 self.alphaSpin.SetMinSize((60, -1)) 1909 self.showAlpha.SetValue(1) 1910 self.accessCode.SetInitialSize((80, -1)) 1911 self.webSafe.SetInitialSize((80, -1)) 1912 self.htmlCode.SetInitialSize((80, -1))
1913 1914
1915 - def DoLayout(self):
1916 """ Layouts all the controls in the L{CubeColourDialog}. """ 1917 1918 dialogSizer = wx.BoxSizer(wx.VERTICAL) 1919 mainSizer = wx.FlexGridSizer(5, 5, 10, 5) 1920 hsvValueSizer = wx.StaticBoxSizer(self.hsvValueSizer_staticbox, wx.VERTICAL) 1921 hsvGridSizer = wx.GridSizer(2, 3, 2, 10) 1922 rgbValueSizer = wx.StaticBoxSizer(self.rgbValueSizer_staticbox, wx.HORIZONTAL) 1923 rgbGridSizer = wx.GridSizer(2, 3, 2, 10) 1924 alphaValueSizer = wx.StaticBoxSizer(self.alphaValueSizer_staticbox, wx.VERTICAL) 1925 alphaGridSizer = wx.BoxSizer(wx.VERTICAL) 1926 buttonSizer = wx.BoxSizer(wx.VERTICAL) 1927 accessSizer = wx.BoxSizer(wx.VERTICAL) 1928 htmlSizer1 = wx.BoxSizer(wx.HORIZONTAL) 1929 htmlSizer2 = wx.BoxSizer(wx.VERTICAL) 1930 htmlSizer_a = wx.BoxSizer(wx.VERTICAL) 1931 htmlSizer_b = wx.BoxSizer(wx.VERTICAL) 1932 1933 hsvSizer = wx.StaticBoxSizer(self.hsvSizer_staticbox, wx.HORIZONTAL) 1934 rgbSizer = wx.StaticBoxSizer(self.rgbSizer_staticbox, wx.VERTICAL) 1935 alphaSizer = wx.StaticBoxSizer(self.alphaSizer_staticbox, wx.VERTICAL) 1936 1937 mainSizer.Add(self.showAlpha, 0, wx.LEFT|wx.TOP, 10) 1938 mainSizer.Add((0, 0), 0, wx.EXPAND) 1939 mainSizer.Add((0, 0), 0, wx.EXPAND) 1940 mainSizer.Add((0, 0), 0, wx.EXPAND) 1941 mainSizer.Add((0, 0), 0, wx.EXPAND) 1942 1943 htmlLabel1 = wx.StaticText(self.mainPanel, -1, "HTML Code") 1944 htmlLabel2 = wx.StaticText(self.mainPanel, -1, "Web Safe") 1945 htmlSizer_a.Add(htmlLabel1, 0, wx.TOP, 3) 1946 htmlSizer_b.Add(htmlLabel2, 0, wx.TOP, 3) 1947 htmlSizer_a.Add(self.htmlCode, 0, wx.TOP, 3) 1948 htmlSizer_b.Add(self.webSafe, 0, wx.TOP, 3) 1949 1950 htmlSizer1.Add(htmlSizer_a, 0) 1951 htmlSizer1.Add(htmlSizer_b, 0, wx.LEFT, 10) 1952 mainSizer.Add(htmlSizer1, 0, wx.LEFT|wx.RIGHT, 10) 1953 1954 htmlLabel3 = wx.StaticText(self.mainPanel, -1, "HTML Name") 1955 htmlSizer2.Add(htmlLabel3, 0, wx.TOP|wx.BOTTOM, 3) 1956 htmlSizer2.Add(self.htmlName, 0) 1957 1958 mainSizer.Add(htmlSizer2, 0, wx.LEFT|wx.RIGHT, 10) 1959 mainSizer.Add((0, 0)) 1960 mainSizer.Add((0, 0)) 1961 mainSizer.Add((0, 0)) 1962 1963 rgbSizer.Add(self.rgbBitmap, 0, wx.ALL, 15) 1964 mainSizer.Add(rgbSizer, 0, wx.ALL|wx.EXPAND, 10) 1965 hsvSizer.Add(self.hsvBitmap, 0, wx.ALL, 15) 1966 hsvSizer.Add(self.brightCtrl, 0, wx.RIGHT|wx.TOP|wx.BOTTOM, 15) 1967 mainSizer.Add(hsvSizer, 0, wx.ALL|wx.EXPAND, 10) 1968 alphaSizer.Add(self.alphaCtrl, 0, wx.TOP|wx.ALIGN_CENTER, 15) 1969 mainSizer.Add(alphaSizer, 0, wx.ALL|wx.EXPAND, 10) 1970 1971 buttonSizer.Add(self.okButton, 0, wx.BOTTOM, 3) 1972 buttonSizer.Add(self.cancelButton, 0) 1973 buttonSizer.Add((0, 0), 1, wx.EXPAND) 1974 oldLabel = wx.StaticText(self.mainPanel, -1, "Old Colour") 1975 buttonSizer.Add(oldLabel, 0, wx.BOTTOM, 3) 1976 buttonSizer.Add(self.oldColourPanel, 0, wx.BOTTOM|wx.EXPAND, 20) 1977 newLabel = wx.StaticText(self.mainPanel, -1, "New Colour") 1978 buttonSizer.Add(newLabel, 0, wx.BOTTOM, 3) 1979 buttonSizer.Add(self.newColourPanel, 0, wx.EXPAND) 1980 mainSizer.Add(buttonSizer, 1, wx.ALL|wx.EXPAND, 10) 1981 mainSizer.Add((10, 10), 0) 1982 redLabel = wx.StaticText(self.mainPanel, -1, "Red") 1983 rgbGridSizer.Add(redLabel, 0) 1984 greenLabel = wx.StaticText(self.mainPanel, -1, "Green") 1985 rgbGridSizer.Add(greenLabel, 0) 1986 blueLabel = wx.StaticText(self.mainPanel, -1, "Blue") 1987 rgbGridSizer.Add(blueLabel, 0) 1988 rgbGridSizer.Add(self.redSpin, 0, wx.EXPAND) 1989 rgbGridSizer.Add(self.greenSpin, 0, wx.EXPAND) 1990 rgbGridSizer.Add(self.blueSpin, 0, wx.EXPAND) 1991 rgbValueSizer.Add(rgbGridSizer, 1, 0, 0) 1992 mainSizer.Add(rgbValueSizer, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.EXPAND, 10) 1993 hueLabel = wx.StaticText(self.mainPanel, -1, "Hue") 1994 hsvGridSizer.Add(hueLabel, 0) 1995 saturationLabel = wx.StaticText(self.mainPanel, -1, "Saturation") 1996 hsvGridSizer.Add(saturationLabel, 0) 1997 brightnessLabel = wx.StaticText(self.mainPanel, -1, "Brightness") 1998 hsvGridSizer.Add(brightnessLabel, 0) 1999 hsvGridSizer.Add(self.hueSpin, 0, wx.EXPAND) 2000 hsvGridSizer.Add(self.saturationSpin, 0, wx.EXPAND) 2001 hsvGridSizer.Add(self.brightnessSpin, 0, wx.EXPAND) 2002 hsvValueSizer.Add(hsvGridSizer, 1, wx.EXPAND) 2003 mainSizer.Add(hsvValueSizer, 1, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.EXPAND, 10) 2004 alphaLabel = wx.StaticText(self.mainPanel, -1, "Alpha") 2005 alphaGridSizer.Add(alphaLabel, 0) 2006 alphaGridSizer.Add(self.alphaSpin, 0, wx.EXPAND|wx.TOP, 10) 2007 alphaValueSizer.Add(alphaGridSizer, 1, wx.EXPAND) 2008 mainSizer.Add(alphaValueSizer, 1, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.EXPAND, 10) 2009 accessLabel = wx.StaticText(self.mainPanel, -1, "MS Access Code") 2010 accessSizer.Add(accessLabel, 0, wx.BOTTOM, 3) 2011 accessSizer.Add(self.accessCode, 0) 2012 mainSizer.Add(accessSizer, 0, wx.LEFT|wx.RIGHT, 5) 2013 mainSizer.Add((10, 10), 0) 2014 self.mainPanel.SetAutoLayout(True) 2015 self.mainPanel.SetSizer(mainSizer) 2016 mainSizer.Fit(self.mainPanel) 2017 mainSizer.SetSizeHints(self.mainPanel) 2018 2019 if self.GetWindowStyleFlag() & CCD_SHOW_ALPHA == 0: 2020 mainSizer.Hide(self.showAlpha) 2021 mainSizer.Hide(alphaSizer) 2022 mainSizer.Hide(alphaValueSizer) 2023 2024 dialogSizer.Add(self.mainPanel, 1, wx.EXPAND) 2025 self.SetAutoLayout(True) 2026 self.SetSizer(dialogSizer) 2027 dialogSizer.Fit(self) 2028 dialogSizer.SetSizeHints(self) 2029 self.Layout() 2030 2031 self.mainSizer = mainSizer 2032 self.dialogSizer = dialogSizer 2033 self.alphaSizers = [alphaSizer, alphaValueSizer]
2034 2035
2036 - def InitDialog(self):
2037 """ Initialize the L{CubeColourDialog}. """ 2038 2039 hsvRect = self.hsvBitmap.GetClientRect() 2040 self._centre = wx.Point(hsvRect.x + hsvRect.width/2, hsvRect.y + hsvRect.height/2) 2041 2042 self._redLen = Distance(Vertex, Top) 2043 self._greenLen = Distance(Vertex, Left) 2044 self._blueLen = Distance(Vertex, Right) 2045 2046 self.CalcSlopes() 2047 self.CalcCuboid() 2048 self.CalcRects() 2049 2050 self.SetSpinVals() 2051 2052 self._initOver = True 2053 wx.CallAfter(self.Refresh)
2054 2055
2056 - def CalcSlopes(self):
2057 """ Calculates the line slopes in the RGB colour cube. """ 2058 2059 self._lines = {RED: LineDescription(), GREEN: LineDescription(), BLUE: LineDescription} 2060 2061 self._lines[RED].slope = Slope(Top, Vertex) 2062 self._lines[GREEN].slope = Slope(Left, Vertex) 2063 self._lines[BLUE].slope = Slope(Right, Vertex) 2064 2065 for i in xrange(3): 2066 self._lines[i].x = Vertex.x 2067 self._lines[i].y = Vertex.y 2068 self._lines[i].c = FindC(self._lines[i])
2069 2070
2071 - def CalcCuboid(self):
2072 """ Calculates the RGB colour cube vertices. """ 2073 2074 rLen = (self._colour.r*self._redLen)/255.0 2075 gLen = (self._colour.g*self._greenLen)/255.0 2076 bLen = (self._colour.b*self._blueLen)/255.0 2077 2078 lines = [LineDescription() for i in xrange(12)] 2079 self._cuboid = [None]*8 2080 2081 self._cuboid[0] = Vertex 2082 self._cuboid[1] = PointOnLine(Vertex, Top, int(rLen), self._redLen) 2083 self._cuboid[3] = PointOnLine(Vertex, Left, int(gLen), self._greenLen) 2084 self._cuboid[7] = PointOnLine(Vertex, Right, int(bLen), self._blueLen) 2085 2086 lines[0] = self._lines[RED] 2087 lines[1] = self._lines[GREEN] 2088 lines[2] = self._lines[BLUE] 2089 2090 lines[3].slope = self._lines[GREEN].slope 2091 lines[3].x = self._cuboid[1].x 2092 lines[3].y = self._cuboid[1].y 2093 lines[3].c = FindC(lines[3]) 2094 2095 lines[4].slope = self._lines[RED].slope 2096 lines[4].x = self._cuboid[3].x 2097 lines[4].y = self._cuboid[3].y 2098 lines[4].c = FindC(lines[4]) 2099 2100 lines[5].slope = self._lines[BLUE].slope 2101 lines[5].x = self._cuboid[3].x 2102 lines[5].y = self._cuboid[3].y 2103 lines[5].c = FindC(lines[5]) 2104 2105 lines[6].slope = self._lines[GREEN].slope 2106 lines[6].x = self._cuboid[7].x 2107 lines[6].y = self._cuboid[7].y 2108 lines[6].c = FindC(lines[6]) 2109 2110 lines[10].slope = self._lines[BLUE].slope 2111 lines[10].x = self._cuboid[1].x 2112 lines[10].y = self._cuboid[1].y 2113 lines[10].c = FindC(lines[10]) 2114 2115 lines[11].slope = self._lines[RED].slope 2116 lines[11].x = self._cuboid[7].x 2117 lines[11].y = self._cuboid[7].y 2118 lines[11].c = FindC(lines[11]) 2119 2120 self._cuboid[2] = Intersection(lines[3], lines[4]) 2121 self._cuboid[4] = Intersection(lines[5], lines[6]) 2122 self._cuboid[6] = Intersection(lines[10], lines[11]) 2123 2124 lines[7].slope = self._lines[RED].slope 2125 lines[7].x = self._cuboid[4].x 2126 lines[7].y = self._cuboid[4].y 2127 lines[7].c = FindC(lines[7]) 2128 2129 lines[8].slope = self._lines[BLUE].slope 2130 lines[8].x = self._cuboid[2].x 2131 lines[8].y = self._cuboid[2].y 2132 lines[8].c = FindC(lines[8]) 2133 2134 self._cuboid[5] = Intersection(lines[7], lines[8])
2135 2136
2137 - def CalcRects(self):
2138 """ Calculates the brightness control user-selected rect. """ 2139 2140 pt = PtFromAngle(self._colour.h, self._colour.s, self._centre) 2141 self._currentRect = wx.Rect(pt.x - RECT_WIDTH, pt.y - RECT_WIDTH, 2142 2*RECT_WIDTH, 2*RECT_WIDTH)
2143 2144
2145 - def DrawMarkers(self, dc=None):
2146 """ Draws the markers for all the controls. """ 2147 2148 if dc is None: 2149 dc = wx.ClientDC(self) 2150 2151 self.hsvBitmap.DrawMarkers() 2152 self.rgbBitmap.DrawMarkers() 2153 self.brightCtrl.DrawMarkers()
2154 2155
2156 - def DrawRGB(self):
2157 """ Refreshes the RGB colour cube. """ 2158 2159 self.rgbBitmap.Refresh()
2160 2161
2162 - def DrawHSB(self):
2163 """ Refreshes the HSB colour wheel. """ 2164 2165 self.hsvBitmap.Refresh()
2166 2167
2168 - def DrawBright(self):
2169 """ Refreshes the brightness control. """ 2170 2171 self.brightCtrl.Refresh()
2172 2173
2174 - def DrawAlpha(self):
2175 """ Refreshes the alpha channel control. """ 2176 2177 self.alphaCtrl.Refresh()
2178 2179
2180 - def SetSpinVals(self):
2181 """ Sets the values for all the spin controls. """ 2182 2183 self.redSpin.SetValue(self._colour.r) 2184 self.greenSpin.SetValue(self._colour.g) 2185 self.blueSpin.SetValue(self._colour.b) 2186 2187 self.hueSpin.SetValue(self._colour.h) 2188 self.saturationSpin.SetValue(self._colour.s) 2189 self.brightnessSpin.SetValue(self._colour.v) 2190 2191 self.alphaSpin.SetValue(self._colour.alpha) 2192 2193 self.SetPanelColours() 2194 self.SetCodes()
2195 2196
2197 - def SetPanelColours(self):
2198 """ Assigns colours to the colour panels. """ 2199 2200 self.oldColourPanel.RefreshColour(self._oldColour) 2201 self.newColourPanel.RefreshColour(self._colour)
2202 2203
2204 - def SetCodes(self):
2205 """ Sets the HTML/MS Access codes (if any) in the text controls. """ 2206 2207 colour = rgb2html(self._colour) 2208 self.htmlCode.SetValue(colour) 2209 self.htmlCode.Refresh() 2210 2211 if colour in HTMLCodes: 2212 colourName, access, webSafe = HTMLCodes[colour] 2213 self.webSafe.SetValue(webSafe) 2214 self.accessCode.SetValue(access) 2215 self.htmlName.SetValue(colourName) 2216 else: 2217 self.webSafe.SetValue("") 2218 self.accessCode.SetValue("") 2219 self.htmlName.SetValue("")
2220 2221
2222 - def OnCloseWindow(self, event):
2223 """ User canceled the dialog. """ 2224 2225 self.EndModal(wx.ID_CANCEL)
2226 2227
2228 - def ShowModal(self):
2229 """ Overridden wx.Dialog.ShowModal. """ 2230 2231 return wx.Dialog.ShowModal(self)
2232 2233
2234 - def SetWindowStyleFlag(self, style):
2235 """ Sets the L{CubeColourDialog} window style flags. """ 2236 2237 wx.Dialog.SetWindowStyleFlag(self, style) 2238 2239 show = self.GetWindowStyleFlag() & CCD_SHOW_ALPHA 2240 self.mainSizer.Show(self.alphaSizers[0], show) 2241 self.mainSizer.Show(self.alphaSizers[1], show) 2242 2243 self.mainSizer.Fit(self.mainPanel) 2244 self.mainSizer.SetSizeHints(self.mainPanel) 2245 self.mainSizer.Layout() 2246 self.dialogSizer.Fit(self) 2247 self.dialogSizer.SetSizeHints(self) 2248 self.Layout() 2249 2250 self.Refresh() 2251 self.Update()
2252 2253
2254 - def OnOk(self, event):
2255 """ Handles the Ok wx.EVT_BUTTON event for L{CubeColourDialog}. """ 2256 2257 self.EndModal(wx.ID_OK)
2258 2259
2260 - def OnCancel(self, event):
2261 """ Handles the Cancel wx.EVT_BUTTON event for L{CubeColourDialog}. """ 2262 2263 self.OnCloseWindow(event)
2264 2265
2266 - def OnShowAlpha(self, event):
2267 """ Shows/hides the alpha channel control in L{CubeColourDialog}. """ 2268 2269 style = self.GetWindowStyleFlag() 2270 show = event.IsChecked() 2271 2272 if show: 2273 style |= CCD_SHOW_ALPHA 2274 else: 2275 style &= ~CCD_SHOW_ALPHA 2276 2277 self.SetWindowStyleFlag(style)
2278 2279
2280 - def OnSpinCtrl(self, event):
2281 """ Handles the wx.EVT_SPINCTRL event for RGB and HSB colours. """ 2282 2283 obj = event.GetEventObject() 2284 position = self.spinCtrls.index(obj) 2285 colourVal = event.GetInt() 2286 2287 attribute, maxVal = colourAttributes[position], colourMaxValues[position] 2288 2289 self.AssignColourValue(attribute, colourVal, maxVal, position)
2290 2291
2292 - def OnAlphaSpin(self, event):
2293 """ Handles the wx.EVT_SPINCTRL event for the alpha channel. """ 2294 2295 colourVal = event.GetInt() 2296 originalVal = self._colour.alpha 2297 if colourVal != originalVal and self._initOver: 2298 if colourVal < 0: 2299 colourVal = 0 2300 if colourVal > 255: 2301 colourVal = 255 2302 2303 self._colour.alpha = colourVal 2304 self.DrawAlpha()
2305 2306
2307 - def AssignColourValue(self, attribute, colourVal, maxVal, position):
2308 """ Common code to handle spin control changes. """ 2309 2310 originalVal = getattr(self._colour, attribute) 2311 if colourVal != originalVal and self._initOver: 2312 2313 if colourVal < 0: 2314 colourVal = 0 2315 if colourVal > maxVal: 2316 colourVal = maxVal 2317 2318 setattr(self._colour, attribute, colourVal) 2319 if position < 3: 2320 self._colour.ToHSV() 2321 else: 2322 self._colour.ToRGB() 2323 2324 self.DrawAll()
2325 2326
2327 - def DrawAll(self):
2328 """ Draws all the custom controls after a colour change. """ 2329 2330 if self._initOver and not self._inDrawAll: 2331 self._inDrawAll = True 2332 2333 dc1 = wx.ClientDC(self.hsvBitmap) 2334 self.hsvBitmap.DrawMarkers(dc1) 2335 2336 dc2 = wx.ClientDC(self.rgbBitmap) 2337 self.rgbBitmap.DrawMarkers(dc2) 2338 self.rgbBitmap.DrawLines(dc2) 2339 2340 dc3 = wx.ClientDC(self.brightCtrl) 2341 self.brightCtrl.DrawMarkers(dc3) 2342 2343 dc4 = wx.ClientDC(self.alphaCtrl) 2344 self.alphaCtrl.DrawMarkers(dc4) 2345 2346 self.CalcCuboid() 2347 self.CalcRects() 2348 2349 self.DrawRGB() 2350 self.DrawHSB() 2351 self.DrawBright() 2352 self.DrawAlpha() 2353 2354 self.SetSpinVals() 2355 self._inDrawAll = False
2356 2357
2358 - def GetColourData(self):
2359 """ Returns a wxPython compatible wx.ColourData. """ 2360 2361 self._colourData.SetColour(self._colour.GetPyColour()) 2362 return self._colourData
2363 2364
2365 - def GetRGBAColour(self):
2366 """ Returns a 4-elements tuple of red, green, blue, alpha components. """ 2367 2368 return (self._colour.r, self._colour.g, self._colour.b, self._colour.alpha)
2369 2370
2371 - def GetHSVAColour(self):
2372 """ Returns a 4-elements tuple of hue, saturation, brightness, alpha components. """ 2373 2374 return (self._colour.h, self._colour.s, self._colour.v, self._colour.alpha)
2375