# Access to Graphics on Pixel Level

**URL:** <https://forum.juce.com/t/access-to-graphics-on-pixel-level/16039>\
**Category:** General JUCE discussion\
**Created:** [November 23, 2015, 8:25pm UTC](https://forum.juce.com/t/access-to-graphics-on-pixel-level/16039 "2015-11-23T20:25:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![WilliamR](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/williamr/32/72_2.png) [@WilliamR](https://forum.juce.com/u/WilliamR)\
**Post date:** [November 23, 2015, 8:25pm UTC](https://forum.juce.com/t/access-to-graphics-on-pixel-level/16039/1 "2015-11-23T20:25:32Z")

</div>

Hi,&nbsp;  
  
it there a way to get access to specific pixels? In short: I need to set the colour of specific pixels to have a high contrast to the background, and I don't know what the background is and who draw it.&nbsp;  
  
I would guess that I could somehow make changes to the juce parts so I can reimplement the way how my class draws. In this case I would just draw to an image and then get pixels from that image (I guess that's possible). But that's really not a nice way to do it...

&nbsp;

Cheers!

---

<div class="post-metadata">

**Author:** ![gammer](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@gammer](https://forum.juce.com/u/gammer)\
**Post date:** [November 23, 2015, 11:00pm UTC](https://forum.juce.com/t/access-to-graphics-on-pixel-level/16039/2 "2015-11-23T23:00:40Z")

</div>

Hello!  
  
It's quite easy: you should inherit your class from **Component** and then reimplement **paint** method from **Component**.

Something like this:

```
class MyComponent : public Component
{
    MyComponent()
    {
      // do some stuff
    }

    // To specify how your class should draw itself, reimplement this method
    void paint (Graphics& g)
    {
         // Here you have access to Graphics object, which allows you to draw anything you want.
         
         // set colour for brush and fill background
         g.setColour (Colours::black);
         g.fillAll(); 

         // Set colour for brush and colour specific pixel
         g.setColour (Colours::green);
         g.setPixel (0, 3); // fill pixel (0, 3)
         g.setPixel (5, 8); // fill pixel (5, 8)

         // But anyway you also have a capability to draw any rectangle, ellipse, etc.
         g.setColour (Colours::blue);
         // draws the rectangle with topleft corner at (10, 20), 50px width, 30px height.
         g.fillRect (10, 20, 50, 30);
    }
};
```

&nbsp;

So you have a huge capabilities. To learn it more, you should open http://learn.juce.com/doc/classGraphics.php or take a look at some JUCE examples (e.g.&nbsp; JUCE/examples/Demo )

---

<div class="post-metadata">

**Author:** ![WilliamR](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/williamr/32/72_2.png) [@WilliamR](https://forum.juce.com/u/WilliamR)\
**Post date:** [November 26, 2015, 2:26pm UTC](https://forum.juce.com/t/access-to-graphics-on-pixel-level/16039/3 "2015-11-26T14:26:07Z")

</div>

Hey,&nbsp;  
Sorry for my late response, was bussy the last days. ;)&nbsp;  
I actually did rtfm this time and I know that I can set Pixels. However, I have a situation like this:  
  
I own a component A which itself contains a bunch of unknown components B. I don't know where instances of B are in my component and I don't know what they paint. In A, I want to reimplement paintOverChildren (or how it's called) . I need to have access to some pixels, reading them so I can chose a color with high contrast to the background.&nbsp;  
  
Essentially&nbsp;, I need a "getPixel" ;)  
Regarding performance: I would need to check ~4 \* 6 Pixels at max, maybe even less.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 26, 2015, 2:51pm UTC](https://forum.juce.com/t/access-to-graphics-on-pixel-level/16039/4 "2015-11-26T14:51:54Z")

</div>

The juce rendering engines, like pretty much all other UI&nbsp;toolkits, never&nbsp;give you access to underlying pixels.

Think about it: the graphics context could be drawing onto&nbsp;_anything_&nbsp;- it could be a vector device, a printer,&nbsp;an alpha-channel image, a GPU-based texture, etc. If it's using CoreGraphics then that API doesn't provide pixel access.&nbsp;If the component you're drawing has a transparency, then the paint method will draw onto&nbsp;a hidden temporary image which is later composited onto the background, so those pixels that you want may not even exist at the time you want them!

This is never really a problem if you approach things sensibly. Maybe try explaining what you're really trying to do, rather than explaining how you expect to do it?

---

<div class="post-metadata">

**Author:** ![WilliamR](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/williamr/32/72_2.png) [@WilliamR](https://forum.juce.com/u/WilliamR)\
**Post date:** [November 26, 2015, 3:16pm UTC](https://forum.juce.com/t/access-to-graphics-on-pixel-level/16039/5 "2015-11-26T15:16:01Z")

</div>

Argh, accidentally closed the tab. :'D&nbsp;  
  
I want to put a GUI editor in the GUI. So the user can add / move / resize etc. components like dials, sliders, labels and so on. I would like to use&nbsp;the basic handles you see in most graphics applications (see attachment). &nbsp;Most applications colour these handles so that they are clearyl visible, i.e. use complementary colours to the background. That's what I would like to do as well.  
  
I thought the easiest way would be to let the children paint whatever they want and then in paintOverChildren draw handles and so on. I guess one solution (if possible, currently&nbsp;no time to check ) would be to change the behaviour of my A class (container) to not forward Graphics &g directly but to generate a new Image, let the child-classes paint on that end then copy over the image.&nbsp;  
  
Cheers!

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 26, 2015, 3:35pm UTC](https://forum.juce.com/t/access-to-graphics-on-pixel-level/16039/6 "2015-11-26T15:35:13Z")

</div>

Sounds like you're making things hard for yourself..&nbsp;Just draw the highlight with a contrasting border colour - that's what everyone does these days.

---

<div class="post-metadata">

**Author:** ![WilliamR](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/williamr/32/72_2.png) [@WilliamR](https://forum.juce.com/u/WilliamR)\
**Post date:** [November 26, 2015, 4:50pm UTC](https://forum.juce.com/t/access-to-graphics-on-pixel-level/16039/7 "2015-11-26T16:50:11Z")

</div>

I just checked some Adobe applications, I thought they did the same thing with complementary colors but they indeed just use bordered squraes. &nbsp;Shame on me! :'D&nbsp;  
  
But then again, when it comes to UI design, it's more about what works best for the user, not what is easy or hard to implement. ;)&nbsp;  
The only option right now seems to be to paint to an image and then use that image in the paint callback,&nbsp;correct?  
  
Cheers!

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 26, 2015, 5:06pm UTC](https://forum.juce.com/t/access-to-graphics-on-pixel-level/16039/8 "2015-11-26T17:06:36Z")

</div>

Yeah, every app since the 1990s has just drawn borders.&nbsp;IMHO it'd be silly to mess around with images just to do something like that.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [November 26, 2015, 7:54pm UTC](https://forum.juce.com/t/access-to-graphics-on-pixel-level/16039/9 "2015-11-26T19:54:32Z")

</div>

I haven't tried it, but what about clipping everything except for the pixel you are interested in and paint? I remember in the early times of openGL I used this for a hit test...
