import React from 'react' import { generateTransform, unitsDimension } from 'app/views/tile/tile.utils' export default function TileGradient({ tile, box, bounds, videoBounds, viewing, onMouseDown, onDoubleClick, onMouseEnter }) { // console.log(tile) const style = { transform: generateTransform(tile, box, bounds, videoBounds), opacity: tile.settings.opacity, } // console.log(generateTransform(tile)) let className = ['tile', tile.type].join(' ') if (tile.target_page_id || (viewing && tile.href)) { if (viewing || tile.settings.cursor !== 'unclickable') { className += ' ' + (tile.settings.cursor || 'hand_up') } } className += ' ' + tile.settings.align style.width = unitsDimension(tile, 'width', bounds, videoBounds) style.height = unitsDimension(tile, 'height', bounds, videoBounds) style.background = linearGradient(tile) return (
) } function linearGradient(tile) { let from_color = tileRGBA(tile.settings.from_color, tile.settings.from_opacity) let to_color = tileRGBA(tile.settings.to_color, tile.settings.to_opacity) let stop = parseFloat(tile.settings.stop) return [ "linear-gradient(", tile.settings.angle, "deg,", to_color, ",", stop + "%", ",", from_color, ")" ].join("") } function tileRGBA(hex, opacity) { const { r, g, b } = hexToRgb(hex) return "rgba(" + [r,g,b,opacity].join(",") + ")" } function hexToRgb(hex) { let result; if (hex.length === 7) { result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex) } else if (hex.length === 4) { result = /^#?([a-f\d]{1})([a-f\d]{1})([a-f\d]{1})$/i.exec(hex) } else { return { r: 255, g: 0, b: 255 } } return result ? { r: parseInt(result[1], 16), g: parseInt(result[2], 16), b: parseInt(result[3], 16) } : null }