Color

class Color

Parent: Object | Class Index
Methods:blend, names, new, alpha, alpha=, blend, blue, blue=, green, green=, red, red=, to_a, to_i, to_s |
Sample Code:colortests.rb |

The Color class is used to create and manipulate colors within SketchUp models.

For methods that accept a Color object, such as the face.material method, you can pass in an actual Color object, or an object that can be converted to a Color. For example:

face.material = Sketchup::Color.new(255, 0, 0)
face.material = 255
face.material = 0xff
face.material = "red"
face.material = "#ff0000"
face.material = [1.0, 0.0, 0.0]
face.material = [255, 0, 0]

SketchUp ships with several built in colors in the Materials Browser. These colors are listed in the following table.

Name | RGB Values
AliceBlue | 240,248,255,
AntiqueWhite | 250,235,215
Aqua | 0,255,255
Aquamarine | 127,255,212
Azure | 240,255,255
Beige | 245,245,220
Bisque | 255,228,196
Black | 0,0,0
BlanchedAlmond | 255,235,205
Blue | 0,0,255
BlueViolet | 138,43,226
Brown | 165,42,42
BurlyWood | 222,184,135
CadetBlue | 95,158,160
Chartreuse | 127,255,0
Chocolate | 210,105,30
Coral | 255,127,80
CornflowerBlue | 100,149,237
Cornsilk | 255,248,220
Crimson | 220,20,60
Cyan | 0,255,255
DarkBlue | 0,0,139
DarkCyan | 0,139,139
DarkGoldenrod | 184,134,11
DarkGray | 169,169,169
DarkGreen | 0,100,0
DarkKhaki | 189,183,107
DarkMagenta | 139,0,139
DarkOliveGreen | 85,107,47
DarkOrange | 255,140,0
DarkOrchid | 153,50,204
DarkRed | 139,0,0
DarkSalmon | 233,150,122
DarkSeaGreen | 143,188,143
DarkSlateBlue | 72,61,139
DarkSlateGray | 47,79,79
DarkTurquoise | 0,206,209
DarkViolet | 148,0,211
DeepPink | 255,20,147
DeepSkyBlue | 0,191,255
DimGray | 105,105,105
DodgerBlue | 30,144,255
FireBrick | 178,34,34
FloralWhite | 255,250,240
ForestGreen | 34,139,34
Fuchsia | 255,0,255
Gainsboro | 220,220,220,
GhostWhite | 248,248,255
Gold | 255,215,0
Goldenrod | 218,165,32
Gray | 128,128,128
Green | 0,128,0
GreenYellow | 173,255,47
Honeydew | 240,255,240
HotPink | 255,105,180
IndianRed | 205,92,92
Indigo | 75,0,130
Ivory | 255,255,240
Khaki | 240,230,140
Lavender | 230,230,250
LavenderBlush | 255,240,245
LawnGreen | 124,252,0
LemonChiffon | 255,250,205
LightBlue | 173,216,230
LightCoral | 240,128,128
LightCyan | 224,255,255
LightGoldenrodYellow | 250,250,210
LightGreen | 144,238,144
LightGrey | 211,211,211
LightPink | 255,182,193
LightSalmon | 255,160,122
LightSeaGreen | 32,178,170
LightSkyBlue | 135,206,250
LightSlateGray | 119,136,153
LightSteelBlue | 176,196,222
LightYellow | 255,255,224
Lime | 0,255,0
LimeGreen | 50,205,50
Linen | 250,240,230
Magenta | 255,0,255
Maroon | 128,0,0
MediumAquamarine | 102,205,170
MediumBlue | 0,0,205
MediumOrchid | 186,85,211
MediumPurple | 147,112,219
MediumSeaGreen | 60,179,113
MediumSlateBlue | 123,104,238
MediumSpringGreen | 0,250,154
MediumTurquoise | 72,209,204
MediumVioletRed | 199,21,133
MidnightBlue | 25,25,112
MintCream | 245,255,250
MistyRose | 255,228,225
Moccasin | 255,228,181
NavajoWhite | 255,222,173
Navy | 0,0,128
OldLace | 253,245,230
Olive | 128,128,0
OliveDrab | 107,142,35
Orange | 255,165,0
OrangeRed | 255,69,0
Orchid | 218,112,214
PaleGoldenrod | 238,232,170
PaleGreen | 152,251,152
PaleTurquoise | 175,238,238
PaleVioletRed | 219,112,147
PapayaWhip | 255,239,213
PeachPuff | 255,218,185
Peru | 205,133,63
Pink | 255,192,203
Plum | 221,160,221
PowderBlue | 176,224,230
Purple | 128,0,128
Red | 255,0,0
RosyBrown | 188,143,143
RoyalBlue | 65,105,225
SaddleBrown | 139,69,19
Salmon | 250,128,114
SandyBrown | 244,164,96
SeaGreen | 46,139,87
Seashell | 255,245,238
Sienna | 160,82,45
Silver | 192,192,192
SkyBlue | 135,206,235
SlateBlue | 106,90,205
SlateGray | 112,128,144
Snow | 255,250,250
SpringGreen | 0,255,127
SteelBlue | 70,130,180
Tan | 210,180,140
Teal | 0,128,128
Thistle | 216,191,216
Tomato | 255,99,71
Turquoise | 64,224,208
Violet | 238,130,238
Wheat | 245,222,179
White | 255,255,255
WhiteSmoke | 245,245,245
Yellow | 255,255,0
YellowGreen | 154,205,50

Class Methods

blend
The blend method is used to blend two colors into one.
Syntax:
color3 = Sketchup::Color.blend color1, color2, weight
Arguments:
color1 - first color to be blended color2 - second color to be blended weight - a value between 0 and 1 that controls how much of each color to use for blending.
Return Value:
color3 - the new Color object if successful
Comments:
The blended color will be the result of taking (1 - weight)*color1 + weight * color2. For example, a weight of .25 will result in a color where .25 is derived from color1 and .75 from color2. If weight = 0, you will get color2. If weight = 1 you will get color1.
Example:

color1 = Sketchup::Color.new "OldLace"
color2 = Sketchup::Color.new "AliceBlue"

begin
color = Sketchup::Color.blend color1, color2, 1
rescue
UI.messagebox $!.message
end

if (color)
UI.messagebox color
else
UI.messagebox "Failure"
end

names
The names method is used to retrieve an array of all color names recognized by SketchUp.
Syntax:
names = Geom::Color.names
Arguments:
Return Value:
names - an array of all SketchUp color names if successful
Comments:
In general, whenever a method wants a color, you can pass in a String with one of these names.
Example:

array = Sketchup::Color.names

if (array)
UI.messagebox array[0]
else
UI.messagebox "Failure"
end

new
The new method is used to create a new Color object.
Syntax:
color = Sketchup::Color.new red, green, blue color = SketchUp::Color.new "name"
Arguments:
red - a red value green - a green value blue - a blue value "name" - the name of a color that currently exists in SketchUp. See the table at the start of this class description for more information.
Return Value:
color - a Color object if successful
Comments:
Example:

color = Sketchup::Color.new "OldLace"

if (color)
UI.messagebox color
else
UI.messagebox "Failure"
end

Instance Methods

alpha
The alpha method is used to retrieve the opacity of the color.
Syntax:
alpha = color.alpha
Arguments:
Return Value:
alpha - the opacity of the color if successful
Comments:
Example:

color = Sketchup::Color.new "OldLace"
alpha = color.alpha

if (alpha)
UI.messagebox alpha
else
UI.messagebox "Failure"
end

alpha=
The alpha= method is used to set the opacity of the color.
Syntax:
alpha = color.alpha = alpha
Arguments:
alpha - the new opacity value
Return Value:
alpha - the newly set opacity value if successful
Comments:
0 is transparent, 255 is opaque.
Example:

color = Sketchup::Color.new "AliceBlue"
alpha = color.alpha=255

if (alpha)
UI.messagebox alpha
else
UI.messagebox "Failure"
end

blend
The blend method is used to blend two colors.
Syntax:
color3 = color1.blend color2, weight
Arguments:
color2 - the second color to be blended (with color1) weight - ?
Return Value:
color3 - the newly blended Color object if successful
Comments:
The blended color will be the result of taking (1 - weight)*color1 + weight * color2. If weight = 0, you will get color2. If weight = 1 you will get color1.
Example:

color1 = Sketchup::Color.new "OldLace"
color2 = Sketchup::Color.new "AliceBlue"
color3 = color1.blend color2, 10

UI.messagebox $!.message
if (color3)
UI.messagebox color3
else
UI.messagebox "Failure"
end

blue
The blue method is used to retrieve the blue value of a color. Colors are comprised of red, green, and blue values.
Syntax:
blue = color.blue
Arguments:
Return Value:
blue - the blue value for the color if successful
Comments:
Value range is 0 to 255.
Example:

color = Sketchup::Color.new "AliceBlue"
setting = color.blue

if (setting)
UI.messagebox setting
else
UI.messagebox "Failure"
end

blue=
The blue= method is used to set the blue value of a color. Colors are comprised of red, green, and blue values.
Syntax:
blue = color.blue = blue
Arguments:
blue - the blue value for the color
Return Value:
blue - the new blue value for the color if successful
Comments:
Value range is 0 to 255.
Example:

color = Sketchup::Color.new "AliceBlue"
setting = color.blue=200
if (setting)
UI.messagebox setting
else
UI.messagebox "Failure"
end

green
The green method is used to retrieve the green value of a color. Colors are comprised of red, green, and blue values.
Syntax:
green = color.green
Arguments:
Return Value:
green - the green value for the color if successful
Comments:
Value range is 0 to 255.
Example:

color = Sketchup::Color.new "AliceBlue"
setting = color.green
if (setting)
UI.messagebox setting
else
UI.messagebox "Failure"
end

green=
The green= method is used to set the green component of a RGB Color.
Syntax:
green = color.green = green
Arguments:
green - the green value for the color
Return Value:
green - the new green value for the color if successful
Comments:
Value range is 0 to 255.
Example:

color = Sketchup::Color.new "AliceBlue"
setting = color.green=200
if (setting)
UI.messagebox setting
else
UI.messagebox "Failure"
end

red
The red method is used to retrieve the red component of a RGB Color.
Syntax:
red = color.red
Arguments:
Return Value:
red - the red value for the color if successful
Comments:
Value range is 0 to 255.
Example:

color = Sketchup::Color.new "AliceBlue"
setting = color.red
if (setting)
UI.messagebox setting
else
UI.messagebox "Failure"
end

red=
The red= method is used to set the red component of a RGB Color.
Syntax:
red = color.red = red
Arguments:
red - the red value for the color
Return Value:
red - the new red value for the color if successful
Comments:
Value range is 0 to 255.
Example:

color = Sketchup::Color.new "AliceBlue"
setting = color.red=200

if (setting)
UI.messagebox setting
else
UI.messagebox "Failure"
end

to_a
The to_a method is used to convert a Color object to an Array object.
Syntax:
array = color.to_a
Arguments:
Return Value:
array - an Array object if successful
Comments:
Example:

color = Sketchup::Color.new "AliceBlue"
ascii = color.to_a
if (ascii)
UI.messagebox ascii
else
UI.messagebox "Failure"
end

to_i
The to_i method is used to convert a Color object to an 32 bit integer.
Syntax:
integer = color.to_i
Arguments:
Return Value:
integer - a 32 bit integer if successful
Comments:
Example:

color = Sketchup::Color.new "AliceBlue"
integer = color.to_i

if (integer)
UI.messagebox integer
else
UI.messagebox "Failure"
end

to_s
The to_s method is used to convert a Color object to a string.
Syntax:
string = color.to_s
Arguments:
Return Value:
string - a string representation of the Color object if successful
Comments:
Example:

color = Sketchup::Color.new "AliceBlue"
str = color.to_s

if (str)
UI.messagebox str
else
UI.messagebox "Failure"
end