How to use the gradient-parser.parse function in gradient-parser

To help you get started, we’ve selected a few gradient-parser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github WordPress / gutenberg / packages / components / src / custom-gradient-picker / index.js View on Github external
export default function CustomGradientPicker( { value, onChange } ) {
	let hasGradient = !! value;
	// gradientAST will contain the gradient AST as parsed by gradient-parser npm module.
	// More information of its structure available at.
	let gradientAST;
	let gradientValueUsed;
	try {
		gradientAST = gradientParser.parse( value || DEFAULT_GRADIENT )[ 0 ];
		gradientValueUsed = value || DEFAULT_GRADIENT;
	} catch ( error ) {
		hasGradient = false;
		gradientAST = gradientParser.parse( DEFAULT_GRADIENT )[ 0 ];
		gradientValueUsed = DEFAULT_GRADIENT;
	}

	const onGradientStructureChange = ( newGradientStructure ) => {
		onChange( serializeGradient( newGradientStructure ) );
	};

	const gradientPickerDomRef = useRef();
	const markerPoints = getMarkerPoints( gradientAST );

	const [ gradientBarState, gradientBarStateDispatch ] = useReducer(
		customGradientBarReducer,
github catalinmiron / react-native-css-gradient / generator.js View on Github external
const generateGradient = memoize((gradient, sizes) => {
  return parser.parse(gradient).map(({ type, colorStops, orientation }) => {
    // YOLO: Radial gradients <3
    if (type === "radial-gradient") {
      return "Only linear-gradient type is supported for now";
    }
    const colorsAndLocations =
      type === "linear-gradient"
        ? getColorsAndLocations(colorStops)
        : getRepeatingColorsAndLocations(colorStops, sizes);

    return {
      ...colorsAndLocations,
      ...getVectorsByOrientation(orientation)
    };
  });
});
github WordPress / gutenberg / packages / components / src / custom-gradient-picker / index.js View on Github external
export default function CustomGradientPicker( { value, onChange } ) {
	let hasGradient = !! value;
	// gradientAST will contain the gradient AST as parsed by gradient-parser npm module.
	// More information of its structure available at.
	let gradientAST;
	let gradientValueUsed;
	try {
		gradientAST = gradientParser.parse( value || DEFAULT_GRADIENT )[ 0 ];
		gradientValueUsed = value || DEFAULT_GRADIENT;
	} catch ( error ) {
		hasGradient = false;
		gradientAST = gradientParser.parse( DEFAULT_GRADIENT )[ 0 ];
		gradientValueUsed = DEFAULT_GRADIENT;
	}

	const onGradientStructureChange = ( newGradientStructure ) => {
		onChange( serializeGradient( newGradientStructure ) );
	};

	const gradientPickerDomRef = useRef();
	const markerPoints = getMarkerPoints( gradientAST );

	const [ gradientBarState, gradientBarStateDispatch ] = useReducer(
		customGradientBarReducer,
		customGradientBarReducerInitialState
	);
	const onMouseEnterAndMove = ( event ) => {
		const insertPosition = getHorizontalRelativeGradientPosition(
github seek-oss / braid-design-system / lib / utils / isLight / index.ts View on Github external
const getLinearGradientColors = (color: string) => {
  const gradients: LinearGradients = parseGradient(color);

  return gradients[0].colorStops.map(({ type, value }) => {
    if (typeof value !== 'string') {
      throw new Error(
        'Gradient parsing in Braid currently only supports hex/literal values',
      );
    }

    return `${type === 'hex' ? '#' : ''}${value}`;
  });
};
github finos / perspective / packages / perspective-viewer-d3fc / src / js / series / colorStyles.js View on Github external
const parseGradient = (gradient, opacity) => {
    const parsed = gparser.parse(gradient)[0].colorStops;
    return parsed.map((g, i) => [g.length ? g.length.value / 100 : i / (parsed.length - 1), stepAsColor(g.value, opacity)]).sort((a, b) => a[0] - b[0]);
};
github finos / perspective / packages / perspective-viewer-highcharts / src / js / highcharts / color_axis.js View on Github external
function _get_gradient(type) {
    let gradient;
    if (window.ShadyCSS) {
        gradient = window.ShadyCSS.getComputedStyleValue(this, `--highcharts-${type}--gradient`);
    } else {
        gradient = getComputedStyle(this).getPropertyValue(`--highcharts-${type}--gradient`);
    }

    const parsed = gparser.parse(gradient)[0].colorStops;
    return parsed.map((x, i) => {
        let color;
        if (x.type === "rgb") {
            color = `rgb(${x.value.join(",")})`;
        } else {
            color = `#${x.value}`;
        }
        return [Number.parseFloat(x.length ? x.length.value / 100 : `${i / (parsed.length - 1)}`), color];
    });
}

gradient-parser

Parse CSS3 gradient definitions and return an AST.

MIT
Latest version published 3 years ago

Package Health Score

50 / 100
Full package analysis

Popular gradient-parser functions