Files
NativeScript/packages/core/ui/styling/linear-gradient.ts
Nathan Walker d756eb5574 feat(root-layout): support gradient colors on shade cover (#9626)
Co-authored-by: William Juan <williamjuan027@gmail.com>
2022-03-01 12:32:39 -08:00

66 lines
1.4 KiB
TypeScript

import { CoreTypes } from '../../core-types';
import { Color } from '../../color';
import { LinearGradient as CSSLinearGradient } from '../../css/parser';
export interface ColorStop {
color: Color;
offset?: CoreTypes.LengthPercentUnit;
}
export class LinearGradient {
public angle: number;
public colorStops: ColorStop[];
public static parse(value: CSSLinearGradient): LinearGradient {
const result = new LinearGradient();
result.angle = value.angle;
result.colorStops = value.colors.map((color) => {
const offset = color.offset || null;
let offsetUnit: CoreTypes.LengthPercentUnit;
if (offset && offset.unit === '%') {
offsetUnit = {
unit: '%',
value: offset.value,
};
}
return {
color: color.color,
offset: offsetUnit,
};
});
return result;
}
public static equals(first: LinearGradient, second: LinearGradient): boolean {
if (!first && !second) {
return true;
} else if (!first || !second) {
return false;
}
if (first.angle !== second.angle) {
return false;
}
if (first.colorStops.length !== second.colorStops.length) {
return false;
}
for (let i = 0; i < first.colorStops.length; i++) {
const firstStop = first.colorStops[i];
const secondStop = second.colorStops[i];
if (firstStop.offset !== secondStop.offset) {
return false;
}
if (!Color.equals(firstStop.color, secondStop.color)) {
return false;
}
}
return true;
}
}