This example demonstrates using the Color
class to create some basic colors. This simulates setting HTML element properties.
Follow these steps to create a new project workspace and install the viridis dependency to run this example.
# Create and open project folder
mkdir Colors_demo
cd Colors_demo
# Initialize project and install dependencies
npm init -y
npm i viridis@1.1.7
# Create and open source file
touch "Colors.mjs"
open "Colors.mjs"
Copy and paste this source code into Colors.mjs
.
import { Color } from 'viridis';
// getContrastingColor() will always return black or
// white, depending on which one has better contrast.
const background = new Color(255, 0, 0),
foreground = background.getContrastingColor(),
border = Color.from('#00beef');
// Automatically calls the toString() member function
console.log('Background: ' + background);
console.log('Foreground: ' + foreground);
console.log('Border color: ' + border);
In Colors_demo/
, execute Colors.mjs
with NodeJS to generate an output.
node "Colors.mjs"
You should expect to see an output similar to the one below.
Background: rgba(255,0,0,100%)
Foreground: rgba(255,255,255,100%)
Border color: rgba(0,190,239,100%)