I don't have a specific code sample on hand for something like this but here's an idea on how to do it with math. Set 0 - 9 to their value and A - F to 10 - 15, like below (you will also need to multiple these values by 16, will make sense in a second):
0 : 0 : 0
1 : 1 : 16
2 : 2 : 32
...
A : 10 : 160
B : 11 : 176
C : 12 : 192
...
F : 15 : 240
Once you have this set up you can (assuming your color is in HEX) separate the 3 groupings and convert each to their hexadecimal equivalent. The first character of the two is multiplied by 16 and the 2nd is multiplied by 1:
FF = (16 * 15) + (15 * 1) = 255
AE = (16 * 10) + (14 * 1) = 174
0F = (16 * 0 ) + (15 * 1) = 15
Once you have the hexadecimal numbers for the color the user picks you can multiple each hexadecimal number based on a formula.
Below is an example, I'm using a lighter red and I want to find a darker red. My starting HEX is DB4658 and I'm going to multiple my values by .5 to get my darker red:
- Separate into 3 groups: DB 46 58
Calculate the Hexadecimal value of each:
D = 13 B = 11 4 = 4 6 = 6 5 = 5 8 = 8
DB = 219 = 13 * 16 + 11 * 1
46 = 70 = 4 * 16 + 6 * 1
58 = 88 = 5 * 16 + 8 * 1
Now calculating those into the darker HEX (I floored the number):
219 * .5 = 109
70 * .5 = 35
88 * .5 = 44
Convert this back into our 3 Groupings, for the first character look at your
table you have built in the multiply by 16 column:
109 = 96 + 13 = 6D
35 = 32 + 3 = 23
44 = 32 + 12 = 2C
You now have your darker red.
You might adjust the .5 to .75 if you want the contrast closer together and if the color they pick is already dark then the adjustment should be by 1.25x or 1.5x.
Hopefully you find this helpful. If you need clarification let me know.