Author Topic: [SOLVED] Getting colour of an image?  (Read 10147 times)

Jishaxe

  • Full Member
  • ***
  • Posts: 103
  • Hobbist Programmer
[SOLVED] Getting colour of an image?
« on: March 14, 2012, 03:00:23 pm »
I have a square image that consists of one colour. Using Lazarus/Pascal, how could I get the colour of said image in RGB format (255;255;255)? A chunk of code I can steal/alter slightly would be great, along with an explanation :)
« Last Edit: March 15, 2012, 04:45:11 pm by Jishaxe »
Linux Mint 12
Windows 7 Home Premium
______________________
Definition of programmer: An organism that converts caffeine into software.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Getting colour of an image?
« Reply #1 on: March 14, 2012, 04:24:52 pm »
I suppose the wiki page answers to your question:
http://wiki.freepascal.org/Colors

Jishaxe

  • Full Member
  • ***
  • Posts: 103
  • Hobbist Programmer
Re: Getting colour of an image?
« Reply #2 on: March 15, 2012, 07:39:22 am »
I suppose the wiki page answers to your question:
http://wiki.freepascal.org/Colors
I'm sorry, I may not have explained myself properly. I have an image that i will load into my software. The image will be a square filled in with a random colour. I wish to be able to "scan" the image from within my program and have it detect what colour the square is filled in with. If I need to pick a colour from a single pixel that is fine because the whole square wil be the same colour.
Thanks!
Linux Mint 12
Windows 7 Home Premium
______________________
Definition of programmer: An organism that converts caffeine into software.

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Getting colour of an image?
« Reply #3 on: March 15, 2012, 07:52:41 am »
Just saying "Image" is far too generic. An image can be anything, it could be a simple double array of colors, or it could be an object of the class TBitmap, or an object of another class, or even TImage ... you should specify what type you are using.

But suposing that you have a normal TGraphic descendent such as TBitmap then you can use MyBitmap.Canvas.Pixels

See also the docs on TCanvas: http://lazarus-ccr.sourceforge.net/docs/lcl/graphics/tcanvas.html

This will give you the color in the format TColor which can be decomposed in the 3 channels using the routine RegGreenBlue: http://lazarus-ccr.sourceforge.net/docs/lcl/graphics/redgreenblue.html
« Last Edit: March 15, 2012, 07:54:38 am by felipemdc »

Jishaxe

  • Full Member
  • ***
  • Posts: 103
  • Hobbist Programmer
Re: Getting colour of an image?
« Reply #4 on: March 15, 2012, 08:08:04 am »
I'm sorry, I meant a non programmatic image. A file typically with an extenion of .png or .jpg that contains a series of coloured pixels that create a picture. Like an icon for an application, or a desktop wallpaper.
I believe the class for this is TImage. I think this is a descendant of TGraphic, so will I be able to use MyImage.Canvas.Pixels? (Can't try, not on PC ATM) If so, does the image have to be drawn on the GUI? Can I specify which pixel has the colour read from it?
Linux Mint 12
Windows 7 Home Premium
______________________
Definition of programmer: An organism that converts caffeine into software.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Getting colour of an image?
« Reply #5 on: March 15, 2012, 09:31:08 am »
There is also Canvas.Colors[] array which in record format, giving you r,g,b without using separate functions for it.

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Getting colour of an image?
« Reply #6 on: March 15, 2012, 10:12:51 am »
TImage is a control to display static images, I don't recommend using it for other tasks, although it will work. TImage does not descend from TGraphic

If you just want to load a image from the disk and read it's colors, use the appropriate reader class. I documented this here:

http://wiki.lazarus.freepascal.org/Developing_with_Graphics#Loading.2FSaving_an_image_from.2Fto_the_disk

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Getting colour of an image?
« Reply #7 on: March 15, 2012, 10:43:51 am »
(offtopic)
Would be nice to move Wiki focus to other formats more (PNG, JPG). Nobody really uses BMP format anymore. Assuming that PNG's should be first converted to TBitmap, makes them lose alpha channel.

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Getting colour of an image?
« Reply #8 on: March 15, 2012, 11:47:06 am »
I did some changes to show that one can switch the classes at will

Jishaxe

  • Full Member
  • ***
  • Posts: 103
  • Hobbist Programmer
Re: Getting colour of an image?
« Reply #9 on: March 15, 2012, 03:04:59 pm »
Okay, this is nice. So I can just load the image from file, call MyBM.Canvas.Pixels.GetPixel(2,2), then get the colour with RedGreenBlue(MyColor) and there we go? Asking for confirmation on here again because I'm not on a computer and would like it sorted before I get home :)
Linux Mint 12
Windows 7 Home Premium
______________________
Definition of programmer: An organism that converts caffeine into software.

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Getting colour of an image?
« Reply #10 on: March 15, 2012, 03:28:43 pm »
Yes, only correction is that the syntax is:

var
  MyColor: TColor;
  lRed, lGreen, lBlue: Byte;
begin
  // ....
  MyColor := MyBMP.Canvas.Pixels[2,2]
  RedGreenBlue(MyColor, lRed, lGreen, lBlue);

Jishaxe

  • Full Member
  • ***
  • Posts: 103
  • Hobbist Programmer
Re: Getting colour of an image?
« Reply #11 on: March 15, 2012, 04:42:45 pm »
Ah thanks a lot, this is great :)
Solved
Linux Mint 12
Windows 7 Home Premium
______________________
Definition of programmer: An organism that converts caffeine into software.

 

TinyPortal © 2005-2018