Jump to content
shadowrecon

Why is this not working? --->

Recommended Posts

Im trying to get all of the colors in a TPA RGB values and store them in a T2DIntArray. Keep getting an our of range error but there is no way when i writeln the length it is always one more than high, Which accounts for the 0. Heres what i have:


Function ColorsToRGB(Colors: TIntArray): T2DIntArray;
Var
I: Integer;
begin
SetLength(Result,Length(Colors));
For I := 0 to High(Colors) do
ColorToRGB(Colors[i],Result[i][0],Result[i][1],Result[i][2]);
end;
[/Code]

 

----------------------------------------------------------------------------------------

So after some messing around i figured it out, But its not as clean and simple as the first code. If anyone can tell me why the first attempt didnt work i would appreciate it.

[scar]

Function ColorsToRGB(Colors: TIntArray): T2DIntArray;

Var

I,R,G,B: Integer;

begin

SetLength(Result,Length(Colors));

For I := Low(Result) to High(Result) do

begin

ColorToRGB(Colors[i],R,G,B);

Result[i] := [R,G,B];

end;

end;

[/scar]

Edited by shadowrecon
Link to comment
Share on other sites

You don't have your sub array lengths set to 3 so when you call [0] etc it's out of range, you'd have to preset those arrays using [0..2] (might cause other problems obviously) or use SetLength(, 3); in your other way you used [x, x, x] which set the array for you.

 

Or you could use types

 

type
 TRGB = record
   R, G, B: Integer;
 end;
 TRGBArray = array of TRGB;

function ColorsToRGB(Colors: TIntegerArray): TRGBArray;
var
 I, L, H: Integer;
begin
 H := High(Colors);
 L := Low(Colors);
 SetLength(Result, Length(Colors));
 for I := L to H do
   ColorToRGB(Colors[i], Result[i].R, Result[i].G, Result[i].B);
end;

Link to comment
Share on other sites

You don't have your sub array lengths set to 3 so when you call [0] etc it's out of range, you'd have to preset those arrays using [0..2] (might cause other problems obviously) or use SetLength(, 3); in your other way you used [x, x, x] which set the array for you.

 

Or you could use types

 

type
 TRGB = record
   R, G, B: Integer;
 end;
 TRGBArray = array of TRGB;

function ColorsToRGB(Colors: TIntegerArray): TRGBArray;
var
 I, L, H: Integer;
begin
 H := High(Colors);
 L := Low(Colors);
 SetLength(Result, Length(Colors));
 for I := L to H do
   ColorToRGB(Colors[i], Result[i].R, Result[i].G, Result[i].B);
end;

 

Thanks, I knew it was something with the sub arrays but didnt know how to declare them. As for types, I am loving them i just started playing with them last week and ive came up with some many useful applications. I think Im going to go with your idea for using types as it looks cleaner. One question is why do you put you high and low out of the for statement? Im assuming so something doesn't go wrong while looping, but there constant so they shouldn't change right?

Link to comment
Share on other sites

Thanks, I knew it was something with the sub arrays but didnt know how to declare them. As for types, I am loving them i just started playing with them last week and ive came up with some many useful applications. I think Im going to go with your idea for using types as it looks cleaner. One question is why do you put you high and low out of the for statement? Im assuming so something doesn't go wrong while looping, but there constant so they shouldn't change right?

 

It's really not needed to do low, it's just there incase someone is using obscure arrays that aren't 0...H but like [3..20]. The high is important, storing it in a variable keeps it High from being called every loop although you could do High downto 0 but I've experienced some bugs etc with 'downto 0' that and usually you want to go in chronological order from 0 to last.

Link to comment
Share on other sites

It's really not needed to do low, it's just there incase someone is using obscure arrays that aren't 0...H but like [3..20]. The high is important, storing it in a variable keeps it High from being called every loop although you could do High downto 0 but I've experienced some bugs etc with 'downto 0' that and usually you want to go in chronological order from 0 to last.

 

Yeah i normally use the low for that reason was more asking why you do not have them in the loop, but you answered my question. I guess calling High every time could cause a small performance issues slowing down the function's over all speed.

 

Instead of just using the TRGB i made the type like this, so i could store the color ect.

[scar]

Type

TColorInfo = Record

Color,Tol,R,G,B: Integer;

Hue,Sat: Extended;

end;

TColorInfoArray = Array of TColorInfo;

[/scar]

Link to comment
Share on other sites

Now that I think about it using Low you're probably still screwed with obscure arrays because Colors indexes won't match up with the Result indexes and you'll go out of range.. you'd have to have a separate value and Inc() it independently, so yea I wouldn't even bother with Low.

Link to comment
Share on other sites

Now that I think about it using Low you're probably still screwed with obscure arrays because Colors indexes won't match up with the Result indexes and you'll go out of range.. you'd have to have a separate value and Inc() it independently, so yea I wouldn't even bother with Low.

 

Thats kinda what i was thinking, I figure there going to pass an array of colors to the function to and it will just convert each one then return in the same order. What i figure is if they use the type TColorInfoArray they can just pass that var to my functions to convert/mod and they wont need any other vars.

 

---------- Post added at 03:00 AM ---------- Previous post was at 02:55 AM ----------

 

What do you think about these and the way i have them set up. Using types i shortened the all of code.

[scar]

{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Procedure ColorsToRGB(Var ColorInfo: TColorInfoArray);

Description: Converts Colors Into RGB Values

Contributors: ShadowRecon.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}

Procedure ColorsToRGB(Var ColorInfo: TColorInfoArray);

Var

I,H: Integer;

begin

H := High(ColorInfo);

For I := 0 to H do

ColorToRGB(ColorInfo.Color,ColorInfo.R,ColorInfo.G,ColorInfo.B);

end;

 

{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Procedure RGBsToColors(Var ColorInfo: TColorInfoArray);

Description: Converts RGB Values Into Colors

Contributors: ShadowRecon.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}

Procedure RGBsToColors(Var ColorInfo: TColorInfoArray);

Var

I,H: Integer;

begin

H := High(ColorInfo);

For I := 0 to H do

ColorInfo.Color := RGBToColor(ColorInfo.R,ColorInfo.G,ColorInfo.B);

end;

 

{=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Procedure ModRGBs(ModR, ModG, ModB: Integer; Var ColorInfo: TColorInfoArray);

Description: Modifies RGB Values By The Integer ModBy.

Example:

Contributors: ShadowRecon.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=}

Procedure ModRGBs(ModR, ModG, ModB: Integer; Var ColorInfo: TColorInfoArray);

Var

I,H: Integer;

begin

H := High(ColorInfo);

For I := 0 to H do

begin

IncEx(ColorInfo.R,ModR);

IncEx(ColorInfo.G,ModG);

IncEx(ColorInfo.B,ModB);

end;

end;

[/scar]

Edited by shadowrecon
Link to comment
Share on other sites

There is one case where Low (and High) is handy. I always forget when indexing elements of a string that they start at 1 and go to Length and not 0 through High. Every blasted time.

 

mutha f***** your kidding right? I was working on a function few days ago and could not figure out why it keep throwing an out of bounds, but if i added a -1 to High it worked.... lmao.. So why do string arrays start at 1 not 0?

Link to comment
Share on other sites

mutha f***** your kidding right? I was working on a function few days ago and could not figure out why it keep throwing an out of bounds, but if i added a -1 to High it worked.... lmao.. So why do string arrays start at 1 not 0?
It's a quirk of the pascal language: In the original string datatype (limited to 255 chars) element 0 contained the length of the string, with the first character at element 1. Strings are no longer limited to 256 characters and I don't think element 0 still holds the length.

 

A number of things are different in pascal just because.

Edited by Bixby Sayz
Link to comment
Share on other sites

The fact that SomeString[1] is the first character of the string, and SomeArray[0] is the first element of the array applies to SCAR.

 

String: 1...Length

Array: 0...(Length - 1) or High (Normally)

 

As Wanted stated you can do silly things like

A: array [3..5] of Integer;

In which case Low=3, High=5, Length=3, First Element = A[3]. Not recommended except for special circumstances.

Edited by Bixby Sayz
Link to comment
Share on other sites

The fact that SomeString[1] is the first character of the string, and SomeArray[0] is the first element of the array applies to SCAR.

 

String: 1...Length

Array: 0...(Length - 1) or High (Normally)

 

As Wanted stated you can do silly things like

A: array [3..5] of Integer;

In which case Low=3, High=5, Length=3, First Element = A[3]. Not recommended except for special circumstances.

 

I gotcha on the bottom part, but never knew that about string arrays thanks for the information i will keep that in mind from now on.

Link to comment
Share on other sites

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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



×
  • Create New...