# Minimal Wx\_Colour bug?

**URL:** <https://www.ruby-forum.com/t/minimal-wx-colour-bug/180124>\
**Category:** wxRuby\
**Created:** [December 12, 2009, 5:02am UTC](https://www.ruby-forum.com/t/minimal-wx-colour-bug/180124 "2009-12-12T05:02:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pnomolos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/85f322/32.png) [@pnomolos](https://www.ruby-forum.com/u/pnomolos)\
**Post date:** [December 12, 2009, 5:02am UTC](https://www.ruby-forum.com/t/minimal-wx-colour-bug/180124/1 "2009-12-12T05:02:02Z")

</div>

require ‘wx’

# col\_sample4.rb

BLACK = Wx::Colour.new(0,0,0)  
RED = Wx::Colour.new(128,0,0)

@background\_colour = BLACK  
@symbol\_colour = BLACK  
temp\_colour = BLACK  
temp2\_colour = RED

@symbol\_colour = Wx::Colour.set(1, 0, 0)

puts “@background\_colour : #{@background\_colour}”  
puts “@symbol\_colour : #{@symbol\_colour}”  
puts “temp\_colour : #{temp\_colour}”  
puts “temp2\_colour : #{temp2\_colour}”

=begin

Hypothetical Values:

@background\_colour : #\<Wx::Colour: (0, 0, 0 \*255)\>  
@symbol\_colour : #\<Wx::Colour: (1, 0, 0 \*255)\>  
temp\_colour : #\<Wx::Colour: (0, 0, 0 \*255)\>  
temp2\_colour : #\<Wx::Colour: (128, 0, 0 \*255)\>

Actual Values:

@background\_colour : #\<Wx::Colour: (1, 0, 0 \*255)\>  
@symbol\_colour : #\<Wx::Colour: (1, 0, 0 \*255)\>  
temp\_colour : #\<Wx::Colour: (1, 0, 0 \*255)\>  
temp2\_colour : #\<Wx::Colour: (128, 0, 0 \*255)\>

What’s going on here and how do I correct this problem?

=end

---

<div class="post-metadata">

**Author:** ![pnomolos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/85f322/32.png) [@pnomolos](https://www.ruby-forum.com/u/pnomolos)\
**Post date:** [December 12, 2009, 5:40am UTC](https://www.ruby-forum.com/t/minimal-wx-colour-bug/180124/2 "2009-12-12T05:40:04Z")

</div>

The fix:

@symbol\_colour = Wx::Colour.new(1, 0, 0)

instead of

@symbol\_colour.set(1, 0, 0)

works but it doesn’t it waste resources? Does someone have a better  
soloution?

Thanks.

---

<div class="post-metadata">

**Author:** ![pnomolos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/85f322/32.png) [@pnomolos](https://www.ruby-forum.com/u/pnomolos)\
**Post date:** [December 13, 2009, 4:41pm UTC](https://www.ruby-forum.com/t/minimal-wx-colour-bug/180124/3 "2009-12-13T16:41:02Z")

</div>

Hi Philip

What’s happening is that you have multiple variables referring to the  
same object. When you change the object, all references change:

Philip S. wrote:

> # col\_sample4.rb
> 
> BLACK = Wx::Colour.new(0,0,0)

A new object is created; the constant BLACK refers to this object

> @background\_colour = BLACK

The instance variable @background\_colour now also refers to this same  
object.

> @symbol\_colour = BLACK

Now so does the variable @symbol\_colour

> @symbol\_colour = Wx::Colour.set(1, 0, 0)

Change the colour intensities of the (single) colour object. So all  
references to it will reflect the change.

If you want to create an independent copy of a colour from a colour, use  
the constructor:

my\_black = Wx::Colour.new(Wx::BLACK)  
Wx::BLACK == my\_black # true  
my\_black.set(127, 127, 127)  
Wx::BLACK == my\_black # false

Don’t worry too much about resource control with colours, they will get  
cleaned up automatically when they fall out of scope, and they are more  
light-weight objects than say Pens and Brushes. Sometimes I find it  
useful to declare a palette as a set of constants in a class.

class MyDrawingThing  
SYMBOL\_COLOUR = Wx::Colour.new(127,127,127)  
HIGHLIGHT\_COLOUR = Wx::Colour.new(Wx::RED)

etc

hth  
a
