# parseInt question

## Recommended Posts

• Moderators

the function below calculates the relative brightness of a colour (specified in hex : #008080 = 'teal')

```    //funtion to find the relative brightness of a colour (hex-code)
function Brightness(colour: string): integer;
begin
asm
function coloro(color) {
if(color.length==7){color=color.substring(1);}
var R =	parseInt(color.substring(0,2),16);
var G =	parseInt(color.substring(2,4),16);
var B =	parseInt(color.substring(4,6),16);
return Math.sqrt(R * R * .241 + G * G * .691 + B * B * .068);
}
@result = coloro(colour);
end;
end;```

and is used to determine the best text colour on specific backgrounds (brightness < 130 = white else black);

Not unlike the ColorListBox project in featured demo's. The above algorithm is a bit different than that demo and supposedly more accurate.

Function works well but I don't like the asm block. Does anyone know if there is a Delphi/Pascal routine available somewhere which does the same as the javascript parseInt function ?

##### Share on other sites
```function Brightness(colour: Variant): Float;
function coloro(color: Variant): Float;
var
R, G, B: Integer;
function parseInt(s: Variant{String}; radix: integer = 0): integer; external "parseInt";
begin
if(color.length=7) then color:= color.substring(1);
R :=	parseInt(color.substring(0,2), 16);
G :=	parseInt(color.substring(2,4), 16);
B :=	parseInt(color.substring(4,6), 16);
Result := sqrt(R * R * 0.241 + G * G * 0.691 + B * B * 0.068);
end;
begin
Result := coloro(colour);
end;```

##### Share on other sites
• Moderators

amazing, thanks heaps

##### Share on other sites

I was about to write that the snippet simply copies 2 by 2 hex characters and then converts that to an integer number.
In pure pascal it would be a bit messy since pascal's StrToInt doesnt support hex (unless prefixed by \$).
ASM sections are very much needed, they might not be pretty (not too fond on them myself) but there are some fundamental code sections that would never work if we used pascal only. Speed is also a factor to keep in mind, especially for procedures that you will call many times.

But you seem all set so i'll leave you to it! Happy coding!

##### Share on other sites
• 1 year later...
• Moderators

Hmmm

The code from @warleyalex above used to work for me for a long time

```    function Brightness(colour: String): Float;

function computeBrightness(color: Variant): Float;
var R, G, B: Integer;

function parseInt(s: Variant{String}; radix: integer = 0): integer; external "parseInt";
begin
if(color.length=7) then color := color.substring(1);
R := parseInt(color.substring(0,2), 16);
G := parseInt(color.substring(2,4), 16);
B := parseInt(color.substring(4,6), 16);
Result := sqrt(R * R * 0.241 + G * G * 0.691 + B * B * 0.068);
end;
begin
Result := computeBrightness(colour);
end;```

However since 3.9 I get "Uncaught ReferenceError: computeBrightness is not defined"

The compiled code does indeed not include the computeBrightness function

What changed ?

##### Share on other sites
• Moderators

Apparently nested function declarations are no longer supported ?

Changing the above code to below solves the problem

```    function computeBrightness(color: Variant): Float;
var R, G, B: Integer;
begin
function parseInt(s: Variant{String}; radix: integer = 0): integer; external "parseInt";
if(color.length=7) then color := color.substring(1);
R := parseInt(color.substring(0,2), 16);
G := parseInt(color.substring(2,4), 16);
B := parseInt(color.substring(4,6), 16);
Result := sqrt(R * R * 0.241 + G * G * 0.691 + B * B * 0.068);
end;

function Brightness(colour: String): Float;
begin
Result := computeBrightness(colour);
end;
```

hmmm

##### Share on other sites
12 minutes ago, lynkfs said:

Apparently nested function declarations are no longer supported ?

Sure, they are supported. In your first example the code just looks extra confusing. But nevertheless, it actually compiles with the latest Alpha.

##### Share on other sites
• Moderators

Correct, both snippets compile fine pre and post version 3.9

However the first snippet gives the runtime error in version 3.9, and not in versions before (tested it in all major browsers too)

It's not a big deal, just happened to stumble across this minor change in compiler behaviour

and true, snippet 1 is a bit confusing to read anyway.

##### Share on other sites

So the problem might be with parseInt itself and not nested functions. Let me have a look.

Nope, the problem seems to be with Smart linking. If you have it enabled in Project Options, computeBrightness is eliminated from the generated JS.

I'll have a look if this is a known bug in DWS and if not, report this to Eric.

##### Share on other sites

A little update: Eric found the bug and fixed it in DWS. I'll include the fix in the next update.

##### Share on other sites
• Moderators

Wonderful, thanks

##### Share on other sites

A new Alpha is now available. @lynkfs can you confirm that it fixes the problem?

##### Share on other sites
• Moderators

yep, fixed the problem, all works as expected 👍

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×