How to create a figma plugin to check contrast

Achuth hadnoor
3 min readJan 4, 2023

Figma is a popular design tool that allows users to create, prototype, and collaborate on designs. One of the features of Figma is the ability to create plugins, which are small pieces of code that can extend the functionality of Figma. In this blog post, we will walk through the process of creating a Figma plugin that compares the color contrast of two colors.

Prerequisites

Before getting started, you will need to have a basic understanding of JavaScript and have a Figma account. You will also need to install the Figma Desktop app, which can be downloaded from the Figma website.

Step 1: Set up your development environment

The first step in creating a Figma plugin is to set up your development environment. This involves creating a new Figma file and installing the Figma Plugin CLI (Command Line Interface).

To create a new Figma file, log in to your Figma account and click the “New File” button. Select “Plugin” from the list of templates and click “Create”. This will create a new file with the necessary scaffolding for a Figma plugin.

Next, install the Figma Plugin CLI by running the following command in your terminal:shells

npm install -g figma-plugin-cli

Step 2: Write the plugin code

With your development environment set up, you can now start writing the code for your plugin.

The first thing you will need to do is import the Figma API. Add the following line to the top of your code.js file:

const figma = require("figma-js");

Next, you will need to define the function that will be called when the plugin is run. This function should accept two colors as arguments and return the contrast ratio between them.

Here is an example of a function that calculates the contrast ratio between two colors:

function contrastRatio(color1, color2) {
const luminance1 = luminance(color1);
const luminance2 = luminance(color2);
const ratio = (Math.max(luminance1, luminance2) + 0.05) / (Math.min(luminance1, luminance2) + 0.05);
return ratio.toFixed(2);
}
function luminance(color) {
const r = normalizedChannelValue(color.r);
const g = normalizedChannelValue(color.g);
const b = normalizedChannelValue(color.b);
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
function normalizedChannelValue(channel) {
return channel / 255 <= 0.03928
? channel / 12.92
: Math.pow((channel + 0.055) / 1.055, 2.4);
}

This function uses the luminance values of the two colors to calculate the contrast ratio. The luminance of a color is a measure of how bright it appears to the human eye.

Step 3: Create the UI for your plugin

With the core functionality of your plugin implemented, you can now create the user interface (UI) for your plugin.

The UI for a Figma plugin is created using HTML and CSS. You can use the figma.ui.html and figma.ui.css properties to set the HTML and CSS for your plugin's UI.

Try to apply and comment your feedback on the results.
Thanks.

--

--

Achuth hadnoor

On my way to be an indie developer, creator creating a focus on better living.