📺 "One project. One language. Three apps." Learn more in our upcoming code sharing webinar!

NativeScript Core

Color

Represents a color object. Stores all color components (alpha (opacity), red, green, blue) in a [0..255] range.

Basics

Using Color class requires the tns-core-modules/color module.

Creating a Color from a hex value.

// Creates the red color
const color = new Color("#FF0000");
// Creates the red color
const color = new Color("#FF0000");

Creating a Color from a short hex value.

// Creates the color #FF8800
const color = new Color("#F80");
// Creates the color #FF8800
const color = new Color("#F80");

Creating a Color from four ARGB values.

// Creates the color with 100 alpha, 255 red, 100 green, 100 blue
const color = new Color(100, 255, 100, 100);
// Creates the color with 100 alpha, 255 red, 100 green, 100 blue
const color = new Color(100, 255, 100, 100);

Creating a Color from a single ARGB value.

// Creates the color with 100 alpha, 100 red, 100 green, 100 blue
const color = new Color(0x64646464);
// Creates the color with 100 alpha, 100 red, 100 green, 100 blue
const color = new Color(0x64646464);

Creating a Color from a known name. Full list of the known color names can be found here.

Comparing two colors for equality.

const red = new Color("#FF0000");
const isRed = red.equals(new Color("red"));
console.log("Are both colors identical: ", isRed);
const red = new Color("#FF0000");
const isRed = red.equals(new Color("red"));
console.log("Are both colors identical: ", isRed);

Improve this document

Demo Source


API Reference for the Color Class

Native Component

Android iOS
android.graphics.Color UIColor