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Ae,EAAE;SACpB,CAAC,CACF,QAAQ,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CACxB,IAAI,KAAK,CAAC,EACV,CACH,CAAC;IACJ,CAAC;CACF;AAED,SAAS,2BAA2B,CAAC,SAAiB;IACpD,IAAI,SAAS,KAAK,CAAC,EAAE;QACnB,OAAO,eAAe,CAAC;KACxB;IACD,MAAM,cAAc,GAAG,SAAS,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAC9C,MAAM,YAAY,GAAG,cAAc,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;IACjD,MAAM,KAAK,GAAG,cAAc,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAC7C,OAAO,IAAI,YAAY,GAAG,KAAK,EAAE,CAAC;AACpC,CAAC","sourcesContent":["import React from 'react';\nimport { View } from 'react-native';\n\ntype Props = {\n colors: number[];\n locations?: number[] | null;\n startPoint?: Point | null;\n endPoint?: Point | null;\n onLayout?: Function;\n} & React.ComponentProps<typeof View>;\n\ntype State = {\n width?: number;\n height?: number;\n};\n\ntype Point = [number, number];\n\nconst PI_2 = Math.PI / 2;\n\nexport default class NativeLinearGradient extends React.PureComponent<Props, State> {\n state = {\n width: undefined,\n height: undefined,\n };\n\n onLayout = event => {\n this.setState({\n width: event.nativeEvent.layout.width,\n height: event.nativeEvent.layout.height,\n });\n if (this.props.onLayout) {\n this.props.onLayout(event);\n }\n };\n\n getControlPoints = (): Point[] => {\n const { startPoint, endPoint } = this.props;\n\n let correctedStartPoint: Point = [0, 0];\n if (Array.isArray(startPoint)) {\n correctedStartPoint = [\n startPoint[0] != null ? startPoint[0] : 0.0,\n startPoint[1] != null ? startPoint[1] : 0.0,\n ];\n }\n let correctedEndPoint: Point = [0.0, 1.0];\n if (Array.isArray(endPoint)) {\n correctedEndPoint = [\n endPoint[0] != null ? endPoint[0] : 0.0,\n endPoint[1] != null ? endPoint[1] : 1.0,\n ];\n }\n return [correctedStartPoint, correctedEndPoint];\n };\n\n calculateGradientAngleFromControlPoints = (): number => {\n const [start, end] = this.getControlPoints();\n const { width = 1, height = 1 } = this.state;\n start[0] *= width;\n end[0] *= width;\n start[1] *= height;\n end[1] *= height;\n const py = end[1] - start[1];\n const px = end[0] - start[0];\n return 90 + (Math.atan2(py, px) * 180) / Math.PI;\n };\n\n getWebGradientColorStyle = (): string => {\n return this.getGradientValues().join(',');\n };\n\n convertJSColorToGradientSafeColor = (color: number, index: number): string => {\n const { locations } = this.props;\n const hexColor = hexStringFromProcessedColor(color);\n let output = hexColor;\n if (locations && locations[index]) {\n const location = Math.max(0, Math.min(1, locations[index]));\n // Convert 0...1 to 0...100\n const percentage = location * 100;\n output += ` ${percentage}%`;\n }\n return output;\n };\n\n getGradientValues = (): string[] => {\n return this.props.colors.map(this.convertJSColorToGradientSafeColor);\n };\n\n getBackgroundImage = (): string => {\n return `linear-gradient(${this.calculateGradientAngleFromControlPoints()}deg, ${this.getWebGradientColorStyle()})`;\n };\n\n render() {\n const { colors, locations, startPoint, endPoint, onLayout, style, ...props } = this.props;\n const backgroundImage = this.getBackgroundImage();\n // TODO: Bacon: In the future we could consider adding `backgroundRepeat: \"no-repeat\"`. For more browser support.\n return (\n <View\n style={[\n style,\n // @ts-ignore: [ts] Property 'backgroundImage' does not exist on type 'ViewStyle'.\n { backgroundImage },\n ]}\n onLayout={this.onLayout}\n {...props}\n />\n );\n }\n}\n\nfunction hexStringFromProcessedColor(argbColor: number): string {\n if (argbColor === 0) {\n return `rgba(0,0,0,0)`;\n }\n const hexColorString = argbColor.toString(16);\n const withoutAlpha = hexColorString.substring(2);\n const alpha = hexColorString.substring(0, 2);\n return `#${withoutAlpha}${alpha}`;\n}\n"]}