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,CAAC;YACtB,IAAI,SAAS,IAAI,SAAS,CAAC,KAAK,CAAC,EAAE;gBACjC,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;gBAC5D,2BAA2B;gBAC3B,MAAM,UAAU,GAAG,QAAQ,GAAG,GAAG,CAAC;gBAClC,MAAM,IAAI,IAAI,UAAU,GAAG,CAAC;aAC7B;YACD,OAAO,MAAM,CAAC;QAChB,CAAC,CAAC,CAAC;QAEH,iBAAiB,CAAC,kBAAkB,CAAC,CAAC;IACxC,CAAC,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;IAExB,MAAM,UAAU,GAAG,cAAc,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC5C,MAAM,eAAe,GAAG,mBAAmB,WAAW,QAAQ,UAAU,GAAG,CAAC;IAC5E,gGAAgG;IAChG,mBAAmB;IACnB,OAAO,CACL,CAAC,IAAI,CACH,IAAI,KAAK,CAAC,CACV,KAAK,CAAC,CAAC;QACL,KAAK,CAAC,KAAK;QACX,kFAAkF;QAClF,EAAE,eAAe,EAAE;KACpB,CAAC,CACF,QAAQ,CAAC,CAAC,KAAK,CAAC,EAAE;QAChB,SAAS,CAAC,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;QACpC,IAAI,KAAK,CAAC,QAAQ,EAAE;YAClB,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;SACvB;IACH,CAAC,CAAC,EACF,CACH,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,oBAAoB,CAAC","sourcesContent":["import React, { FunctionComponent, useEffect, useState } from 'react';\nimport { LayoutRectangle, View } from 'react-native';\nimport normalizeColor from 'react-native-web/src/modules/normalizeColor';\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 Point = [number, number];\n\nconst NativeLinearGradient: FunctionComponent<Props> = ({\n colors,\n locations,\n startPoint,\n endPoint,\n ...props\n}: Props) => {\n const [layout, setLayout] = useState<LayoutRectangle | null>(null);\n const [gradientColors, setGradientColors] = useState<string[]>([]);\n const [pseudoAngle, setPseudoAngle] = useState<number>(0);\n\n useEffect(() => {\n const getControlPoints = (): Point[] => {\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 const [start, end] = getControlPoints();\n const { width = 1, height = 1 } = layout || {};\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\n setPseudoAngle(90 + (Math.atan2(py, px) * 180) / Math.PI);\n }, [startPoint, endPoint]);\n\n useEffect(() => {\n const nextGradientColors = colors.map((color: number, index: number): string => {\n const hexColor = normalizeColor(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 setGradientColors(nextGradientColors);\n }, [colors, locations]);\n\n const colorStyle = gradientColors.join(',');\n const backgroundImage = `linear-gradient(${pseudoAngle}deg, ${colorStyle})`;\n // TODO: Bacon: In the future we could consider adding `backgroundRepeat: \"no-repeat\"`. For more\n // browser support.\n return (\n <View\n {...props}\n style={[\n props.style,\n // @ts-ignore: [ts] Property 'backgroundImage' does not exist on type 'ViewStyle'.\n { backgroundImage },\n ]}\n onLayout={event => {\n setLayout(event.nativeEvent.layout);\n if (props.onLayout) {\n props.onLayout(event);\n }\n }}\n />\n );\n};\n\nexport default NativeLinearGradient;\n"]}
\No newline at end of file