# Get pixel color from TCastleImageControl

**URL:** https://forum.castle-engine.io/t/get-pixel-color-from-tcastleimagecontrol/1873
**Category:** 2D
**Created:** [April 22, 2025, 12:50pm UTC](https://forum.castle-engine.io/t/get-pixel-color-from-tcastleimagecontrol/1873 "2025-04-22T12:50:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Didi](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/didi/32/1908_2.png) [@Didi](https://forum.castle-engine.io/u/Didi)
#### Post date: [April 22, 2025, 12:50pm UTC](https://forum.castle-engine.io/t/get-pixel-color-from-tcastleimagecontrol/1873/1 "2025-04-22T12:50:31Z")

</div>

Hi,  
I want to read the color of one pixel from a TCastleImageControl. I found in the class TCastleImage this:  
_Assuming the above definition, [RawPixels](https://castle-engine.io/apidoc/html/CastleImages.TEncodedImage.html#RawPixels)ˆ[z, y, x] is color of pixel at position z, x, y_

I can’t find an example how to do it. Who can help?

many thanks in advance.

---

<div class="post-metadata">

### Author: ![edj](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/edj/32/1701_2.png) [@edj](https://forum.castle-engine.io/u/edj)
#### Post date: [April 22, 2025, 2:16pm UTC](https://forum.castle-engine.io/t/get-pixel-color-from-tcastleimagecontrol/1873/2 "2025-04-22T14:16:26Z")

</div>

Good to see your icon again.

I don’t think you want to use the RawPixels because then you need to know how the color is encoded. I think.TCastleImage.GetColors( x, y, z : integer ) : TCastleColor is what you would want to use?

---

<div class="post-metadata">

### Author: ![Didi](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/didi/32/1908_2.png) [@Didi](https://forum.castle-engine.io/u/Didi)
#### Post date: [April 22, 2025, 8:50pm UTC](https://forum.castle-engine.io/t/get-pixel-color-from-tcastleimagecontrol/1873/3 "2025-04-22T20:50:48Z")

</div>

Thanks a lot. Now it seems working.

---

<div class="post-metadata">

### Author: ![Noscow](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/noscow/32/964_2.png) [@Noscow](https://forum.castle-engine.io/u/Noscow)
#### Post date: [April 22, 2025, 9:13pm UTC](https://forum.castle-engine.io/t/get-pixel-color-from-tcastleimagecontrol/1873/4 "2025-04-22T21:13:12Z")

</div>

Maybe this theme can help you?

> [@TransformUnderMouse with Alpha pixels check for quad-based Transforms](https://forum.castle-engine.io/t/transformundermouse-with-alpha-pixels-check-for-quad-based-transforms/1494):
>
> Hi, All ! Happy new year! I want to make precise Mouse picking of quad based Transforms - like TCastleImageTransform, or TCastleScene with 2d sprite, in this case TransformUnderMouse performs hit-test over whole quad (geometry) and not taking into account transparent pixels of loaded textures. I found those similar topics : [Convenient way to determine exact point under mouse - #4 by michalis](https://forum.castle-engine.io/t/convenient-way-to-determine-exact-point-under-mouse/791/4) and [Get all geometry under raycast? - #5 by bugan11](https://forum.castle-engine.io/t/get-all-geometry-under-raycast/957/5) where some ideas presented , but no complete solutio…

---

<div class="post-metadata">

### Author: ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)
#### Post date: [April 23, 2025, 12:55pm UTC](https://forum.castle-engine.io/t/get-pixel-color-from-tcastleimagecontrol/1873/5 "2025-04-23T12:55:05Z")

</div>

Thank you all! I confirm that [TCastleImage.Colors](https://castle-engine.io/apidoc/html/CastleImages.TCastleImage.html#Colors), already mentioned above, would be my recommended solution too.

Note that you could also use `RawPixels` … but it would be more difficult. You would need to know the particular memory layout of your image descendant, which depends on the image type. E.g. for `TRGBImage` (RGB, no alpha), it’s a sequence of 3-byte values, and can be accessed like `TRGBImage(MyImage).RGBPixels[Y * ImageHeight + X]` or `TRGBImage(MyImage).PixelPtr(X, Y)^`. But… you likely don’t want to know all these details, it’s exactly why we created `TCastleImage.Colors` that just works with all image descendants and is simple 🙂

I just improved the docs a bit in [Improve TCastleImage.Colors docs · castle-engine/castle-engine@53c34e6 · GitHub](https://github.com/castle-engine/castle-engine/commit/53c34e6f5d655a8b8d5b3193eed78b7f4ee6cfd4) .

As mentioned, we also have [TAbstractTextureNode.Color](https://castle-engine.io/apidoc/html/X3DNodes.TAbstractTextureNode.html#Color-TVector2-) which is great for textures. It can do additional bilinear filtering and is generally better if you have `TAbstractTextureNode` instance – but this is not the case when you have `TCastleImageControl`.
