Re: manipulating hex values

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi darrell,

Use the Int32.Parse(String, System.Globalization.NumberStyles) overload to
parse the string, and perform math on it. Then use the
Int32.ToString(String) method to translate it back. Example:

string color = "#6699cc";
int r, g, b;
double rr, gg, bb;

// Parse string to Int32 values
r = Int32.Parse(color.Substring(1, 2),
System.Globalization.NumberStyles.AllowHexSpecifier);
g = Int32.Parse(color.Substring(3, 2),
System.Globalization.NumberStyles.AllowHexSpecifier);
b = Int32.Parse(color.Substring(5, 2),
System.Globalization.NumberStyles.AllowHexSpecifier);

rr = (double)r/255D;
gg = (double)g/255D;
bb = (double)b/255D;

// perform some math on the percentages, then convert back

r = (int)(rr * 255D);
g = (int)(gg * 255D);
b = (int)(bb * 255D);

color = "#" + r.ToString("X2") + g.ToString("X2") + b.ToString("X2");

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.


"darrel" <notreal@xxxxxxxxxxx> wrote in message
news:%23%23uEOFYPGHA.2704@xxxxxxxxxxxxxxxxxxxxxxx
(apologies if I've asked this before in here...I thought I had, but can't
seem to find my original post).

I'm trying to automate the process of picking some colors. I want to
select one color, then programtically generate some different values of
the same color...both lighter and darker.

I'd like to start and end with a hex value.

Can that be done in VB.net? It looks like I can, but the hex values I see
in examples don't quite match the typical hex notation of color values in
CSS.

For instance, I'd like to take this:

#6699cc

Split it into 3 values (easy enough) and convert to a percentage value
(not sure on that step, but basically 'hex converted to base 10/255'):

66 = .4
99 = .6
cc = .8

Then do some math on each (like multiply by 1.2 to make a ligher color)
and then convert back to hex.

It looks like hex is a type of LONG, but I'm not exactly sure how to
translate between the two. Is ctype the answer?

-Darrel





.



Relevant Pages

  • Re: Convert HTML to String
    ... // Translate this into the ASCII equivalent? ... is your problem to translate a string of kind "XX" where XX is an hex ...
    (microsoft.public.vc.mfc)
  • Re: 4-byte ieee754 single-precision floating-point number, convert to string in JS
    ... They are the serialization of the 32-bit single-precision floating- ... point number whose value in hex is 0x41a8147b and whose decimal value ... Or a Method of String which takes 8 characters of the existing String ... scripts written by Dr JRStockton: ...
    (comp.lang.javascript)
  • Hex editor display - can this be more pythonic?
    ... I'm building a hex line editor as a first real Python programming exercise. ... I had considered using the .translatemethod of strings, however this would require a larger translation table than my printable string. ... Where printing chars are shown in parenthesis, characters with Python escape sequences will be shown as their escapes in parens., while non-printing chars with no escapes will be shown with nothing in parens. ...
    (comp.lang.python)
  • Re: compilation problem
    ... want this utility to print decimal for hex numbers entered. ... IF the third argument passed is a string specifying a decimal number ... TERMINATE with an EXIT_SUCCESS returncode ...
    (comp.lang.c)
  • Re: Need a sysRPL CRC16 routine.
    ... Thanks for your thoughts on the string> hex and vice versa. ... Sysrpl is not particularly intuitive on this point. ... effort he has put into the Debug4x programming environment. ...
    (comp.sys.hp48)