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,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAG,KAAK,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QAC1F,IAAI,aAAa,GAAG,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;QAEpD,MAAM,SAAS,GAAG;YAChB,GAAG,aAAa;YAChB,kFAAkF;YAClF,eAAe,EAAE,IAAI,CAAC,kBAAkB,EAAE;SAC3C,CAAC;QACF,iHAAiH;QACjH,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,IAAI,KAAK,CAAC,EAAG,CAAC;IACxE,CAAC;CACF","sourcesContent":["import React from 'react';\nimport { StyleSheet, 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\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  getAngle(): string {\n    const startPoint = this.props.startPoint ? this.props.startPoint : [0.5, 0.0];\n    const endPoint = this.props.endPoint ? this.props.endPoint : [0.5, 1.0];\n    const { width = 0, height = 0 } = this.state;\n    let angle = 0;\n\n    const gradientWidth = height * (endPoint[0] - startPoint[0]);\n    const gradientHeight = width * (endPoint[1] - startPoint[1]);\n    angle = Math.atan2(gradientHeight, gradientWidth) + Math.PI / 2;\n\n    return `${angle}rad`;\n  }\n\n  getColors(): string {\n    const { colors } = this.props;\n    return colors\n      .map((color, index) => {\n        const colorStr = `${color.toString(16)}`;\n        const hex = `#${colorStr.substring(2, colorStr.length)}`;\n\n        const location = this.props.locations && this.props.locations[index];\n        if (location) {\n          return `${hex} ${location * 100}%`;\n        }\n        return hex;\n      })\n      .join(',');\n  }\n\n  getBackgroundImage(): string | null {\n    if (this.state.width && this.state.height) {\n      return `linear-gradient(${this.getAngle()},${this.getColors()})`;\n    } else {\n      return 'transparent';\n    }\n  }\n\n  render() {\n    const { colors, locations, startPoint, endPoint, onLayout, style, ...props } = this.props;\n    let compiledStyle = StyleSheet.flatten(style) || {};\n\n    const flatStyle = {\n      ...compiledStyle,\n      // @ts-ignore: [ts] Property 'backgroundImage' does not exist on type 'ViewStyle'.\n      backgroundImage: this.getBackgroundImage(),\n    };\n    // TODO: Bacon: In the future we could consider adding `backgroundRepeat: \"no-repeat\"`. For more browser support.\n    return <View style={flatStyle} onLayout={this.onLayout} {...props} />;\n  }\n}\n"]}