Socket
Socket
Sign inDemoInstall

smooth-gradient

Package Overview
Dependencies
0
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.3.2 to 2.4.0

test/Color.test.js

2

dist/index.js

@@ -1,1 +0,1 @@

!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var o=t();for(var r in o)("object"==typeof exports?exports:e)[r]=o[r]}}(self,(()=>(()=>{"use strict";var e={d:(t,o)=>{for(var r in o)e.o(o,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:o[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{Gradient:()=>r});class o{constructor(e){const t=e.replace("#","");if(3!==t.length&&6!==t.length)throw new Error("Hex color is not valid");const r=3==t.length?/.{1}/g:/.{1,2}/g,s=t.match(r);if(3!==s.length)throw new Error("Could not get RGB parameters");this.r=o.hexToDec(s[0]),this.g=o.hexToDec(s[1]),this.b=o.hexToDec(s[2])}static isValidHex=e=>/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/.test(e);static hexToDec=e=>(1===e.length&&(e+=e),parseInt(e,16));static decToHex=e=>{let t=e.toString(16);return t.length<=1&&(t="0"+t),t}}class r{constructor(...e){this.colors=[];for(const t of e)o.isValidHex(t)&&this.colors.push(new o(t))}getSubGradient=e=>{e<0&&(e=0),e>100&&(e=100);const t=Math.min(Math.trunc(e*(this.colors.length-1)/100),this.colors.length-2),o=this.getColorsScale();return{from:{color:this.colors[t],value:o[t]},to:{color:this.colors[t+1],value:o[t+1]}}};getColorsScale=()=>this.colors.map(((e,t)=>100*t/(this.colors.length-1)));getWeightedAverage=(e,t,o,r)=>{const s=Math.pow(e,2)*t,l=Math.pow(o,2)*r;return Math.sqrt((s+l)/(t+r))};getColorFromValue=(e,t="hex")=>{const r=this.getSubGradient(e),s=Math.round(r.to.value-r.from.value),l=Math.round(s-Math.abs(r.from.value-e)),n=Math.round(s-l),a=Math.round(this.getWeightedAverage(r.from.color.r,l,r.to.color.r,n)),c=Math.round(this.getWeightedAverage(r.from.color.g,l,r.to.color.g,n)),h=Math.round(this.getWeightedAverage(r.from.color.b,l,r.to.color.b,n));return"hex"===t?`#${o.decToHex(a)}${o.decToHex(c)}${o.decToHex(h)}`:"rgb"===t?`rgb(${a}, ${c}, ${h})`:[a,c,h]}}return t})()));
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var o=t();for(var r in o)("object"==typeof exports?exports:e)[r]=o[r]}}(self,(()=>(()=>{"use strict";var e={d:(t,o)=>{for(var r in o)e.o(o,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:o[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{Gradient:()=>r});class o{constructor(e){if(!o.isValidHex(e))throw new Error("Hex value or color format is not valid");const t=e.replace("#",""),r=3==t.length?/.{1}/g:/.{1,2}/g,s=t.match(r);this.r=o.hexToDec(s[0]),this.g=o.hexToDec(s[1]),this.b=o.hexToDec(s[2])}static isValidHex=e=>/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/.test(e);static hexToDec=e=>(1===e.length&&(e+=e),parseInt(e,16));static decToHex=e=>{if(!/\d+/.test(e))return NaN;let t=e.toString(16);return t.length<=1&&(t="0"+t),t}}class r{constructor(...e){this.colors=[];for(const t of e)o.isValidHex(t)&&this.colors.push(new o(t))}getSubGradient=e=>{isNaN(e)&&(e=0),e<0&&(e=0),e>100&&(e=100);const t=Math.min(Math.trunc(e*(this.colors.length-1)/100),this.colors.length-2),o=this.getColorsScale();return{from:{color:this.colors[t],value:o[t]},to:{color:this.colors[t+1],value:o[t+1]}}};getColorsScale=()=>this.colors.map(((e,t)=>100*t/(this.colors.length-1)));getWeightedAverage=(e,t,o,r)=>{const s=Math.pow(e,2)*t,a=Math.pow(o,2)*r;return Math.sqrt((s+a)/(t+r))};getColorFromValue=(e,t="hex")=>{const r=this.getSubGradient(e),s=Math.round(r.to.value-r.from.value),a=Math.round(s-Math.abs(r.from.value-e)),l=Math.round(s-a),c=Math.round(this.getWeightedAverage(r.from.color.r,a,r.to.color.r,l)),i=Math.round(this.getWeightedAverage(r.from.color.g,a,r.to.color.g,l)),n=Math.round(this.getWeightedAverage(r.from.color.b,a,r.to.color.b,l));return"hex"===t?`#${o.decToHex(c)}${o.decToHex(i)}${o.decToHex(n)}`:"rgb"===t?`rgb(${c}, ${i}, ${n})`:[c,i,n]}}return t})()));
{
"name": "smooth-gradient",
"version": "2.3.2",
"version": "2.4.0",
"description": "Generates a gradient and can return a calculated color from an input",

@@ -9,3 +9,3 @@ "type": "module",

"start": "node src/index.js",
"test": "node src/test.js",
"test": "jest --coverage=true",
"build": "webpack --mode=production"

@@ -32,5 +32,16 @@ },

"devDependencies": {
"@babel/plugin-transform-modules-commonjs": "^7.23.0",
"jest": "^29.7.0",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4"
},
"babel": {
"env": {
"test": {
"plugins": [
"@babel/plugin-transform-modules-commonjs"
]
}
}
}
}
export default class Color {
constructor(hex) {
const curated_hex = hex.replace('#', '');
if (curated_hex.length !== 3 && curated_hex.length !== 6) {
throw new Error('Hex color is not valid');
if (!Color.isValidHex(hex)) {
throw new Error('Hex value or color format is not valid');
}
const curated_hex = hex.replace('#', '');
const split_regex = curated_hex.length == 3 ? /.{1}/g : /.{1,2}/g
const array_colors = curated_hex.match(split_regex);
if (array_colors.length !== 3) {
throw new Error('Could not get RGB parameters');
}

@@ -25,2 +23,5 @@ this.r = Color.hexToDec(array_colors[0]);

static decToHex = (dec) => {
if (!/\d+/.test(dec)) {
return NaN;
}
let hex = dec.toString(16);

@@ -27,0 +28,0 @@ if (hex.length <= 1) {

@@ -19,2 +19,3 @@ import Color from './Color.js';

getSubGradient = (value) => {
if (isNaN(value)) value = 0;
if (value < 0) value = 0;

@@ -21,0 +22,0 @@ if (value > 100) value = 100;

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc