Hex - Why is that? 
Author Message
 Hex - Why is that?

I'm trying to set colors on ASP using dhtml.

<P style="color:<%=Hex(arrColors(i))%>">
<%=arrCountries(i)%>
</P>

where arrColors stores my colors in integers (integere subtype).

style {color} requires number in full hex format (or name) , while Hex()
from VBScript returns only significant digits (without zeros).

Why is that? It annoys me.

Someone knows nice solution?

Thanks

Julius



Sat, 25 Aug 2001 03:00:00 GMT  
 Hex - Why is that?

Quote:

>I'm trying to set colors on ASP using dhtml.

><P style="color:<%=Hex(arrColors(i))%>">
><%=arrCountries(i)%>
></P>

>where arrColors stores my colors in integers (integer subtype).

>style {color} requires number in full hex format (or name) , while Hex()
>from VBScript returns only significant digits (without zeros).

>Why is that? It annoys me.

I dunno why, but it *is* annoying, isn't it.

Easy solution:

     <%
            temp = Hex( arrColors(i) + &H10000000 )
            temp = Right( temp, 6 )
    %>
     <P style="color:<%= temp %>">

The trick:  You only need 6 hex digits for RGB,
so by adding &H10000000 to your number, we
ensure that the resulting hex string will look like
        10rrggbb
then we use Right to get just the righthand 6
digits.  Voila.

Warning: I haven't tried exactly this with style=...
You might need a prefix on the <%= temp%>
string??



Sat, 25 Aug 2001 03:00:00 GMT  
 Hex - Why is that?
Well, I would have gone for:
    temp = right("000000" & Hex( arrColors(i) ), 6)
both  work pretty much the same.

    However, the write may have a different problem.  As I recall, a Win32
COLORREF and HTML's color style, want the RGB values in different orders.

--
Truth,
   James [MVP]
http://www.NJTheater.Com       -and-
http://www.NJTheater.Com/JamesCurran

Quote:

>     <%
>            temp = Hex( arrColors(i) + &H10000000 )
>            temp = Right( temp, 6 )
>    %>
>     <P style="color:<%= temp %>">



Fri, 31 Aug 2001 03:00:00 GMT  
 Hex - Why is that?
Or you can deal with every set of Hex numbers individually, adding a 0
'zero' to the left and then using the two characters from the right. You can
use something similar to the FixHex function below.

Function FixHex(LETTER)
LETTER = "0" & LETTER
LETTER = Right(LETTER, 2)
FixHex = LETTER
End Function

AB.



Fri, 31 Aug 2001 03:00:00 GMT  
 Hex - Why is that?

Quote:

>Well, I would have gone for:
>    temp = right("000000" & Hex( arrColors(i) ), 6)
>both  work pretty much the same.

The original author noted that my solution DID
work.  And, I have no doubt, yours would, too.

However, if we want to nit pick...

    temp1 = Right( Hex( arrColors(i) + &H10000000 ), 6 )
vs.
    temp2 = right("000000" & Hex( arrColors(i) ), 6)

The temp1 version will execute measurably faster
(well..."measurably" assuming you do it 100,000
times or so to get the entire thing up to something
less than microseconds).

Reason:

Temp1 has to
        add an integer constant to an integer array member,
                    [ very fast...even in VBScript ]
        invoke the Hex function, and
                    [ creates a string buffer...slower ]
        invoke the Right function.
                    [ creates another string...slower ]
                    [ two strings to be garbage collected later ]

versus

Temp2 has to
        get an integer array member
                    [ very fast ]
        invoke the Hex function
                    [ create string ]
        invoke the string append operation
                    [ create string ]
        invoke the Right function
                    [ create string ]
                    [ THREE strings to be garbage collected later! ]

Since string operations in VBScript are [ how can
I say this nicely? ] not blindingly fast, saving one
string operation as well having one fewer strings
to be garbage collected helps.  In a very minor way.

Anyway, this response was just for the fun of it!

Both answers would work quite well.



Sat, 01 Sep 2001 03:00:00 GMT  
 Hex - Why is that?

Quote:

>    temp1 = Right( Hex( arrColors(i) + &H10000000 ), 6 )
>vs.
>    temp2 = right("000000" & Hex( arrColors(i) ), 6)

>The temp1 version will execute measurably faster

But you are wasting a byte! Your constant hex number is one hex digit too
long, hence your string is one character longer than needed ;-).

Also, I think (although don't know) that "Or" would be better, since it's
not going to do over-flow checks and so on.

    temp1 = Right( Hex( arrColors(i) Or &H1000000 ), 6 )

Peter (I missed the original thread; just picked up on this "fun
nit-picking" :-) )

--
Peter Torr, Developer / Webmaster          Vantage Systems Australia.




Mon, 03 Sep 2001 03:00:00 GMT  
 Hex - Why is that?

Quote:


>>    temp1 = Right( Hex( arrColors(i) + &H10000000 ), 6 )
>>vs.
>>    temp2 = right("000000" & Hex( arrColors(i) ), 6)

>>The temp1 version will execute measurably faster

>But you are wasting a byte! Your constant hex number is one hex digit too
>long, hence your string is one character longer than needed ;-).

Actually, two bytes.  Because characters in VBScript are
two bytes each.  Okay.  You get credit on this one.

Quote:

>Also, I think (although don't know) that "Or" would be better, since it's
>not going to do over-flow checks and so on.

>    temp1 = Right( Hex( arrColors(i) Or &H1000000 ), 6 )

Gimme back the credit.  [huge grin]  Actually, even VBScript
is smart enough to figure out that it is adding an integer to
an integer.  In which case there aren't any overflow checks.

On the other hand, the "Or" operator has to check to see if
one (or both) of its operands is a logical expression and, if so,
do a logical OR instead of a numeric one.  It is possible the
VBScript compiler can figure that out, but I suspect that it
doesn't...that it defers that decision to runtime.

If you REALLY care, I'll try to check it out.  (I'm porting VBScript
to OS/390 right now; I pretty well understand the parser and
token generator, but I haven't yet worked much with the runtime
interpretation.)

Wow!  Even more "fun".  Right?



Mon, 03 Sep 2001 03:00:00 GMT  
 Hex - Why is that?

Quote:

>Actually, two bytes.  Because characters in VBScript are
>two bytes each.  Okay.  You get credit on this one.

OK, two bytes.

Quote:
>Gimme back the credit.  [huge grin]  Actually, even VBScript
>is smart enough to figure out that it is adding an integer to
>an integer.  In which case there aren't any overflow checks.

Nope, not true (thanks Eric ;-) ).

Quote:
>On the other hand, the "Or" operator has to check to see if
>one (or both) of its operands is a logical expression and, if so,
>do a logical OR instead of a numeric one.  It is possible the
>VBScript compiler can figure that out, but I suspect that it
>doesn't...that it defers that decision to runtime.

Again, this is actually incorrect - after some off-line discussion with Bill
and Eric, it has been noted that Or *is* faster than addition, so the Or
code is faster...

Quote:
>Wow!  Even more "fun".  Right?

As long as you're right ;-) Ha ha ha,

Peter

--
Peter Torr, Developer / Webmaster          Vantage Systems Australia.




Fri, 07 Sep 2001 03:00:00 GMT  
 Hex - Why is that?
[Discussion about saving two bytes, etc.]

OK, having had discussions with other people, I feel obliged to make the
following statement:

(If this all sounds obvious - good for you. If it doesn't, then please take
note.)

The recent debate about saving two bytes or a few millionths of a second by
doing a bit-wise OR instead of an addition should *NOT* be taken as good
general advice on how to code! It might be fun (for some people ;-) ) to
theorise about which particular line of code will run faster than another
line of code, but it's not a good way to actually *write* your code,
especially if you are a beginner.

You shouldn't try and hack your code just to save a few instructions at the
expense of readability / maintainability / etc. unless it is *absolutely
critical* that the code be as small / fast as possible (in which case, why
are you using script? :-) ).

It is far more important that your code *work correctly* than that it runs
half a nanosecond faster. Once your code works correctly (and how do you
prove that?), you might consider improving performance *if it is going to
make a difference*.

This has been a community service announcement.

Peter

--
Peter Torr, Developer / Webmaster          Vantage Systems Australia.




Sat, 08 Sep 2001 03:00:00 GMT  
 Hex - Why is that?

Quote:

>[Discussion about saving two bytes, etc.]

>It is far more important that your code *work correctly* than that it runs
>half a nanosecond faster.

Hear, hear!!!  And my apologies if I led anybody down
the garden path!

Write CLEAR code and you will win almost every time!

Bill Wilkinson
Chili!Soft, Inc.



Sat, 08 Sep 2001 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Hex - Why is that?

2. Please Help: Why am I now getting this error

3. Why am I getting this error ?

4. Converting hex value in str format to hex

5. why am I getting this error??

6. Why am I losing html formatting?

7. Why am I getting this message?

8. Null Values - WHY am I getting this error?

9. why am I out of memory?

10. Why am I printing the entire database

11. Why am I getting a Closed Recordset

12. Why am I so Slow

 

 
Powered by phpBB® Forum Software