feat: lut creator (2)

This commit is contained in:
Hinnka committed 2026-03-04 22:32:45 +08:00
1 parent 82aebac2ab
commit 05c7882ec8
4 files changed
+217 -138

No files matched your search

@@ -4,8 +4,10 @@ import android.graphics.Bitmap
import android.graphics.Color
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlin.math.ln
import kotlin.math.max
import kotlin.math.min
import kotlin.math.pow
object LocalImageAnalyzer {
@@ -38,6 +40,7 @@ object LocalImageAnalyzer {
val hueCounts = IntArray(8)
val hueSatSums = FloatArray(8)
val hueLumaSums = FloatArray(8)
val hsv = FloatArray(3)
var totalPixels = 0
@@ -81,7 +84,13 @@ object LocalImageAnalyzer {
val g = Color.green(color) / 255f
val b = Color.blue(color) / 255f
val luma = 0.2126f * r + 0.7152f * g + 0.0722f * b
// Use proper linear conversion and Oklab L for all analysis
val lr = OklchConverter.srgbToLinear(r)
val lg = OklchConverter.srgbToLinear(g)
val lb = OklchConverter.srgbToLinear(b)
val oklab = OklchConverter.linearSrgbToOklab(lr, lg, lb)
val luma = oklab[0]
lumaList[idx++] = luma
totalLuma += luma
@@ -89,23 +98,21 @@ object LocalImageAnalyzer {
if (r > b + 0.05f) warmCount++ else if (b > r + 0.05f) coolCount++
if (g > (r + b) / 2f + 0.05f) greenCount++ else if ((r + b) / 2f > g + 0.05f) magentaCount++
Color.colorToHSV(color, hsv)
val hue = hsv[0]
val sat = hsv[1]
totalSatSum += sat
val hueRad = Math.toRadians(hue.toDouble())
val (L, C, H) = OklchConverter.oklabToOklch(oklab[0], oklab[1], oklab[2])
totalSatSum += C
val hueRad = Math.toRadians(H.toDouble())
// Saturation weighted vector
val vx = (Math.cos(hueRad) * sat).toFloat()
val vy = (Math.sin(hueRad) * sat).toFloat()
// Saturation weighted vector (using Oklab Chroma)
val vx = (Math.cos(hueRad) * C).toFloat()
val vy = (Math.sin(hueRad) * C).toFloat()
// Luma region assignment for Split Toning & Shadows/Highlights
if (luma > 0.7f) {
// Luma region assignment (using Oklab L)
if (luma > 0.8f) {
hlCount++
hlX += vx
hlY += vy
highlightLumaSum += luma
} else if (luma < 0.3f) {
} else if (luma < 0.2f) {
shCount++
shX += vx
shY += vy
@@ -117,19 +124,20 @@ object LocalImageAnalyzer {
}
// HSL Shifts bucket assignment
if (sat > 0.01f) { // Include almost all pixels with some color info
if (C > 0.01f) {
val bucket = when {
hue < 15f || hue >= 345f -> 0 // Red
hue < 45f -> 1 // Orange
hue < 75f -> 2 // Yellow
hue < 165f -> 3 // Green
hue < 195f -> 4 // Cyan
hue < 255f -> 5 // Blue
hue < 285f -> 6 // Purple
H < 29f || H >= 330f + (360 - 330 + 29) / 2f -> 0 // Red
H < 60f -> 1 // Orange
H < 105f -> 2 // Yellow
H < 140f -> 3 // Green
H < 195f -> 4 // Cyan
H < 260f -> 5 // Blue
H < 295f -> 6 // Purple
else -> 7 // Magenta
}
hueCounts[bucket]++
hueSatSums[bucket] += sat
hueSatSums[bucket] += C
hueLumaSums[bucket] += luma
}
}
@@ -150,25 +158,30 @@ object LocalImageAnalyzer {
val p90 = lumaList[(lumaList.size * 0.9).toInt()]
val contrastEst = ((p90 - p10) - 0.65f) * 0.2f
// Highlights & Shadows
// Highlights & Shadows
val avgHighlightLuma = if (hlCount > 0) highlightLumaSum / hlCount else 0.85f
val avgShadowLuma = if (shCount > 0) shadowLumaSum / shCount else 0.15f
val highlightsEst = ((avgHighlightLuma - 0.85f) * 0.8f).coerceIn(-0.3f, 0.3f)
val shadowsEst = ((avgShadowLuma - 0.15f) * 0.8f).coerceIn(-0.3f, 0.3f)
// Reduce parametric weights: curve handles tone mapping better
val highlightsEst = ((avgHighlightLuma - 0.85f) * 0.3f).coerceIn(-0.15f, 0.15f)
val shadowsEst = ((avgShadowLuma - 0.15f) * 0.5f).coerceIn(-0.2f, 0.2f)
// 5. Build Macro Tone Curve (CDF Histogram Matching)
// 5. Build Macro Tone Curve (Look extraction)
val lumaCurve = mutableListOf<List<Float>>()
// Allow higher lift for faded film blacks (up to 0.18)
lumaCurve.add(listOf(0f, lumaList.first().coerceIn(0f, 0.18f)))
lumaCurve.add(listOf(0f, lumaList.first().coerceIn(0f, 0.12f)))
for (i in 1..9) {
val pX = i * 0.1f // 0.1, 0.2 ... 0.9 input neutral
val pX = i * 0.1f
val pY = lumaList[(lumaList.size * pX).toInt()]
// Strongly favor the original photo's histogram (80% photo / 20% linear anchor)
val blendedY = pX * 0.2f + pY * 0.8f
// Balanced weights to preserve curve style: 60% histogram influence
// Use smooth threshold to protect highlights (L > 0.85)
val protection = max(0f, (pX - 0.85f) / 0.15f).pow(1.5f)
val photoWeight = (0.6f * (1f - protection)).coerceIn(0f, 0.6f)
val blendedY = pX * (1f - photoWeight) + pY * photoWeight
lumaCurve.add(listOf(pX, blendedY.coerceIn(0f, 1f)))
}
// Allow deeper compressed highlights
lumaCurve.add(listOf(1f, lumaList.last().coerceIn(0.85f, 1f)))
lumaCurve.add(listOf(1f, 1f))
// 2. White Balance / Temperature & Tint
val tempEst = if (warmCount + coolCount > 0) {
@@ -193,8 +206,8 @@ object LocalImageAnalyzer {
var h = Math.toDegrees(Math.atan2(avgY.toDouble(), avgX.toDouble())).toFloat()
if (h < 0) h += 360f
// Extract Oklab chroma and scale broadly to 0.0-1.0 mapping range.
// Reduced max multiplier from 2.0 to 1.0 to prevent overly thick color grading
val s = min(1f, Math.sqrt((avgX * avgX + avgY * avgY).toDouble()).toFloat()) * 1.0f * satMultiplier
// Reduce saturation extraction weight to prevent overly aggressive color casts
val s = min(1f, Math.sqrt((avgX * avgX + avgY * avgY).toDouble()).toFloat()) * 0.4f * satMultiplier
return ToningTarget(hue = h, saturation = s)
}
@@ -205,51 +218,62 @@ object LocalImageAnalyzer {
val midToning = getToningTarget(midX, midY, midCount, 0.1f)
// 4. HSL Shifts Evaluator
val avgSatGlobal = if (totalPixels > 0) totalSatSum / totalPixels else 0f
val neutralSat = 0.08f // Baseline in Oklab Chroma (C) is much lower than HSV Sat
val avgSatGlobal = if (totalPixels > 0) totalSatSum / totalPixels else neutralSat
val saturationEst = (1.0f + ln(max(0.01f, avgSatGlobal / neutralSat)) * 0.4f).coerceIn(0.7f, 1.4f)
val hueLumaBaselines = floatArrayOf(0.4f, 0.6f, 0.8f, 0.65f, 0.6f, 0.35f, 0.45f, 0.5f)
fun getShiftSat(bucketIndex: Int): Float {
// Use global average as baseline if this specific color is missing
val bucketAvgSat = if (hueCounts[bucketIndex] > totalPixels * 0.005f) {
hueSatSums[bucketIndex] / hueCounts[bucketIndex]
} else {
avgSatGlobal
}
val neutralSat = 0.35f // "Standard" photo saturation baseline in HSV
// Using a multiplier that maps 0.0 to ~ -1.0
return ((bucketAvgSat - neutralSat) * 2.85f).coerceIn(-1.0f, 1.0f)
val rawShifts = Array(8) { bucketIndex ->
if (hueCounts[bucketIndex] < totalPixels * 0.005f) return@Array Shift()
val bucketAvgSat = hueSatSums[bucketIndex] / hueCounts[bucketIndex]
val bucketAvgLuma = hueLumaSums[bucketIndex] / hueCounts[bucketIndex]
val baseLuma = hueLumaBaselines[bucketIndex]
// Raw scales
Shift(
sScale = (1.0f + ln(max(0.1f, bucketAvgSat / neutralSat))).coerceIn(0.4f, 2.0f),
lScale = (1.0f + ln(max(0.1f, bucketAvgLuma / baseLuma)) * 0.5f).coerceIn(0.5f, 1.6f)
)
}
// Banding Prevention: Circular smoothing on HSL buckets (moving average)
fun getSmoothedShift(index: Int): Shift {
var sumS = 0f
var sumL = 0f
val neighbors = listOf(-1, 0, 1) // kernel
for (offset in neighbors) {
val i = (index + offset + 8) % 8
sumS += rawShifts[i].sScale
sumL += rawShifts[i].lScale
}
return Shift(sScale = sumS / 3f, lScale = sumL / 3f)
}
val smoothedHsl = HslShifts(
red = getSmoothedShift(0),
orange = getSmoothedShift(1),
yellow = getSmoothedShift(2),
green = getSmoothedShift(3),
cyan = getSmoothedShift(4),
blue = getSmoothedShift(5),
purple = getSmoothedShift(6),
magenta = getSmoothedShift(7)
)
LutRecipe(
colorFeatures = ColorFeatures(
tone = Tone(
exposure = exposureEst,
contrast = contrastEst,
saturation = saturationEst,
highlights = highlightsEst,
shadows = shadowsEst
),
balance = Balance(
temperature = tempEst,
tint = tintEst
),
splitToning = SplitToning(
shadows = shToning,
midtones = midToning,
highlights = hlToning
),
hslShifts = HslShifts(
red = Shift(sShift = getShiftSat(0)),
orange = Shift(sShift = getShiftSat(1)),
yellow = Shift(sShift = getShiftSat(2)),
green = Shift(sShift = getShiftSat(3)),
cyan = Shift(sShift = getShiftSat(4)),
blue = Shift(sShift = getShiftSat(5)),
purple = Shift(sShift = getShiftSat(6)),
magenta = Shift(sShift = getShiftSat(7))
),
curves = Curves(
luma = lumaCurve
)
balance = Balance(temperature = tempEst, tint = tintEst),
splitToning = SplitToning(shadows = shToning, midtones = midToning, highlights = hlToning),
hslShifts = smoothedHsl,
curves = Curves(luma = lumaCurve)
)
)
}
@@ -50,36 +50,52 @@ object LutGenerator {
val oklab = OklchConverter.linearSrgbToOklab(lr, lg, lb)
var (L, C, H) = OklchConverter.oklabToOklch(oklab[0], oklab[1], oklab[2])
// Apply Global Saturation
C *= features.tone.saturation.coerceAtLeast(0f)
// 2. Global Contrast / Tone (applied on Lightness L)
L = applyCurve(L, features.curves.luma)
L = applyContrast(L, features.tone.contrast)
L = applyHighlightsShadows(L, features.tone.highlights, features.tone.shadows)
// 3. Black/White point
L = L * (1f - features.tone.whitePoint - features.tone.blackPoint) + features.tone.blackPoint
// 3. Black/White point (Map [0,1] to [BlackPoint, WhitePoint])
val wp = features.tone.whitePoint
val bp = features.tone.blackPoint
L = (L * (wp - bp) + bp).coerceIn(0f, 1f)
// 4. HSL Shifts
if (hasAnyHslShift(features.hslShifts)) {
val shift = getHslShiftForHue(H, features.hslShifts)
// Universal Damping Logic:
// 1. Chroma Gate: Only apply shifts to distinct colors. Protections neutrals (white/gray).
// Use a higher power (3.0) for a smoother 'fade-in' from gray.
val chromaGate = (C / 0.15f).pow(3.0f).coerceIn(0f, 1f)
// 2. Luma Gate: Protect the poles (Pure White and Pure Black)
// Use a smoother sine-based or higher-power parabola to avoid mid-range jumps.
val lumaGate = sin(L * PI.toFloat()).pow(1.5f).coerceIn(0f, 1f)
val finalDamp = (chromaGate * lumaGate).coerceIn(0f, 1f)
val hShift = shift.hShift * finalDamp
val sScale = 1.0f + (shift.sScale - 1.0f) * finalDamp
val lScale = 1.0f + (shift.lScale - 1.0f) * finalDamp
// H is 0-360 degrees
H += shift.hShift * 30f // Arbitrary scale for Hue shift, normally relative.
H += hShift * 360f
if (H < 0) H += 360f
if (H >= 360f) H -= 360f
// C is saturation (chroma). It is unbounded, typically 0 to 0.32
// sShift from -1 to 1 should scale C.
// If sShift > 0, boost saturation. If < 0, reduce it.
C *= (1.0f + shift.sShift).coerceAtLeast(0f)
// L is Lightness 0.0 to 1.0.
// lShift -1 to 1 should add to L.
L = (L + shift.lShift * 0.2f).coerceIn(0f, 1f)
// Saturation and Lightness scaling
C *= sScale.coerceAtLeast(0f)
L = (L * lScale).coerceIn(0f, 1f)
}
// 5. Split Toning
// Toning the shadow and highlight regions
val shadowWeight = max(0f, 1.0f - (L / 0.5f)).pow(2.0f).coerceIn(0f, 1f)
val highlightWeight = max(0f, (L - 0.5f) / 0.5f).pow(2.0f).coerceIn(0f, 1f)
// Use wider, more overlapping curves for smoother blending
val shadowWeight = (1.0f - L).pow(2.5f).coerceIn(0f, 1f)
val highlightWeight = L.pow(2.5f).coerceIn(0f, 1f)
val midtoneWeight = (1.0f - shadowWeight - highlightWeight).coerceIn(0f, 1f)
var oklabOut = OklchConverter.oklchToOklab(L, C, H)
@@ -88,9 +104,7 @@ object LutGenerator {
fun applyToning(target: ToningTarget, weight: Float) {
if (target.saturation > 0f && weight > 0f) {
// Oklab Chroma (a/b axes) max bounds are roughly [-0.3, 0.3].
// So adding 0.1 is already a massive cinematic color shift.
val maxChromaShift = 0.1f
val maxChromaShift = 0.12f
val hRad = target.hue * Math.PI.toFloat() / 180f
ta += target.saturation * maxChromaShift * weight * cos(hRad)
tb += target.saturation * maxChromaShift * weight * sin(hRad)
@@ -103,6 +117,16 @@ object LutGenerator {
// Convert back
val rgb = OklchConverter.oklabToLinearSrgb(L, ta, tb)
// Safety: Prevent negative or NaN values before WB and sRGB conversion.
rgb[0] = rgb[0].coerceAtLeast(0f)
rgb[1] = rgb[1].coerceAtLeast(0f)
rgb[2] = rgb[2].coerceAtLeast(0f)
// 6. RGB Channel Curves (Applied on Linear RGB)
rgb[0] = applyCurve(rgb[0], features.curves.red)
rgb[1] = applyCurve(rgb[1], features.curves.green)
rgb[2] = applyCurve(rgb[2], features.curves.blue)
// White balance on Linear (simplified)
if (features.balance.temperature != 0f || features.balance.tint != 0f) {
@@ -135,12 +159,12 @@ object LutGenerator {
private fun applyHighlightsShadows(x: Float, highlights: Float, shadows: Float): Float {
var ret = x
if (shadows != 0f) {
val sWeight = 1f - x
ret += shadows * (sWeight * sWeight) * 0.5f
val sWeight = (1f - x).pow(2.0f)
ret += shadows * sWeight * 0.4f
}
if (highlights != 0f) {
val hWeight = x
ret += highlights * (hWeight * hWeight) * 0.5f
val hWeight = x.pow(2.0f)
ret += highlights * hWeight * 0.4f
}
return ret
}
@@ -165,40 +189,65 @@ object LutGenerator {
}
private fun hasAnyHslShift(shifts: HslShifts): Boolean {
return shifts.red.hShift != 0f || shifts.red.sShift != 0f || shifts.red.lShift != 0f ||
shifts.green.hShift != 0f || shifts.green.sShift != 0f || shifts.green.lShift != 0f ||
shifts.blue.hShift != 0f || shifts.blue.sShift != 0f || shifts.blue.lShift != 0f
// checking major ones for quick exit
// Checking for any non-neutral values
return shifts.red.hShift != 0f || shifts.red.sScale != 1f || shifts.red.lScale != 1f ||
shifts.green.hShift != 0f || shifts.green.sScale != 1f || shifts.green.lScale != 1f ||
shifts.blue.hShift != 0f || shifts.blue.sScale != 1f || shifts.blue.lScale != 1f
}
private fun getHslShiftForHue(hue: Float, shifts: HslShifts): Shift {
// Precise Oklch Hue Angles mapping for color buckets (Red 29°, Orange 60°, Yellow 105°, Green 140°, Cyan 195°, Blue 260°, Purple 295°, Magenta 330°)
val targets = listOf(
29f to shifts.red,
60f to shifts.orange, // Standard orange is ~60 in Oklch
105f to shifts.yellow,
140f to shifts.green,
195f to shifts.cyan,
260f to shifts.blue,
295f to shifts.purple,
330f to shifts.magenta,
389f to shifts.red // 360 + 29
29f to shifts.red, 60f to shifts.orange, 105f to shifts.yellow,
140f to shifts.green, 195f to shifts.cyan, 260f to shifts.blue,
295f to shifts.purple, 330f to shifts.magenta
)
val n = targets.size
val normalizedHue = if (hue < 29f) hue + 360f else hue
// Find adjacent anchors and interpolate
for (i in 0 until targets.size - 1) {
val (h1, s1) = targets[i]
val (h2, s2) = targets[i + 1]
if (normalizedHue in h1..h2) {
val t = (normalizedHue - h1) / (h2 - h1)
return Shift(
hShift = s1.hShift * (1 - t) + s2.hShift * t,
sShift = s1.sShift * (1 - t) + s2.sShift * t,
lShift = s1.lShift * (1 - t) + s2.lShift * t
)
}
// Find the index of the anchor just before our hue
var i = 0
while (i < n) {
val h1 = if (i == 0) targets[0].first else targets[i].first
val h2 = if (i == n - 1) targets[0].first + 360f else targets[i+1].first
if (normalizedHue >= h1 && normalizedHue < h2) break
i++
}
return Shift()
if (i >= n) i = n - 1
// Catmull-Rom Spline Interpolation (4 points: p0, p1, p2, p3)
// This ensures the slope (tangent) is continuous at every anchor point.
val idx0 = (i - 1 + n) % n
val idx1 = i
val idx2 = (i + 1) % n
val idx3 = (i + 2) % n
val p0 = targets[idx0].second
val p1 = targets[idx1].second
val p2 = targets[idx2].second
val p3 = targets[idx3].second
val h1 = targets[idx1].first
var h2 = targets[idx2].first
if (h2 <= h1) h2 += 360f
val t = (normalizedHue - h1) / (h2 - h1)
fun catmullRom(v0: Float, v1: Float, v2: Float, v3: Float, t: Float): Float {
val t2 = t * t
val t3 = t2 * t
return 0.5f * (
(2f * v1) +
(-v0 + v2) * t +
(2f * v0 - 5f * v1 + 4f * v2 - v3) * t2 +
(-v0 + 3f * v1 - 3f * v2 + v3) * t3
)
}
return Shift(
hShift = catmullRom(p0.hShift, p1.hShift, p2.hShift, p3.hShift, t),
sScale = catmullRom(p0.sScale, p1.sScale, p2.sScale, p3.sScale, t),
lScale = catmullRom(p0.lScale, p1.lScale, p2.lScale, p3.lScale, t)
)
}
/**
@@ -34,10 +34,11 @@ data class ColorFeatures(
data class Tone(
val exposure: Float = 0f,
val contrast: Float = 0f,
val saturation: Float = 1f, // Multiplier: 1.0 is neutral, 0.0 is grayscale
val highlights: Float = 0f,
val shadows: Float = 0f,
val whitePoint: Float = 0f,
val blackPoint: Float = 0f
val whitePoint: Float = 1f, // Absolute: 1.0 is full white
val blackPoint: Float = 0f // Absolute: 0.0 is full black
)
@@ -86,9 +87,9 @@ data class HslShifts(
@Keep
data class Shift(
val hShift: Float = 0f, // Degrees
val sShift: Float = 0f,
val lShift: Float = 0f
val hShift: Float = 0f, // Offset in [-1.0, 1.0] where 1.0 is full rotation (360 deg)
val sScale: Float = 1f, // Multiplier: 1.0 is neutral
val lScale: Float = 1f // Multiplier: 1.0 is neutral
)
@@ -93,34 +93,39 @@ class OpenAIApiClient(
"You must fully reverse-engineer the image's look and feel, mapping its aesthetic to the following parameters. Return ONLY valid JSON matching this exact schema (no markdown blocks like ```json):\n" +
"{\n" +
" \"colorFeatures\": {\n" +
" \"tone\": { \"exposure\": 0.0, \"contrast\": 0.0, \"highlights\": 0.0, \"shadows\": 0.0, \"whitePoint\": 0.0, \"blackPoint\": 0.0 },\n" +
" \"tone\": { \"exposure\": 0.0, \"contrast\": 0.0, \"saturation\": 1.0, \"highlights\": 0.0, \"shadows\": 0.0, \"whitePoint\": 0.0, \"blackPoint\": 0.0 },\n" +
" \"balance\": { \"temperature\": 0.0, \"tint\": 0.0 },\n" +
" \"splitToning\": { \"shadows\": { \"hue\": 0.0, \"saturation\": 0.0 }, \"midtones\": { \"hue\": 0.0, \"saturation\": 0.0 }, \"highlights\": { \"hue\": 0.0, \"saturation\": 0.0 } },\n" +
" \"hslShifts\": { \"red\": { \"hShift\": 0.0, \"sShift\": 0.0, \"lShift\": 0.0 }, ... (orange, yellow, green, cyan, blue, purple, magenta) },\n" +
" \"curves\": { \"luma\": [], \"red\": [], \"green\": [], \"blue\": [] }\n" +
" \"hslShifts\": { \"red\": { \"hShift\": 0.0, \"sScale\": 1.0, \"lScale\": 1.0 }, ... (orange, yellow, green, cyan, blue, purple, magenta) },\n" +
" \"curves\": { \"luma\": [[0.0, 0.0], ...], \"red\": [], \"green\": [], \"blue\": [] }\n" +
" }\n" +
"}\n\n" +
"=== PARAMETER DEFINITIONS & STRICT RANGES ===\n" +
"1. TONE & BALANCE [-1.0 to 1.0]:\n" +
" - exposure: Global brightness shift (-1.0=darker, 1.0=brighter).\n" +
" - contrast: Macro contrast (S-curve intensity). Positive values increase contrast.\n" +
" - highlights/shadows: Recovery/compression. Negative highlights darken bright skies. Positive shadows lift dark areas (faded film look).\n" +
" - blackPoint/whitePoint: Lifts pure blacks (e.g., 0.1 for milky blacks) or drops pure whites.\n" +
" - temperature: -1.0(very cool/blue) to 1.0(very warm/orange).\n" +
" - tint: -1.0(greenish) to 1.0(magenta).\n\n" +
"1. TONE & BALANCE [-1.0 to 1.0 for most, 0.0 to 2.0 for saturation]:\n" +
" - exposure: Global Exposure Value (EV) compensation. -1.0 decreases exposure by 1 stop (50% light), 1.0 increases by 1 stop (200% light).\n" +
" - contrast: Macro contrast adjustment. Positive values create a non-linear S-curve to increase punch; negative values flatten the tone mapping.\n" +
" - saturation: Global color intensity multiplier [0.0 to 2.0]. 1.0 is neutral, 0.0 is grayscale.\n" +
" - highlights: Recovery/compression of the highlights. Negative values darken the brightest areas to recover details; positive values brighten them.\n" +
" - shadows: Adjustment of the shadow regions. Positive values lift the darkest areas (creating a 'faded' or 'milky' film look); negative values crush them.\n" +
" - blackPoint: The absolute target level for pure black [0.0 to 1.0] (Default: 0.0). e.g., 0.06 lifts absolute black to 6%% grey.\n" +
" - whitePoint: The absolute target level for pure white [0.0 to 1.0] (Default: 1.0). e.g., 0.94 compresses peak white to 94%% grey.\n" +
" - temperature: -1.0(cool/blue) to 1.0(warm/orange). Minimize global shifts to avoid muddying the image.\n" +
" - tint: -1.0(greenish) to 1.0(magenta). Use sparingly.\n" +
" * Note: Use either WP/BP or Curves to map black/white levels; avoid double-processing to prevent extreme contrast loss.\n\n" +
"2. SPLIT TONING (Cinematic Color Grading):\n" +
" - hue: Exact color wheel angle [0.0 to 360.0]. (e.g., 210 for teal shadows, 45 for orange highlights).\n" +
" - saturation: Intensity of the color tint [0.0 to 1.0]. Keep shadows/highlights vibrant (0.1-0.3) for cinematic looks. Keep midtones low (0.0-0.1) to avoid washing out the image.\n\n" +
"3. HSL SHIFTS [-1.0 to 1.0]:\n" +
" - hShift (Hue): Skews the color towards neighbors.\n" +
" - sShift (Saturation): Desaturates (-1.0) or super-saturates (1.0) that specific color bucket.\n" +
" - lShift (Lightness): Darkens or brightens that specific color.\n" +
" * Use this to isolate colors (e.g., deeply desaturate greens, boost orange/red for skin tones).\n\n" +
" - saturation: Intensity of the color tint [0.0 to 1.0]. Keep shadows/highlights vibrant (0.1-0.3) for cinematic looks. STRONGLY MINIMIZE midtones saturation (0.0 to 0.05) to preserve natural skin tones and avoid an unnatural global color cast.\n\n" +
"3. HSL SHIFTS:\n" +
" - hShift (Hue): Fraction of a full color wheel rotation [-1.0 to 1.0]. 1.0 = 360°. (e.g., 0.1 skews slightly towards neighbors).\n" +
" - sScale (Saturation): Multiplier for color intensity [0.0 to 2.0]. 1.0 is neutral, 0.0 is grayscale.\n" +
" - lScale (Lightness): Multiplier for color brightness [0.0 to 2.0]. 1.0 is neutral.\n" +
" * PREFERENCE: Prioritize HSL adjustments over global white balance (Temperature/Tint) to capture specific film-like color signatures without destroying the overall color integrity.\n" +
" * INFERENCE: Identify specific scene elements (e.g., the exact blue/cyan of the sky, the green/yellow of foliage, or skin tones). Reverse-engineer the color offsets by comparing these observed colors to standard real-world expectations. Determine the precise HSL shifts for each hue bucket to replicate these characteristic transformations.\n\n" +
"4. CURVES (Spline Points):\n" +
" - Arrays of exactly 2D [x, y] coordinates mapping input to output [0.0 to 1.0].\n" +
" - luma: The master contrast/fade curve. For a classic faded film look, start with [[0.0, 0.15], [0.5, 0.5], [1.0, 0.95]].\n" +
" - rgb: Individual color channel curves for advanced cross-processing.\n\n" +
"CRITICAL: ALL values must strictly adhere to these mathematical float ratios. NEVER use integer percentages (like 15.0 or -20.0) except for 'hue' degrees. Exceeding [-1.0, 1.0] for ratios or [0.0, 1.0] for curves will crash the rendering engine."
"CRITICAL: ALL values must strictly adhere to these mathematical float ratios. NEVER use integer percentages (like 15.0 or -20.0) except for 'hue' degrees. Exceeding [-1.0, 1.0] for ratios or [0.0, 1.0] for curves will crash the rendering engine. Ensure all parameters collaborate harmoniously; avoid compounding shifts (e.g., overlapping Tint, Split Toning, and HSL shifts) that result in severe color clipping or unnatural color bias."
)
}
val userMessage = JSONObject().apply {