### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [SOLVED] coordinatesysten issue or not  (Read 1814 times)

#### Jumbo

• New Member
• Posts: 13
##### [SOLVED] coordinatesysten issue or not
« on: March 13, 2019, 02:12:50 pm »
Hello Programmers,

I have run into some issue I see no answer to,

I have a square image divided into four fields. upperleft == red, UpperRight == Blue, LowerLeft == white, Lower right == Green.

I want to know the coloer of each field and click on it to get the text as a start, but it shows me wrong or unexpected answers.

This code:
Code: Pascal  [Select][+][-]
1. procedure TForm2.Image1MouseDown(Sender: TObject; Button: TMouseButton;
2.   Shift: TShiftState; X, Y: Integer);
3. var
4.   c: TColor;
5. begin
6.   c:=Byte(Form2.Image1.Canvas.Pixels[X,Y]);
7.   Form1.FileNameEdit2.Caption:='X = '+IntToStr(X)+'  Y = '+IntToStr(y)+
8.      '  Color = '+ColorToString(c);
9. end;
10.

When I click on red it says : X = 166  Y = 98  Color = \$000000FE
click on white:  X = 156  Y = 262  Color = clRed
click on Bleu : X = 397  Y = 87  Color = clBlack
click on Green: X = 401  Y = 265  Color = clBlack

The click on white makes me think the coordinatesystem is inverted, but I guesss thats not treu..
The click on white does the same,   but blue and green realy puzzles me....

I even have no idea what I do wrong.

So, indeed, any help is epreciated.
« Last Edit: March 17, 2019, 02:24:22 pm by Jumbo »

#### VTwin

• Hero Member
• Posts: 979
• Former Turbo Pascal 3 user
##### Re: coordinatesysten issue or not
« Reply #1 on: March 13, 2019, 02:18:55 pm »
Why are you typecasting to a byte?
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.0
macOS 10.13.6: Lazarus 2.0.12 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.12 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.12 (64 bit on VBox)

#### Jumbo

• New Member
• Posts: 13
##### Re: coordinatesysten issue or not
« Reply #2 on: March 13, 2019, 02:31:15 pm »
I am fairly new to lazarus, maby thats an issue too, but I just try to use the functions as givven by the manual.

But tell me what I do wrong...

I already tried this one too, but gives also silly answers.

Code: Pascal  [Select][+][-]
1. Form1.FileNameEdit2.Caption:=
2.      ColorToString(Red(c))+' '+
3.      ColorToString(Blue(c))+' '+
4.      ColorToString(Green(c));
5.

Dont look at the FileEdit.... thats just an interim view solution

#### lucamar

• Hero Member
• Posts: 3779
##### Re: coordinatesysten issue or not
« Reply #3 on: March 13, 2019, 02:35:57 pm »
When I click on red it says : X = 166  Y = 98  Color = \$000000FE
click on white:  X = 156  Y = 262  Color = clRed
click on Bleu : X = 397  Y = 87  Color = clBlack
click on Green: X = 401  Y = 265  Color = clBlack

Think!
Code: [Select]
`Red = \$0000FE {Not  a pure full red, then};White = \$FFFFFF;Blue = \$FF0000;Green = \$00FF00;`
Since you typecast to a byte, they get converted to
Code: [Select]
`Red = \$FE;White = \$FF;Blue = \$00;Green = \$00;`
which, when converted back to TColor gives:
Code: [Select]
`Red = \$0000FE;White = \$0000FF = clRed;Blue = \$000000 = clBlack; Green = \$000000 = clBlack;`
Yor code should be:
Code: Pascal  [Select][+][-]
1. procedure TForm2.Image1MouseDown(Sender: TObject; Button: TMouseButton;
2.   Shift: TShiftState; X, Y: Integer);
3. var
4.   c: TColor;
5. begin
6.   c:= Form2.Image1.Canvas.Pixels[X,Y]);
7.   Form1.FileNameEdit2.Caption :=
8.     Format( 'X = %d   Y = %d   Color = %s', [X, Y, ColorToString(c)));
9. end;
« Last Edit: March 13, 2019, 02:41:12 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

#### Jumbo

• New Member
• Posts: 13
##### Re: coordinatesysten issue or not
« Reply #4 on: March 13, 2019, 02:36:21 pm »
As some background to the question...

The image will be devided at last in multiple squares and each sqaure will get the avarage color of that square.
So the amount of colors is redused.

#### Jumbo

• New Member
• Posts: 13
##### Re: coordinatesysten issue or not
« Reply #5 on: March 13, 2019, 02:39:51 pm »
Owwww, How stupid of me. Now I see the typecast. BYTE.

I just overlooked it. I think I can manage to rewrite it. And post the answer.

#### VTwin

• Hero Member
• Posts: 979
• Former Turbo Pascal 3 user
##### Re: coordinatesysten issue or not
« Reply #6 on: March 13, 2019, 02:43:10 pm »
Your second try also has the problem, Red, Blue, and Green functions return byte from TColor, not TColor.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.0
macOS 10.13.6: Lazarus 2.0.12 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.12 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.12 (64 bit on VBox)

#### Jumbo

• New Member
• Posts: 13
##### Re: coordinatesysten issue or not
« Reply #7 on: March 13, 2019, 02:52:35 pm »
Both forms working as test!

Code: Pascal  [Select][+][-]
1. procedure TForm2.Image1MouseDown(Sender: TObject; Button: TMouseButton;
2.   Shift: TShiftState; X, Y: Integer);
3. var
4.   c: TColor;
5. begin
6.   c:=Form2.Image1.Canvas.Pixels[X,Y];
7.
8. // method 1
9.   Form1.FileNameEdit2.Caption:=' ' +
10.   ' X = '+IntToStr(X)+'  Y = '+IntToStr(y)+
11.      '  Color = '+ ColorToString(c);
12.
13. // method 2
14.   Form1.FileNameEdit2.Caption:='RGB = '+ IntToStr(Red(c))+' '+
15.   IntToStr(Green(c))+' '+IntToStr(Blue(c));
16. end;
17.

Thanks for all quick help !
« Last Edit: March 13, 2019, 02:54:17 pm by Jumbo »