User Tools

Site Tools


en:code:core:color

Color

This object represents a color.

Functions

Color

Constructor function.

Syntax

var myObject = new Color();
var myObject = new Color(colorName);
var myObject = new Color(otherColor);
var myObject = new Color(red, green, blue);
var myObject = new Color(red, green, blue, alpha);

Arguments

  1. colorName - (string) name of a color
  2. otherColor - (Color) other Color to copy
  3. red - (integer) red value (0-255)
  4. green - (integer) green value (0-255)
  5. blue - (integer) blue value (0-255)
  6. alpha - (integer) alpha value (0-255)

Exceptions

  • (ColorNameError) incorrect color name
  • (ParameterTypeError) incorrect parameter type
  • (ParameterCountError) incorrect parameter count

Example

Create a black Color.

var myObject = new Color();

Create a named color. See http://www.w3.org/TR/SVG/types.html#ColorKeywords for the complete list.

var myObject = new Color("red");

Create a color from an hexadecimal value.

var myObject = new Color("#FE5");
var myObject = new Color("#FF5577");

Create a Color representing a copy of otherColor.

var myObject = new Color(otherColor);

Create a gray Color.

var myObject = new Color(50, 50, 50);

Create a half-transparent gray Color.

var myObject = new Color(50, 50, 50, 128);

Methods

clone

Returns a copy of this Color.

Syntax

myObject.clone();

Returns

  • (Color) a copy of this Color

Example

var copyOfMyObject = myObject.clone();

equals

Returns true if this Color and another are referencing the same color.

Syntax

myObject.equals(other);

Arguments

  1. other - (Color) another Color

Returns

  • (bool) true if other is referencing the same color

Example

if(myFirstObject.equals(mySecondObject))
	//Do something

toString

Returns a string representing this Color.

Syntax

myObject.toString();

Returns

  • (string) a string representing this Color

Example

Console.print(myObject.toString());

Notes

This method is automatically called when trying to convert a Color to a string.

setRed

Set the red component of this Color.

Syntax

myObject.setRed(red);

Returns

  • (Color) this Color

Example

myObject.setRed(128);

setGreen

Set the green component of this Color.

Syntax

myObject.setGreen(green);

Returns

  • (Color) this Color

Example

myObject.setGreen(128);

setBlue

Set the blue component of this Color.

Syntax

myObject.setBlue(blue);

Returns

  • (Color) this Color

Example

myObject.setBlue(128);

setAlpha

Set the alpha component of this Color.

Syntax

myObject.setAlpha(alpha);

Returns

  • (Color) this Color

Example

myObject.setAlpha(128);

setCmyk

Sets this Color to CMYK values.

Syntax

myObject.setCmyk(cyan, magenta, yellow, black);
myObject.setCmyk(cyan, magenta, yellow, black, alpha);

Returns

  • (Color) this Color

Example

myObject.setCmyk(128, 60, 60, 100);
myObject.setCmyk(128, 60, 60, 128);

setHsl

Sets this Color to HSL values.

Syntax

myObject.setHsl(hue, saturation, lightness);
myObject.setHsl(hue, saturation, lightness, alpha);

Returns

  • (Color) this Color

Example

myObject.setHsl(50, 100, 100);
myObject.setHsl(50, 100, 100, 128);

setHsv

Sets this Color to HSV values.

Syntax

myObject.setHsv(hue, saturation, value);
myObject.setHsv(hue, saturation, value, alpha);

Returns

  • (Color) this Color

Example

myObject.setHsv(25, 50, 150);
myObject.setHsv(25, 50, 150, 100);

setNamedColor

Sets this Color to a named value.

Syntax

myObject.setNamedColor(name);

Returns

  • (Color) this Color

Exceptions

  • (ColorNameError) incorrect color name

Example

Set a named color. See http://www.w3.org/TR/SVG/types.html#ColorKeywords for the complete list.

myObject.setNamedColor("red");

Set a color from an hexadecimal value.

myObject.setNamedColor("#FE5");
myObject.setNamedColor("#FF5577");

lighten

Lighten the color by a factor.

Syntax

Ligthen by 50%.

myObject.lighten();

Ligthen by a factor. (percent)

myObject.lighten(factor);

Returns

  • (Color) this Color

Example

myObject.lighten();
myObject.lighten(175); //Lighten by 75%

darken

Darken the color by a factor.

Syntax

Darken by 50%.

myObject.darken();

Darken by a factor. (percent)

myObject.darken(factor);

Returns

  • (Color) this Color

Example

myObject.darken();
myObject.darken(175); //Darken by 75%

red

Returns the red component of this Color.

Syntax

myObject.red();

Returns

  • (int) the red component value (0-255)

Example

var myValue = myObject.red();

green

Returns the green component of this Color.

Syntax

myObject.green();

Returns

  • (int) the green component value (0-255)

Example

var myValue = myObject.green();

blue

Returns the blue component of this Color.

Syntax

myObject.blue();

Returns

  • (int) the blue component value (0-255)

Example

var myValue = myObject.blue();

alpha

Returns the alpha component of this Color.

Syntax

myObject.alpha();

Returns

  • (int) the alpha component value (0-255)

Example

var myValue = myObject.alpha();

cyan

Returns the cyan component of this Color.

Syntax

myObject.cyan();

Returns

  • (int) the cyan component value (0-255)

Example

var myValue = myObject.cyan();

magenta

Returns the magenta component of this Color.

Syntax

myObject.magenta();

Returns

  • (int) the magenta component value (0-255)

Example

var myValue = myObject.magenta();

yellow

Returns the yellow component of this Color.

Syntax

myObject.yellow();

Returns

  • (int) the yellow component value (0-255)

Example

var myValue = myObject.yellow();

black

Returns the black component of this Color.

Syntax

myObject.black();

Returns

  • (int) the black component value (0-255)

Example

var myValue = myObject.black();

hue

Returns the hue component of this Color.

Syntax

myObject.hue();

Returns

  • (int) the hue component value (0-255)

Example

var myValue = myObject.hue();

saturation

Returns the saturation component of this Color.

Syntax

myObject.saturation();

Returns

  • (int) the saturation component value (0-255)

Example

var myValue = myObject.saturation();

lightness

Returns the lightness component of this Color.

Syntax

myObject.lightness();

Returns

  • (int) the lightness component value (0-255)

Example

var myValue = myObject.lightness();

name

Returns the name of this Color in the format "#RRGGBB".

Syntax

myObject.name();

Returns

  • (int) the name of this Color in the format "#RRGGBB"

Example

var myValue = myObject.name();

Attributes

red

The red component of this Color. (integer)

Example

myObject.red = 50;
Console.print(myObject.red);

green

The green component of this Color. (integer)

Example

myObject.green = 50;
Console.print(myObject.green);

blue

The blue component of this Color. (integer)

Example

myObject.blue = 50;
Console.print(myObject.blue);

alpha

The alpha component of this Color. (integer)

Example

myObject.alpha = 50;
Console.print(myObject.alpha);
en/code/core/color.txt · Last modified: 2021/02/13 11:23 by 127.0.0.1