Getting the OneNote section coloring powertoy to work better
Some minor challenges cropped up when I was working on the OSCO powertoy.
In no particular order, the first was converting the RGB values from the control from decimal to hex values. The color format string for the schema is in this format: pattern = #[a-fA-F0-9]{6}. The color control already returns a red, green and blue setting, but as a byte value of 0-255 (in decimal). I needed to convert that to hex to match the OneNote format.
Quick example: the color I used last time was: color="#F6B078" , which is "Orange" to OneNote. So Orange is a red value of F6 (246 in decimal), green is B0 (176) and blue is 78 (120). Throw a pound sign in front of the string and I am done.
Converting from decimal the hex was fairly easy (https://msdn.microsoft.com is my friend). I looked for a Convert method on a byte object, but there wasn't one that I could find. Fortunately, the .ToString() operator takes an optional "X" parameter that will return the hex value of the string. Here's the line from the code:
string redColor = selectedColor.R.ToString("X");
In this case, selectedColor hold the value the user chose from the Choose Color dialog already. Same idea for green and blue.
Next, create the string and replace the value for color in the selected section node:
nodeToChange.Attributes.GetNamedItem("color").Value = "#" + redColor + greenColor + blueColor;
Remember that leading # sign – at one point, I forgot it and hit an error. Adding it, I was ready to test this so I started OSCO, selected pure red (red = 255, blue and green were both zero) and I got an error: 0x80042001, which is XML is invalid. This was odd since pure red should have worked fine. Breaking into the debugger showed the string I trying to use was "#FF00". Looking back at the required format, I needed 6 total digits, but only had 4. The "0" values needed to be "00" - makes sense. The fix here was easy:
if (selectedColor.R == 0)
redColor = "00";
Again, same for green and blue. Now it worked fine and properly showed the preview when you selected a color.
Then I did a similar check to get the existing color of the node from the tree control when you selected a section. I updated the background color to reflect the current section color. This was the last thing I added - now it's time to just give it away!
I figured with all the focus on OneNote 2010 it would be nice to let all the OneNote 2007 users get a powertoy. Thanks for using OneNote!
Questions, comments, concerns and criticisms always welcome,
John