![Image 1](/KB/macros/ThePaletteVSAddin/thepalette.jpg)
Introduction
Many of us often need to work with color macros, inserting them into our code one and a million times over while we look for a copasetic combination of alizarin crimson, scheveningen green and indanthrene blue. Catering to this madness, there are scores of color picker utilities out there... and some of them right here, in the CodeProject.
Yet, to my knowledge, there are just a handful of color picker add-ins, and the difference matters. For years, I have used Alex Vallat's ColourPicker Add-in and have come to completely rely on it. This wonderful little add-in grabs the color of the selection I've made or automatically scans for the boundaries of the RGB macro where the caret was. After I've chosen a color from the standard Windows color dialog (or with the dropper), the ColourPicker inserts the RGB macro directly into my code.
Convenient in-friggin'-deed, my friends.
Well, as it happens with software that is used regularly, one often dreams of tweaking it one's own way to suit one's own peculiar conception of it all. And so, the mind departs from the task at hand, floating as it were in zero-g reverie, visiting rarely activated neural assemblies of suspect dendritic affiliation, skipping along ever-recursive paths, whistling outside the rhythm of lucid discourse, and, in short, ineptly collecting gerunds without a single care in the world.
Might it have been on one of those kooky journeys that I unwittingly rendezvoused with ThePalette add-in, a picker/dropper with zoom, user-defined multiple palettes, automatic parsing/replacement/insertion into code, and support for RGB, HSV, HLS, CMYK macros as well as HTML and plain (hexa)decimal color formats?
Absolutely.
Installation
- Download the add-in binaries and unzip the file.
- Copy the file 'thepalette.dll' to the folder '{VisualStudio}\Common\MSDev98\AddIns'.
- Close all files, projects and workspaces!!!!!! else ThePalette won't work. You've been warned, bold letters and all.
- Navigate to the menu Tools/Customize... and click on the tab 'Add-ins and Macro Files'.
- If there is no entry for 'ThePalette Add-in', click on 'Browse' and locate it manually.
- A toolbar button is available and should be displayed automatically. If not, click on the tab 'Commands', select 'Add-ins' from the 'Category' dropdown, and drag'n'drop ThePalette's icon onto the appropriate toolbar.
How to Use
Once invoked, ThePalette add-in scans the text surrounding the caret and automatically detects boundaries by following the steps below:
- Tries to identify one of the color macros (RGB, HSV, HLS, or CMYK).
- If unsuccessful, then tries to identify HTML, hexadecimal or decimal formats.
- If unsuccessful, then grabs an identifier (useful when replacing variables or defines).
![Image 2](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
(Note that if a selection has been made by the user, ThePalette skips the scanning and takes the selected text at his/her risk.)
If the chunk of extracted text can be evaluated into a color (i.e., RGB( 123, 0xA0, 255 )
can be evaluated while RGB( rand() % 255, g1, 34 )
cannot), ThePalette inserts the color in the first available white ColorCtrl
and, if none is found, in the first (top-left) ColorCtrl
in the grid.
![Image 3](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
While running, ThePalette relies on certain combinations of mouse clicks, CTRL/SHIFT, and ARROW keys, in order to provide quick access to functionality. These combinations suit me but here is the code for you to modify it as it pleases you.
Now, the heart of ThePalette is the 4x5 grid of ColorCtrl
s. These controls display the colors of the active palette and behave as describe below.
LEFT-CLICK | Places the focus on the ColorCtrl under the mouse. |
Double LEFT-CLICK or ENTER key | Inserts the corresponding color macro/value into the code (add-in) and/or copies it to the clipboard (standalone). |
CTRL + LEFT-CLICK or MIDDLE-CLICK or SPACEBAR | Brings up the Windows Color Dialog. |
UP/DOWN/LEFT/RIGHT arrow keys | Move the focus within the ColorCtrl grid in the corresponding direction. |
CTRL + UP/LEFT arrow keys | Activates the next palette (in alphabetical order). |
CTRL + DOWN/RIGHT arrow keys | Activates the previous palette (in alphabetical order). |
SHIFT + UP/DOWN/LEFT/RIGHT arrow keys | Swap the colors of two adjacent ColorCtrl s in the corresponding direction. |
SHIFT + LEFT-CLICK | Starts a drag 'n' drop operation. If the SHIFT key is not pressed at release time, the color of the source ColorCtrl is copied to the target ColorCtrl . If the SHIFT key is pressed while releasing the LEFT mouse button, the source and target ColorCtrl s swap colors. |
RIGHT-CLICK or 'z' key | Activates the zoom and changes the mouse cursor to the crosshair. Read below for details on this operation. |
When the zoom is active, the following applies:
UP/DOWN/LEFT/RIGHT arrow keys | Moves the mouse cursor one pixel in the corresponding direction to facilitate precision placing. |
SHIFT + UP/DOWN/LEFT/RIGHT arrow keys | Moves the mouse cursor five pixels in the corresponding direction. |
CTRL + UP arrow key or rotating the MOUSE-WHEEL backwards | Zoom in. |
CTRL + DOWN arrow key or rotating the MOUSE-WHEEL forward | Zoom out. |
LEFT-CLICK or ENTER key | Picks the color of the pixel under the crosshair and places it in the color control that triggered the zoom operation. |
RIGHT-CLICK or ESC key | Cancels the zoom operation. |
The Options dialog should be self-explanatory.
![Image 4](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
The Palettes dialog facilitates simple management of palettes (add, rename, delete, duplicate). The listbox enumerates the palettes in alphabetical order and hover tooltips serve to remind us of the contents of each. The FillPalette dialog provides a quick way to (re)set a palette (clicking on the preview shows the original palette).
![Image 6](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Differences between the Add-in and the Standalone
Aside from the obvious difference, namely integration with Visual Studio, there is little difference between both versions.
The add-in creates two settings files. The first of these contains the information particular to a given project and is located in each workspace directory that has used ThePalette (the filename takes the form of '{Workspace}.tpl'). The second file contains shared settings (i.e. dialogs' positions) and is located in '{VisualStudio}\Common\MSDev98\Bin\ThePalette.ini'.
The standalone creates a single settings file ('ThePalette.ini') in the directory where ThePalette resides. The standalone also provides the option to inspect the clipboard on startup (the default behavior) and when minimized, it is sent to the SystemTray.
The Source Code
Because ThePalette is a small project, I decided to code it from scratch which, I'm afraid, means that the classes that make up the project are not as clean, tested, portable, documented, general, and fruity-flavored as they could or should be.
Nonetheless, there may be something of interest for you guys. A quick description of some of the classes follows:
CCSC_BT_KeyButton | Simple CButton -derived class that translates arrow-key events into focus changes. |
CCSC_ST_ColorCtrl | CStatic -derived class that customizes some mouse/keyboard events, implements drag 'n' drop, and triggers the zoom operation. |
CCSC_ST_FitToClient | Simple CStatic -derived class that displays a given text using the largest font that fits the control's client area. |
CCSC_ST_PalettePreview | Simple CStatic -derived class to create and display a palette in the FillPalette dialog. |
CCSC_ST_URL | Simple CStatic -derived class that displays a hover-sensitive hyperlink. |
CCSC_ST_Zoom | CStatic -derived class that captures and displays a screenshot of the desktop. |
CDAT_ColorCode | A collection of static methods for parsing, evaluation, and color conversions (See WARNING below). |
CDAT_IniFile | Encapsulates INI file reading/writing. |
CWIN_ToolTipManager | CWnd -derived class that displays multiline text and color-grid tooltips. |
Warning on the Color Conversions to/from HSV, HLS, and CMYK
I have not been able to find algorithms that convert to/from HSV, HLS and CMYK accurately (although the error may well be in my implementation).
ThePalette works with COLORREF
s internally and this requires a double conversion from the values found in your code to COLORREF
and then from COLORREF
to one of these formats. Therefore, ThePalette will report values that do not match those found in your code.
(Of course, if a color is manufactured - chosen, picked - with ThePalette, only one conversion is needed and what's shown will be inserted.)
Despite this problem, I have decided to include these formats because those of you that use them may still find ThePalette useful and may be able to let me know how to implement better algorithms. In the mean time, you know what to expect.
Acknowledgments
First and foremost, my gratitude goes to Alex Vallat for his ColourPicker, for that bit of help that made all the difference (mouse_event
), and for the unintentional challenge that eventually got me to write ThePalette.
Credit is also due to, at least, the following:
Other praiseworthy mentions must include:
- Dan Moulding's Visual Leak Detector which I used to check for memory shenanigans. A very, very useful tool.
- An unrelated objective of this endeavor was to test out Whole Tomato's Visual Assist X of which I've heard a great deal. Well, I installed the trial version and it did make development much easier. Acknowledging this is the least I can do (in return) for the guys at Whole Tomato Software.
History
Louis Armstrong, Count Basie, Chick Corea, Miles Davis, Benny Goodman, Spyro Gyra, Dizzy Gillespie, Keith Jarrett, Leroy Jenkins, Yusef Lateef, Al Di Meola, Glenn Miller, McCoy Tyner, Cecil Taylor, John Coltrane, Duke Ellington, Bill Evans, Ella Fitzgerald, Jean-Luc Ponty, John McLaughlin, Fats Navarro, Tito Puente, Paul Whiteman, Sun Ra, Caravan, Joe Farrell, Paco de Lucia, Weather Report, Charles Mingus, Pat Metheny, Charlie Parker, Charlie Byrd, Mahavishnu Orchestra, Wynton Marsalis, Return to Forever, Julien Loureau, Thelonious Monk, Max Roach , Pharaoh Sanders, Albert Ayler, Ornette Coleman, Sidney Bechet,...