Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

postcss-hexrgba

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postcss-hexrgba - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

98

index.js

@@ -6,33 +6,35 @@ 'use strict';

module.exports = postcss.plugin('postcss-hexrgba', function () {
return function (css) {
// build hex -> converter (returns array)
/**
* Hex to RGB converter
* @param {string} hex hexidecimal string
* @return {array} RGB values
*/
var hexRgb = function(hex){
// strip the #
var stripped = hex.split('');
stripped.shift();
hex = stripped.join('');
// if given a shorthand decleration, expand it
// If given shorthand, expand it
var shorthandCheck = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandCheck, function(m, r, g, b) {
return r + r + g + g + b + b;
return '#' + r + r + g + g + b + b;
});
// convert it
var convert = parseInt(hex, 16);
var red = convert >> 16 & 255;
var green = convert >> 8 & 255;
var blue = convert & 255;
// Extract full hex into an array
var rgbRegex = /^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i;
var rgb = hex
.replace(/^\s+|\s+$/g, '')
.match(rgbRegex);
// throw the results into an array
hex = [red, green, blue];
// Convert it
return rgb ? [
parseInt(rgb[1], 16),
parseInt(rgb[2], 16),
parseInt(rgb[3], 16)
] : false;
return hex;
};
// build our plugin handler
var ruleHandler = function(decl) {
/**
* CSS rule handler
* @param {string} decl CSS delcaration
*/
var ruleHandler = function(decl, result) {

@@ -42,37 +44,36 @@ var input = decl.value;

// only process rgba declaration values
if (input.indexOf('rgba') === -1) {
return false;
}
// strip out surrounding css property and throw the values in an array
// Strip out surrounding css property and throw the values in an array
var rgba = input.split('(')[1].split(')')[0];
rgba = rgba.split(',');
// only process rgba values with 2 arguments (hex + alpha)
// Only process rgba values with 2 arguments (hex + alpha)
if (rgba.length > 2){
return false;
return;
}
// store the alpha value for safe keeping
// Store the alpha value for safe keeping and strip trailing spaces
var a = rgba[1];
// and strip any trailing spaces
a = a.replace(/\s/g, '');
// rip out the alpha and turn array into hex string
// Rip out the alpha and turn array into hex string
var hex = rgba.splice(0, 1);
hex = hex.toString();
// feed it to our converter
var rgb = hexRgb('#ffffff');
// Feed it to our converter
var converted = hexRgb(hex);
// add the alpha back in
rgb.push(a);
// Add the alpha back in
if (!converted) {
result.warn('not a valid hex', { node: decl });
return;
}
// convert the whole thing to a string again and add back in css wrapper
output = rgb.toString();
converted.push(a);
// Convert the whole thing to a string again and add back in css wrapper
output = converted.toString();
output = 'rgba(' + output + ')';
// create the new declaration value
// Create the new declaration value
decl.value = output;

@@ -82,8 +83,17 @@

// loop through each css rule and declaration, and run our plugin through them
css.eachRule(function(rule) {
rule.each(ruleHandler);
});
// Do it!
return function(css, result) {
css.eachDecl(function(decl) {
// Only process rgba declaration values
if (decl.value.indexOf('rgba') === -1) {
return;
}
ruleHandler(decl, result);
});
};
});
{
"name": "postcss-hexrgba",
"version": "0.1.0",
"version": "0.1.1",
"description": "PostCSS plugin that adds shorthand hex methods to rgba() values",

@@ -5,0 +5,0 @@ "keywords": [

@@ -40,3 +40,3 @@ # PostCSS HexRGBa

MIT © [Sean King](http://simpla.io)
MIT © [Sean King](https://twitter.com/seaneking)

@@ -43,0 +43,0 @@ [npm-image]: https://badge.fury.io/js/postcss-hexrgba.svg

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc