# The display.colorSample() function does not work correctly

**URL:** <https://forums.solar2d.com/t/the-display-colorsample-function-does-not-work-correctly/356365>\
**Category:** Other\
**Created:** [March 4, 2024, 5:55pm UTC](https://forums.solar2d.com/t/the-display-colorsample-function-does-not-work-correctly/356365 "2024-03-04T17:55:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![C2G](https://forums.solar2d.com/user_avatar/forums.solar2d.com/c2g/32/3908_2.png) [@C2G](https://forums.solar2d.com/u/C2G)\
**Post date:** [March 4, 2024, 5:55pm UTC](https://forums.solar2d.com/t/the-display-colorsample-function-does-not-work-correctly/356365/1 "2024-03-04T17:55:46Z")

</div>

Hello!

The display.colorSample() function does not work correctly.

More details:

```auto
JSON = require('json')
CX, CY = display.contentCenterX, display.contentCenterY

local rect = display.newRect(CX, CY, 100, 100)

timer.performWithDelay(1000, function()
    
    display.colorSample(CX, CY,
    function(event)

        local r, g, b = event.r, event.g, event.b
        r, g, b = tonumber(r)*255, tonumber(g)*255, tonumber(b)*255

        print(r, g, b, JSON.encode(event))

    end)

end, 1)

```

Output: 0 0 0 {“y”:512,“x”:384,“name”:“colorSample”,“r”:0,“a”:0,“g”:0,“b”:0}

But I assumed that there would be 1 1 1 {“y”:512,“x”:384,“name”:“colorSample”,“r”:1,“a”:1,“g”:1"b":1}, since the rect object is still white color

---

<div class="post-metadata">

**Author:** ![Siu](https://forums.solar2d.com/user_avatar/forums.solar2d.com/siu/32/2129_2.png) [@Siu](https://forums.solar2d.com/u/Siu)\
**Post date:** [March 4, 2024, 11:24pm UTC](https://forums.solar2d.com/t/the-display-colorsample-function-does-not-work-correctly/356365/2 "2024-03-04T23:24:14Z")

</div>

Your code, in isolation as posted, is working for me as expected.

Have you test it on an empty project?

---

<div class="post-metadata">

**Author:** ![nkp.bro](https://forums.solar2d.com/user_avatar/forums.solar2d.com/nkp.bro/32/1871_2.png) [@nkp.bro](https://forums.solar2d.com/u/nkp.bro)\
**Post date:** [March 5, 2024, 7:27am UTC](https://forums.solar2d.com/t/the-display-colorsample-function-does-not-work-correctly/356365/3 "2024-03-05T07:27:49Z")

</div>

@C2G  
Your code is working for me as expected.  
I tested with **Solar2D Simulator 2024.3704**

 ![result](https://forums.solar2d.com/uploads/default/original/2X/0/080d6a33628cb5bd8a6d73f3d41a99eaaab489cd.png)

---

<div class="post-metadata">

**Author:** ![C2G](https://forums.solar2d.com/user_avatar/forums.solar2d.com/c2g/32/3908_2.png) [@C2G](https://forums.solar2d.com/u/C2G)\
**Post date:** [March 5, 2024, 12:43pm UTC](https://forums.solar2d.com/t/the-display-colorsample-function-does-not-work-correctly/356365/4 "2024-03-05T12:43:35Z")

</div>

No, project not empty. But I don’t understand what this could be related to…

---

<div class="post-metadata">

**Author:** ![C2G](https://forums.solar2d.com/user_avatar/forums.solar2d.com/c2g/32/3908_2.png) [@C2G](https://forums.solar2d.com/u/C2G)\
**Post date:** [March 5, 2024, 12:45pm UTC](https://forums.solar2d.com/t/the-display-colorsample-function-does-not-work-correctly/356365/5 "2024-03-05T12:45:58Z")

</div>

I have already tested the code in an empty project, but the problem remains the same

---

<div class="post-metadata">

**Author:** ![C2G](https://forums.solar2d.com/user_avatar/forums.solar2d.com/c2g/32/3908_2.png) [@C2G](https://forums.solar2d.com/u/C2G)\
**Post date:** [March 5, 2024, 12:58pm UTC](https://forums.solar2d.com/t/the-display-colorsample-function-does-not-work-correctly/356365/6 "2024-03-05T12:58:36Z")

</div>

I have updated Solar 2D to the latest version and everything works, but when building in .apk it gives an error. Well, that’s a completely different story…

---

<div class="post-metadata">

**Author:** ![system](https://forums.solar2d.com/uploads/default/original/1X/1bf6a947166df24e92aaa3358558f4b82d7f3d5a.png) [@system](https://forums.solar2d.com/u/system)\
**Post date:** [September 1, 2024, 12:59pm UTC](https://forums.solar2d.com/t/the-display-colorsample-function-does-not-work-correctly/356365/7 "2024-09-01T12:59:13Z")

</div>

Closed on September 1, 2024, 12:59pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
