xkeshav / color-collector

A vs code extension to collect all color value from a css file and replace every different color value with intuitive variable name.
https://marketplace.visualstudio.com/items?itemName=xkeshav.css-color-collector
Apache License 2.0
4 stars 2 forks source link
color color-collections css regex typescript vscode vscode-extension

CSS Color Collector

VS Marketplace Download Install License

A VS code extension which works with css file, collect every color from A css file and assign it to a meaningful css variable and put these variables under a new :root pseudo selector block on the file.

Description

In an open css file, on execution of collect colors command, this extension parse the file and search all color value across all supported color syntax and and assign every color value to a meaningful css variable name and replace the color value with variable and these color variables collected under a :root pseudo selector.

VS Code Marketplace Link

Use Case

This approach has a few benefits.

First, it can help make your code more readable by giving names to colors that might otherwise be difficult to remember.

Second, it can help you avoid duplicating colors throughout your code.

And third, if you ever need to change a color value, you can do so in one place and have the change propagate throughout your entire stylesheet.

Install

How to Use

Working Demo

collect and replace on same file.

create separate file for :root, when cssColorCollector.colorInSeparateFile is true

Configuration Option

To create a separate file for :root block

"cssColorCollector.colorInSeparateFile": true

check screenshot below for the same

vs code settings

note: if this option checked in either of User Settings or Workspace Settings then it will be consider as a true

Features

body {
  background-color: whitesmoke;
}

will be converted into

:root {
  --body-bg-1: whitesmoke;
}

body {
  background-color: --body-bg-1;
}

Note: if extension unable to parse property and selector of a color then variable name would be --defaultSelector__defaultElement--<number>

Note: New file will be created in the same directory where the css file is opened and naming convention of file would be color-collector--[open-file-name].css and multi line comment will be added on top of :root which mention the source file and date of conversion.

Screenshots

Basic Example

input file => basic.css file

basic: before conversion

output file - basic

basic: after conversion

Advance Example

input file => advance.css

advance: before conversion

output file - advance

advance: after conversion

check-list

Release Notes

Contribution Guide

To raise any issue / suggestion / request write here .

moreover, if you want to contribute, please feel free to raise the PR

Future Work

Known issues

References

followings are useful links which helps me to develop this extension

Also, few of the notable extensions which helps to write better code and test cases

Thank you for your time for reading.


Keshav Mohta xkeshav@gmail.com