Spaces:
Sleeping

File size: 8,069 Bytes
611725d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5da34a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34c6998
a5da34a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abd1771
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
611725d
 
 
 
 
 
 
 
 
 
 
a5da34a
 
 
 
611725d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abd1771
611725d
abd1771
611725d
abd1771
611725d
 
 
 
 
abd1771
611725d
 
abd1771
 
 
 
 
 
 
 
 
 
 
611725d
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
function ColorPanel({ data, expanded = false }) {
  const color = data || {
    temperature: 'Warm',
    contrast: 'Medium-High',
    saturation: 'Slightly Desaturated',
    shadows: 'Lifted, Teal tint',
    highlights: 'Warm, Orange push',
    palette: ['#1a2634', '#3d4f5f', '#d4a574', '#f5e6d3', '#8b5a2b'],
  }

  const items = [
    { label: 'Temperature', value: color.temperature },
    { label: 'Contrast', value: color.contrast },
    { label: 'Saturation', value: color.saturation },
    { label: 'Shadows', value: color.shadows },
    { label: 'Highlights', value: color.highlights },
  ]

  // Generate and download .cube LUT file
  const downloadLUT = () => {
    const size = 17 // Standard LUT size
    const toneCurve = color.toneCurve || [[0, 0], [50, 50], [100, 100]]

    // Interpolate tone curve value
    const applyToneCurve = (value) => {
      const v = value * 100
      for (let i = 0; i < toneCurve.length - 1; i++) {
        const [x1, y1] = toneCurve[i]
        const [x2, y2] = toneCurve[i + 1]
        if (v >= x1 && v <= x2) {
          const t = (v - x1) / (x2 - x1)
          return (y1 + t * (y2 - y1)) / 100
        }
      }
      return value
    }

    // Determine color adjustments from analysis
    const isWarm = color.temperature?.toLowerCase().includes('warm')
    const isCool = color.temperature?.toLowerCase().includes('cool')
    const hasTealShadows = color.shadows?.toLowerCase().includes('teal')
    const hasWarmHighlights = color.highlights?.toLowerCase().includes('warm')

    let lutContent = `TITLE "FrameMatch - Generated LUT"\n`
    lutContent += `# Generated from image analysis\n`
    lutContent += `LUT_3D_SIZE ${size}\n\n`

    for (let b = 0; b < size; b++) {
      for (let g = 0; g < size; g++) {
        for (let r = 0; r < size; r++) {
          let rVal = r / (size - 1)
          let gVal = g / (size - 1)
          let bVal = b / (size - 1)

          // Apply tone curve
          const luminance = 0.299 * rVal + 0.587 * gVal + 0.114 * bVal
          const curveMultiplier = applyToneCurve(luminance) / (luminance || 0.001)

          rVal = Math.min(1, rVal * curveMultiplier)
          gVal = Math.min(1, gVal * curveMultiplier)
          bVal = Math.min(1, bVal * curveMultiplier)

          // Apply temperature shift
          if (isWarm) {
            rVal = Math.min(1, rVal * 1.05)
            bVal = bVal * 0.95
          } else if (isCool) {
            rVal = rVal * 0.95
            bVal = Math.min(1, bVal * 1.05)
          }

          // Apply shadow tint (affects darker values)
          if (hasTealShadows && luminance < 0.4) {
            const shadowAmount = (0.4 - luminance) / 0.4
            gVal = Math.min(1, gVal + shadowAmount * 0.03)
            bVal = Math.min(1, bVal + shadowAmount * 0.05)
          }

          // Apply highlight warmth (affects brighter values)
          if (hasWarmHighlights && luminance > 0.6) {
            const highlightAmount = (luminance - 0.6) / 0.4
            rVal = Math.min(1, rVal + highlightAmount * 0.03)
          }

          lutContent += `${rVal.toFixed(6)} ${gVal.toFixed(6)} ${bVal.toFixed(6)}\n`
        }
      }
    }

    // Create and download file
    const blob = new Blob([lutContent], { type: 'text/plain' })
    const url = URL.createObjectURL(blob)
    const a = document.createElement('a')
    a.href = url
    a.download = 'shot-match.cube'
    document.body.appendChild(a)
    a.click()
    document.body.removeChild(a)
    URL.revokeObjectURL(url)
  }

  // Generate SVG path from tone curve points
  // Points are [x, y] where x and y are 0-100, y needs to be flipped for SVG coordinates
  const generateCurvePath = (points) => {
    if (!points || points.length < 2) {
      // Fallback to linear curve
      return "M 0 100 L 100 0"
    }

    // Convert points to SVG coordinates (flip Y axis)
    const svgPoints = points.map(([x, y]) => [x, 100 - y])

    // Create a smooth curve through the points using quadratic bezier
    let path = `M ${svgPoints[0][0]} ${svgPoints[0][1]}`

    for (let i = 1; i < svgPoints.length; i++) {
      const prev = svgPoints[i - 1]
      const curr = svgPoints[i]

      // Use quadratic bezier for smooth curve
      const cpX = (prev[0] + curr[0]) / 2
      const cpY = prev[1]

      if (i === 1) {
        path += ` Q ${cpX} ${cpY}, ${curr[0]} ${curr[1]}`
      } else {
        path += ` T ${curr[0]} ${curr[1]}`
      }
    }

    return path
  }

  const curvePath = generateCurvePath(color.toneCurve)
  const fillPath = curvePath + " L 100 100 L 0 100 Z"

  return (
    <div className="p-6 bg-slate-800/50 rounded-xl border border-slate-700/50">
      <div className="flex items-center justify-between mb-4">
        <div className="flex items-center gap-2">
          <div className="w-8 h-8 rounded-lg bg-rose-500/20 flex items-center justify-center">
            <svg className="w-4 h-4 text-rose-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M7 21a4 4 0 01-4-4V5a2 2 0 012-2h4a2 2 0 012 2v12a4 4 0 01-4 4zm0 0h12a2 2 0 002-2v-4a2 2 0 00-2-2h-2.343M11 7.343l1.657-1.657a2 2 0 012.828 0l2.829 2.829a2 2 0 010 2.828l-8.486 8.485M7 17h.01" />
            </svg>
          </div>
          <h3 className="text-lg font-semibold text-white">Color Grade</h3>
        </div>
        <button
          onClick={downloadLUT}
          className="px-3 py-1 text-xs font-medium text-amber-400 border border-amber-500/50 rounded-lg hover:bg-amber-500/10 transition-colors"
        >
          Download LUT
        </button>
      </div>

      {!expanded && (
        <div className="flex gap-1 h-8 rounded-lg overflow-hidden mb-4">
          {color.palette?.map((c, i) => (
            <div key={i} className="flex-1" style={{ backgroundColor: c }} />
          ))}
        </div>
      )}

      <div className={`grid ${expanded ? 'grid-cols-1' : 'grid-cols-2'} gap-3`}>
        {items.map((item, index) => (
          <div key={index} className="py-2">
            <span className="text-xs text-slate-500 block mb-1">{item.label}</span>
            <span className="text-sm font-medium text-white">{item.value}</span>
          </div>
        ))}
      </div>

      {expanded && (
        <div className="mt-6 pt-6 border-t border-slate-700/50">
          <h4 className="text-sm font-medium text-slate-400 mb-4">Tone Curve</h4>
          <div className="h-40 bg-slate-900 rounded-lg relative overflow-hidden">
            <svg className="w-full h-full" viewBox="0 0 100 100" preserveAspectRatio="none">
              <defs>
                <linearGradient id="curveGradient" x1="0%" y1="100%" x2="100%" y2="0%">
                  <stop offset="0%" stopColor="#f59e0b" stopOpacity="0.3" />
                  <stop offset="100%" stopColor="#ea580c" stopOpacity="0.3" />
                </linearGradient>
              </defs>
              {/* Reference diagonal line (linear response) */}
              <line x1="0" y1="100" x2="100" y2="0" stroke="#374151" strokeWidth="0.5" strokeDasharray="2,2" />
              {/* Actual tone curve from image analysis */}
              <path
                d={curvePath}
                fill="none"
                stroke="url(#curveGradient)"
                strokeWidth="2"
              />
              <path
                d={fillPath}
                fill="url(#curveGradient)"
              />
              {/* Control points visualization */}
              {color.toneCurve?.map(([x, y], i) => (
                <circle
                  key={i}
                  cx={x}
                  cy={100 - y}
                  r="1.5"
                  fill="#f59e0b"
                  opacity="0.6"
                />
              ))}
            </svg>
            <div className="absolute bottom-2 left-2 text-xs text-slate-500">Shadows</div>
            <div className="absolute top-2 right-2 text-xs text-slate-500">Highlights</div>
          </div>
        </div>
      )}
    </div>
  )
}

export default ColorPanel