Lazarus

Programming => Graphics and Multimedia => Graphics => Topic started by: Jumbo on March 13, 2019, 02:12:50 pm

Title: [SOLVED] coordinatesysten issue or not
Post by: Jumbo 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.
Title: Re: coordinatesysten issue or not
Post by: VTwin on March 13, 2019, 02:18:55 pm
Why are you typecasting to a byte?
Title: Re: coordinatesysten issue or not
Post by: Jumbo 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
Title: Re: coordinatesysten issue or not
Post by: lucamar 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;
Title: Re: coordinatesysten issue or not
Post by: Jumbo 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.
Title: Re: coordinatesysten issue or not
Post by: Jumbo 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.
Title: Re: coordinatesysten issue or not
Post by: VTwin 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.
Title: Re: coordinatesysten issue or not
Post by: Jumbo 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 !