[SOLVED] Getting colour of an image? lazarus / 39-Graphics ---------------------------------------------------------------------- Jishaxe · 2012-03-14 14:00 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 ---------------------------------------------------------------------- User137 · 2012-03-14 15:24 I suppose the wiki page answers to your question: http://wiki.freepascal.org/Colors ---------------------------------------------------------------------- Jishaxe · 2012-03-15 06:39 Quote from: User137 on March 14, 2012, 04:24:52 pmI 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! ---------------------------------------------------------------------- felipemdc · 2012-03-15 06:52 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 ---------------------------------------------------------------------- Jishaxe · 2012-03-15 07:08 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? ---------------------------------------------------------------------- User137 · 2012-03-15 08:31 There is also Canvas.Colors[] array which in record format, giving you r,g,b without using separate functions for it. ---------------------------------------------------------------------- felipemdc · 2012-03-15 09:12 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.2F Saving_an_image_from.2Fto_the_disk ---------------------------------------------------------------------- User137 · 2012-03-15 09:43 (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 · 2012-03-15 10:47 I did some changes to show that one can switch the classes at will ---------------------------------------------------------------------- Jishaxe · 2012-03-15 14:04 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 ---------------------------------------------------------------------- felipemdc · 2012-03-15 14:28 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 · 2012-03-15 15:42 Ah thanks a lot, this is great Solved