summaryrefslogtreecommitdiff
path: root/frontend/app/views/tile/handles/tile.gradient.js
blob: 1b3cd80f75af69c6112e497c5d42f3eae8802111 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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 (
    <div
      className={className}
      onMouseDown={onMouseDown}
      onDoubleClick={onDoubleClick}
      onMouseEnter={onMouseEnter}
      style={style}
    />
  )
}

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
}