# Gdk::Pixbuf - red pixel?

**URL:** <https://www.ruby-forum.com/t/gdk-pixbuf-red-pixel/115283>\
**Category:** Ruby-Gnome 2\
**Created:** [September 23, 2007, 3:21am UTC](https://www.ruby-forum.com/t/gdk-pixbuf-red-pixel/115283 "2007-09-23T03:21:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [September 23, 2007, 3:21am UTC](https://www.ruby-forum.com/t/gdk-pixbuf-red-pixel/115283/1 "2007-09-23T03:21:18Z")

</div>

Hi,

is it possible to use a Gdk::Pixbuf which consists of i.e 10x10 pixels  
of a red colour?  
Inside DrawingArea this is easy, but I would like to use simple Pixbufs  
which have different colours

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [September 24, 2007, 11:29am UTC](https://www.ruby-forum.com/t/gdk-pixbuf-red-pixel/115283/2 "2007-09-24T11:29:42Z")

</div>

Marc H. wrote:

> Hi,
> 
> is it possible to use a Gdk::Pixbuf which consists of i.e 10x10 pixels  
> of a red colour?  
> Inside DrawingArea this is easy, but I would like to use simple Pixbufs  
> which have different colours

Try:  
pb = Gdk::Pixbuf.new(Gdk::Pixbuf::COLORSPACE\_RGB, false, 8, 10, 10)  
pb.fill!(0xff0000ff)

Just in case it interests anyone, here’s an example that uses basic  
Gdk::Pixbuf functions to draw a red rectangle around a specified region  
of the image & reduce the saturation of the surrounding image:

—8\<—  
require ‘gdk\_pixbuf2’

Rect = Struct.new(“Rect”, :x, :y, :w, :h)

file = ARGV[0] ||  
‘/home/geoff/Desktop/iStock/iStock\_000001958797XSmall.jpg’

pb = Gdk::Pixbuf.new(file)

# Sample crop area

crop = Rect.new(pb.width / 4, pb.height / 4, pb.width / 2, pb.height /  
2)

# Make pixbuf red (for border)

red\_pb = pb.dup  
red\_pb.fill!(0xff0000ff)

# Make greyed out/pixelated pixbuf for background

grey\_pb = pb.saturate\_and\_pixelate(0.2, true)

# Box for border

box = Rect.new(crop.x - 2, crop.y - 2, crop.w + 4, crop.h + 4)

# Ensure that border doesn’t overflow image

box.x = [box.x,0].max  
box.y = [box.y,0].max  
box.w = [box.w, red\_pb.width - box.x].min  
box.h = [box.h, red\_pb.height - box.y].min

# Use red pixbuf as border background

red\_pb.copy\_area(box.x, box.y, box.w, box.h, grey\_pb, box.x, box.y)

# Copy pristine image into the crop area

pb.copy\_area(crop.x, crop.y, crop.w, crop.h, grey\_pb, crop.x, crop.y)

pb = grey\_pb

pb.save(file + ‘.out.jpeg’, ‘jpeg’, :quality =\> 80)

—8\<—

* * *

This [SF.net](http://SF.net) email is sponsored by: Microsoft  
Defy all challenges. Microsoft(R) Visual Studio 2005.  
[http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/](http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/)

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [September 24, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/gdk-pixbuf-red-pixel/115283/3 "2007-09-24T17:40:41Z")

</div>

Whoa, cool stuff … in fact looks like a “filter” for a  
dungeon-like map on some images, reminds me of old indiana  
jones game in a dark maze 🙂

And thanks by the way!
