feat: color palette refactor

This commit is contained in:
zhoubinjia committed 2026-07-29 16:28:35 +08:00
1 parent 98e48e660e
commit ba67a18bb4
21 files changed
+761 -277

No files matched your search

@@ -1,5 +1,6 @@
package com.hinnka.mycamera.lut
import android.content.Context
import android.graphics.Bitmap
import com.hinnka.mycamera.lut.creator.LutGenerator
import com.hinnka.mycamera.model.ColorRecipeParams
@@ -10,6 +11,7 @@ object BakedLutExporter {
private const val HALD_LEVEL = 8
suspend fun exportBakedCube(
context: Context,
lutConfig: LutConfig,
recipe: ColorRecipeParams,
name: String
@@ -23,7 +25,7 @@ object BakedLutExporter {
.toByteArray(Charsets.UTF_8)
}
val imageProcessor = LutImageProcessor()
val imageProcessor = LutImageProcessor(context)
return try {
val clutBitmap = createIdentityUnwrappedClutBitmap(UNWRAPPED_CLUT_SIZE)
val processedBitmap = imageProcessor.applyLut(
@@ -41,10 +43,11 @@ object BakedLutExporter {
}
suspend fun exportBakedHaldPng(
context: Context,
lutConfig: LutConfig,
recipe: ColorRecipeParams
): ByteArray? {
val imageProcessor = LutImageProcessor()
val imageProcessor = LutImageProcessor(context)
return try {
val haldBitmap = createIdentityHaldBitmap(HALD_LEVEL)
val processedBitmap = imageProcessor.applyLut(
@@ -0,0 +1,187 @@
package com.hinnka.mycamera.lut
import android.content.Context
import android.opengl.GLES30
import com.hinnka.mycamera.color.TransferCurve
import com.hinnka.mycamera.raw.ColorSpace
import com.hinnka.mycamera.utils.PLog
import java.nio.ByteBuffer
import java.nio.ByteOrder
import kotlin.math.abs
/**
* Oplus GR 影调轴的 Low Key / High Key 两个端点 LUT。
*
* 端点来自 SC 表中饱和度为 0 的 SC32Lut3(Cont=-100)和 SC32Lut5(Cont=+100)。
* 原始文件是 32³、RGB 三平面、little-endian float32。加载时转换为 OpenGL 所需的
* RGB 交错顺序;R 是纹理 X 轴、G 是 Y 轴、B 是 Z 轴。
*/
internal object BasicToneLut {
const val SIZE = 32
enum class Endpoint(
val assetPath: String,
val title: String,
) {
LOW_KEY(
assetPath = "internal/basic_tone/low_key_32f.bin",
title = "BasicTone Low Key",
),
HIGH_KEY(
assetPath = "internal/basic_tone/high_key_32f.bin",
title = "BasicTone High Key",
),
}
@Volatile
private var lowKeyConfig: LutConfig? = null
@Volatile
private var highKeyConfig: LutConfig? = null
fun load(context: Context, endpoint: Endpoint): LutConfig {
val cached = when (endpoint) {
Endpoint.LOW_KEY -> lowKeyConfig
Endpoint.HIGH_KEY -> highKeyConfig
}
if (cached != null) return cached
return synchronized(this) {
val synchronizedCached = when (endpoint) {
Endpoint.LOW_KEY -> lowKeyConfig
Endpoint.HIGH_KEY -> highKeyConfig
}
synchronizedCached ?: loadFromAssets(context.applicationContext, endpoint).also {
when (endpoint) {
Endpoint.LOW_KEY -> lowKeyConfig = it
Endpoint.HIGH_KEY -> highKeyConfig = it
}
}
}
}
private fun loadFromAssets(context: Context, endpoint: Endpoint): LutConfig {
val raw = context.assets.open(endpoint.assetPath).use { it.readBytes() }
return LutConfig(
size = SIZE,
data = parsePlanarFloat32(raw, SIZE, endpoint.title),
title = endpoint.title,
configDataType = LutConfig.CONFIG_DATA_TYPE_UINT16,
curve = TransferCurve.SRGB,
colorSpace = ColorSpace.SRGB,
)
}
internal fun parsePlanarFloat32(
raw: ByteArray,
size: Int,
title: String = "BasicTone",
): FloatArray {
val voxelCount = size * size * size
val expectedBytes = voxelCount * CHANNEL_COUNT * Float.SIZE_BYTES
require(raw.size == expectedBytes) {
"Invalid $title LUT size: ${raw.size}, expected $expectedBytes"
}
val planar = ByteBuffer.wrap(raw)
.order(ByteOrder.LITTLE_ENDIAN)
.asFloatBuffer()
return FloatArray(voxelCount * CHANNEL_COUNT).also { interleaved ->
for (index in 0 until voxelCount) {
val output = index * CHANNEL_COUNT
interleaved[output] = planar.get(index)
interleaved[output + 1] = planar.get(voxelCount + index)
interleaved[output + 2] = planar.get(voxelCount * 2 + index)
}
}
}
private const val CHANNEL_COUNT = 3
}
/**
* 每个 GL context 持有一组端点纹理。符号只决定绑定哪张 LUT,绝对值作为应用强度。
*/
internal class BasicToneGlTextures {
private var lowKeyTextureId = 0
private var highKeyTextureId = 0
fun bind(
context: Context?,
textureUnit: Int,
samplerLocation: Int,
intensityLocation: Int,
amount: Float,
) {
ensureCreated(context)
val textureId = if (amount >= 0f) highKeyTextureId else lowKeyTextureId
val intensity = if (textureId != 0) abs(amount).coerceIn(0f, 1f) else 0f
GLES30.glActiveTexture(GLES30.GL_TEXTURE0 + textureUnit)
GLES30.glBindTexture(GLES30.GL_TEXTURE_3D, textureId)
if (samplerLocation >= 0) {
GLES30.glUniform1i(samplerLocation, textureUnit)
}
if (intensityLocation >= 0) {
GLES30.glUniform1f(intensityLocation, intensity)
}
}
fun reset() {
lowKeyTextureId = 0
highKeyTextureId = 0
}
fun release() {
val textures = intArrayOf(lowKeyTextureId, highKeyTextureId).filter { it != 0 }.toIntArray()
if (textures.isNotEmpty()) {
GLES30.glDeleteTextures(textures.size, textures, 0)
}
reset()
}
private fun ensureCreated(context: Context?) {
if (lowKeyTextureId != 0 && highKeyTextureId != 0) return
if (context == null) return
try {
if (lowKeyTextureId == 0) {
lowKeyTextureId = GlUtils.create3DTexture(
BasicToneLut.load(context, BasicToneLut.Endpoint.LOW_KEY)
)
}
if (highKeyTextureId == 0) {
highKeyTextureId = GlUtils.create3DTexture(
BasicToneLut.load(context, BasicToneLut.Endpoint.HIGH_KEY)
)
}
} catch (error: Exception) {
PLog.e(TAG, "Failed to create BasicTone endpoint textures", error)
release()
}
}
private companion object {
const val TAG = "BasicToneGlTextures"
}
}
internal object BasicToneLutShader {
val GLSL = """
uniform mediump sampler3D uBasicToneLut;
uniform float uBasicToneIntensity;
vec3 applyBasicToneLut(vec3 color) {
float intensity = clamp(uBasicToneIntensity, 0.0, 1.0);
if (intensity < 0.001) {
return color;
}
const float lutSize = 32.0;
float scale = (lutSize - 1.0) / lutSize;
float offset = 1.0 / (2.0 * lutSize);
vec3 coordinate = clamp(color, 0.0, 1.0) * scale + offset;
vec3 endpointColor = texture(uBasicToneLut, coordinate).rgb;
return mix(color, endpointColor, intensity);
}
""".trimIndent()
}
@@ -32,6 +32,8 @@ internal data class ColorPassLocations(
val uToneToeLocation: Int,
val uToneShoulderLocation: Int,
val uTonePivotLocation: Int,
val uBasicToneLutLocation: Int,
val uBasicToneIntensityLocation: Int,
val uFilmGrainLocation: Int,
val uVignetteLocation: Int,
val uBleachBypassLocation: Int,
@@ -126,6 +128,8 @@ internal class PreviewColorProgramCache {
uToneToeLocation = GLES30.glGetUniformLocation(program, "uToneToe"),
uToneShoulderLocation = GLES30.glGetUniformLocation(program, "uToneShoulder"),
uTonePivotLocation = GLES30.glGetUniformLocation(program, "uTonePivot"),
uBasicToneLutLocation = GLES30.glGetUniformLocation(program, "uBasicToneLut"),
uBasicToneIntensityLocation = GLES30.glGetUniformLocation(program, "uBasicToneIntensity"),
uFilmGrainLocation = GLES30.glGetUniformLocation(program, "uFilmGrain"),
uVignetteLocation = GLES30.glGetUniformLocation(program, "uVignette"),
uBleachBypassLocation = GLES30.glGetUniformLocation(program, "uBleachBypass"),
@@ -56,6 +56,8 @@ data class LutStackRenderResult(
* 所有 GPU 操作在独立单线程完成,确保 EGL 上下文线程安全
*/
class LutImageProcessor(context: Context? = null) {
private val appContext = context?.applicationContext
@Volatile
private var glThread: Thread? = null
@@ -77,6 +79,7 @@ class LutImageProcessor(context: Context? = null) {
private var shaderProgram = 0
private var imageTextureId = 0
private var lutTextureId = 0
private val basicToneTextures = BasicToneGlTextures()
private var framebufferId = 0
private var outputTextureId = 0
private var outputFramebufferWidth = 0
@@ -1120,6 +1123,14 @@ class LutImageProcessor(context: Context? = null) {
GLES30.glUniform1i(GLES30.glGetUniformLocation(program, "uCurveTexture"), 3)
GLES30.glUniform1i(GLES30.glGetUniformLocation(program, "uCurveEnabled"), if (curveActive) 1 else 0)
basicToneTextures.bind(
context = appContext,
textureUnit = 6,
samplerLocation = GLES30.glGetUniformLocation(program, "uBasicToneLut"),
intensityLocation = GLES30.glGetUniformLocation(program, "uBasicToneIntensity"),
amount = effectiveRecipeParams?.let(ColorPaletteMapper::basicToneAmount) ?: 0f,
)
// 设置 HDF 参数
GLES30.glUniform1f(GLES30.glGetUniformLocation(program, "uHalation"), halation)
if (halation > 0f) {
@@ -3310,6 +3321,7 @@ class LutImageProcessor(context: Context? = null) {
if (lutTextureId != 0) {
GLES30.glDeleteTextures(1, intArrayOf(lutTextureId), 0)
}
basicToneTextures.release()
releaseOutputFramebuffer()
releaseNaturalLightFramebuffer()
releaseNaturalLightOutputFramebuffer()
@@ -3575,6 +3587,7 @@ class LutImageProcessor(context: Context? = null) {
${PreviewColorShaderModules.HLG_TO_LINEAR}
${PreviewColorShaderModules.EXPOSURE}
${PreviewColorShaderModules.SANITIZE}
${BasicToneLutShader.GLSL}
vec3 prepareToneSample(vec3 sampleColor) {
vec3 prepared = sampleColor;
@@ -3744,6 +3757,10 @@ class LutImageProcessor(context: Context? = null) {
color.rgb = applyToneCurve(color.rgb, uToneToe, uToneShoulder, uTonePivot);
color.rgb = sanitizeColor(color.rgb);
// 3.6. Oplus BasicTone 端点 LUT(符号在 CPU 侧选端点,绝对值控制强度)
color.rgb = applyBasicToneLut(color.rgb);
color.rgb = sanitizeColor(color.rgb);
// 4. 白平衡调整(色温 + 色调)
color.r += uTemperature * 0.1;
color.b -= uTemperature * 0.1;
@@ -7,6 +7,7 @@ import android.graphics.SurfaceTexture
import android.opengl.*
import com.hinnka.mycamera.livephoto.LivePhotoRecorder
import com.hinnka.mycamera.livephoto.resolveLivePhotoRotationDegrees
import com.hinnka.mycamera.model.ColorPaletteMapper
import com.hinnka.mycamera.raw.ColorSpace
import com.hinnka.mycamera.raw.HncsFilmCurveMode
import com.hinnka.mycamera.raw.HncsNaturalLightGl
@@ -47,6 +48,7 @@ enum class PreviewCaptureSource {
}
class LutRenderer(context: Context) : GLSurfaceView.Renderer {
private val appContext = context.applicationContext
companion object {
private const val TAG = "LutRenderer"
@@ -86,6 +88,7 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
private var cameraTextureId: Int = 0
private var lutTextureId: Int = 0
private var baselineLutTextureId: Int = 0
private val basicToneTextures = BasicToneGlTextures()
// 缓冲区
private var vertexBufferId: Int = 0
@@ -400,6 +403,15 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
@Volatile
var tonePivot: Float = 0f // -1.0 ~ +1.0
@Volatile
var paletteX: Float = 0.5f
@Volatile
var paletteY: Float = 0.5f
@Volatile
var paletteDensity: Float = 1f
@Volatile
var filmGrain: Float = 0f // 0.0 ~ 1.0
@@ -551,6 +563,7 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
cameraTextureId = 0
lutTextureId = 0
baselineLutTextureId = 0
basicToneTextures.reset()
vertexBufferId = 0
texCoordBufferId = 0
indexBufferId = 0
@@ -1456,6 +1469,14 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
GLES30.glUniform1i(locations.uCurveTextureLocation, 2)
GLES30.glUniform1i(locations.uCurveEnabledLocation, if (curveEnabled && curveTextureId != 0) 1 else 0)
basicToneTextures.bind(
context = appContext,
textureUnit = 3,
samplerLocation = locations.uBasicToneLutLocation,
intensityLocation = locations.uBasicToneIntensityLocation,
amount = ColorPaletteMapper.basicToneAmount(params),
)
GLES30.glUniform1f(locations.uApertureLocation, apertureOverride)
val fp = focusPointOverride ?: PointF(0.5f, 0.5f)
GLES30.glUniform2f(locations.uFocusPointLocation, fp.x, 1.0f - fp.y)
@@ -3832,6 +3853,7 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
GLES30.glDeleteTextures(1, intArrayOf(stackTextureId), 0)
stackTextureId = 0
}
basicToneTextures.release()
recordFboWidth = 0
recordFboHeight = 0
@@ -3872,6 +3894,9 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
toneToe = toneToe,
toneShoulder = toneShoulder,
tonePivot = tonePivot,
paletteX = paletteX,
paletteY = paletteY,
paletteDensity = paletteDensity,
fade = fade,
filmGrain = filmGrain,
vignette = vignette,
@@ -3951,6 +3976,9 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
toneToe = params.toneToe
toneShoulder = params.toneShoulder
tonePivot = params.tonePivot
paletteX = params.paletteX
paletteY = params.paletteY
paletteDensity = params.paletteDensity
filmGrain = params.filmGrain
vignette = params.vignette
bleachBypass = params.bleachBypass
@@ -74,6 +74,7 @@ internal object PreviewColorShader {
${if (variant.includeHlgInput) PreviewColorShaderModules.HLG_TO_LINEAR else PreviewColorShaderModules.HLG_TO_LINEAR_STUB}
${PreviewColorShaderModules.EXPOSURE}
${PreviewColorShaderModules.SANITIZE}
${BasicToneLutShader.GLSL}
float getLuma(vec3 color) {
return dot(color, W);
@@ -181,6 +182,9 @@ internal object PreviewColorShader {
color.rgb = applyToneCurve(color.rgb, uToneToe, uToneShoulder, uTonePivot);
color.rgb = sanitizeColor(color.rgb);
color.rgb = applyBasicToneLut(color.rgb);
color.rgb = sanitizeColor(color.rgb);
color.r += uTemperature * 0.1;
color.b -= uTemperature * 0.1;
color.g -= uTint * 0.05;
@@ -8,6 +8,7 @@ import androidx.media3.common.util.UnstableApi
import androidx.media3.effect.BaseGlShaderProgram
import androidx.media3.effect.GlEffect
import androidx.media3.effect.GlShaderProgram
import com.hinnka.mycamera.model.ColorPaletteMapper
import com.hinnka.mycamera.model.ColorRecipeParams
import com.hinnka.mycamera.utils.PLog
import java.nio.ByteBuffer
@@ -19,8 +20,11 @@ import java.nio.ByteOrder
@UnstableApi
class VideoLutEffect(
@Volatile var lutConfig: LutConfig?,
@Volatile var recipeParams: ColorRecipeParams?
recipeParams: ColorRecipeParams?
) : GlEffect {
@Volatile
var recipeParams: ColorRecipeParams? = recipeParams?.let(ColorPaletteMapper::mergeIntoEffectiveParams)
private set
private var shaderProgram: VideoLutShaderProgram? = null
@@ -37,7 +41,7 @@ class VideoLutEffect(
fun update(lutConfig: LutConfig?, recipeParams: ColorRecipeParams?) {
PLog.d("VideoLutEffect", "update called, lutConfig: ${lutConfig?.title}, recipeParams: ${recipeParams != null}")
this.lutConfig = lutConfig
this.recipeParams = recipeParams
this.recipeParams = recipeParams?.let(ColorPaletteMapper::mergeIntoEffectiveParams)
shaderProgram?.triggerUpdate()
}
}
@@ -182,6 +186,8 @@ private class VideoLutShaderProgram(
);
}
${BasicToneLutShader.GLSL}
float applyToneCurveToLuma(float luma, float toe, float shoulder, float pivot) {
float safeLuma = clamp(luma, 0.0, 1.0);
float pivotPoint = clamp(0.5 + pivot * 0.12, 0.2, 0.8);
@@ -550,6 +556,9 @@ private class VideoLutShaderProgram(
color.rgb = applyToneCurve(color.rgb, uToneToe, uToneShoulder, uTonePivot);
color.rgb = sanitizeColor(color.rgb);
color.rgb = applyBasicToneLut(color.rgb);
color.rgb = sanitizeColor(color.rgb);
color.r += uTemperature * 0.1;
color.b -= uTemperature * 0.1;
color.g -= uTint * 0.05;
@@ -698,6 +707,7 @@ private class VideoLutShaderProgram(
private var lutTextureId = 0
private var curveTextureId = 0
private val basicToneTextures = BasicToneGlTextures()
private var lastLutConfig: LutConfig? = null
private var lastRecipeParams: ColorRecipeParams? = null
@@ -828,6 +838,14 @@ private class VideoLutShaderProgram(
GLES30.glUniform1i(GLES30.glGetUniformLocation(programId, "uCurveEnabled"), 0)
}
basicToneTextures.bind(
context = context.applicationContext,
textureUnit = 2,
samplerLocation = GLES30.glGetUniformLocation(programId, "uBasicToneLut"),
intensityLocation = GLES30.glGetUniformLocation(programId, "uBasicToneIntensity"),
amount = currentRecipeParams?.let(ColorPaletteMapper::basicToneAmount) ?: 0f,
)
// 设置色彩配方 uniforms
val colorRecipeEnabled = currentRecipeParams != null && !currentRecipeParams.isDefault()
GLES30.glUniform1i(GLES30.glGetUniformLocation(programId, "uColorRecipeEnabled"), if (colorRecipeEnabled) 1 else 0)
@@ -999,6 +1017,7 @@ private class VideoLutShaderProgram(
super.release()
deleteLutTexture()
deleteCurveTexture()
basicToneTextures.release()
if (positionVbo != 0) {
GLES30.glDeleteBuffers(1, intArrayOf(positionVbo), 0)
positionVbo = 0
@@ -43,7 +43,7 @@ class LutSynthesisViewModel(application: Application) : AndroidViewModel(applica
private val importManager = CustomImportManager(application)
private val lutManager = LutManager(application)
private val imageProcessor = LutImageProcessor()
private val imageProcessor = LutImageProcessor(application)
// 状态流
private val _layers = MutableStateFlow<List<LutSynthesisLayer>>(emptyList())
@@ -1,17 +1,18 @@
package com.hinnka.mycamera.model
import kotlin.math.abs
import kotlin.math.max
/**
* 在“调色盘交互状态”和现有色彩配方参数之间做单向/近似双向映射。
* 把二维调色盘转换为现有渲染参数。
*
* 第一版策略:
* 1. x/y 先定义目标风格轮廓。
* 2. density 再把目标风格从 neutral 混合到目标值。
* 3. 仅覆盖色彩/影调相关参数,保留纹理、镜头、备注、LUT 强度等其它字段。
* 调色盘对外显示 -100..+100,并保持连续输入:
*
* - 饱和度线性对应 0.4..1.6 倍,中心 0 对应 1.0。
* - 影调直接归一化为 -1..+1 的 BasicTone 强度:负值使用 Low Key 端点,正值使用 High Key 端点。
*
* - 两条轴彼此独立,不再隐式改变色温、色相、LCH 或曝光参数。
*/
object ColorPaletteMapper {
private const val SATURATION_OFFSET_AT_MAX = 0.6f
fun updatePaletteState(
base: ColorRecipeParams,
paletteState: ColorPaletteState
@@ -26,104 +27,17 @@ object ColorPaletteMapper {
fun buildPaletteContribution(paletteState: ColorPaletteState): ColorRecipeParams {
val state = paletteState.normalized()
val xBias = (state.x - 0.5f) * 2f
val toneBias = 1f - state.y * 2f
val density = state.density
val warmBias = smoothSignedBias(xBias)
val coolBias = smoothSignedBias(-xBias)
val vividness = positiveBias(xBias)
val mutedness = positiveBias(-xBias)
val highKey = positiveBias(toneBias)
val lowKey = positiveBias(-toneBias)
val targetExposure = RecipeParam.EXPOSURE.clamp(
highKey * 0.4f - lowKey * 0.4f
)
val targetContrast = RecipeParam.CONTRAST.clamp(
1f - highKey * 0.08f + lowKey * 0.18f
)
val targetHighlights = RecipeParam.HIGHLIGHTS.clamp(
highKey * 0.22f - lowKey * 0.18f
)
val targetShadows = RecipeParam.SHADOWS.clamp(
highKey * 0.22f - lowKey * 0.32f
)
val targetToneToe = clampLch(highKey * 0.50f - lowKey * 0.56f)
val targetToneShoulder = clampLch(highKey * 0.34f - lowKey * 0.48f)
val targetTonePivot = clampLch(highKey * 0.14f - lowKey * 0.10f)
val targetVibrance = RecipeParam.COLOR.clamp(
vividness * 0.42f - mutedness * 0.28f
)
val targetSaturation = RecipeParam.SATURATION.clamp(
1f + vividness * 0.06f - mutedness * 0.04f
)
val targetFade = RecipeParam.FADE.clamp(
mutedness * 0.14f + highKey * 0.08f - lowKey * 0.04f
)
val globalChromaLift = vividness * 0.24f - mutedness * 0.10f
val globalLightnessLift = highKey * 0.24f - lowKey * 0.22f
fun lerpNeutral(defaultValue: Float, targetValue: Float): Float {
return defaultValue + (targetValue - defaultValue) * density
}
fun chromaWeight(
warmAffinity: Float,
coolAffinity: Float,
neutralAffinity: Float = 0f
): Float {
return globalChromaLift +
warmBias * warmAffinity * 0.18f -
coolBias * coolAffinity * 0.16f +
mutedness * neutralAffinity * -0.08f
}
fun lightnessWeight(
brightAffinity: Float,
darkAffinity: Float,
warmAffinity: Float = 0f,
coolAffinity: Float = 0f
): Float {
return globalLightnessLift * brightAffinity -
lowKey * darkAffinity * 0.10f +
warmBias * warmAffinity * 0.04f -
coolBias * coolAffinity * 0.04f
}
val saturationFactor =
1f +
state.saturationValue / ColorPaletteState.AXIS_MAX *
SATURATION_OFFSET_AT_MAX *
state.density
return ColorRecipeParams.DEFAULT.copy(
exposure = RecipeParam.EXPOSURE.clamp(lerpNeutral(RecipeParam.EXPOSURE.defaultValue, targetExposure)),
contrast = RecipeParam.CONTRAST.clamp(lerpNeutral(RecipeParam.CONTRAST.defaultValue, targetContrast)),
saturation = RecipeParam.SATURATION.clamp(lerpNeutral(RecipeParam.SATURATION.defaultValue, targetSaturation)),
fade = RecipeParam.FADE.clamp(lerpNeutral(RecipeParam.FADE.defaultValue, targetFade)),
color = RecipeParam.COLOR.clamp(lerpNeutral(RecipeParam.COLOR.defaultValue, targetVibrance)),
highlights = RecipeParam.HIGHLIGHTS.clamp(lerpNeutral(RecipeParam.HIGHLIGHTS.defaultValue, targetHighlights)),
shadows = RecipeParam.SHADOWS.clamp(lerpNeutral(RecipeParam.SHADOWS.defaultValue, targetShadows)),
toneToe = clampLch(lerpNeutral(0f, targetToneToe)),
toneShoulder = clampLch(lerpNeutral(0f, targetToneShoulder)),
tonePivot = clampLch(lerpNeutral(0f, targetTonePivot)),
saturation = RecipeParam.SATURATION.clamp(saturationFactor),
paletteX = state.x,
paletteY = state.y,
paletteDensity = state.density,
skinChroma = clampLch(lerpNeutral(0f, chromaWeight(warmAffinity = 0.45f, coolAffinity = 0.08f, neutralAffinity = 0.10f))),
redChroma = clampLch(lerpNeutral(0f, chromaWeight(warmAffinity = 0.78f, coolAffinity = 0.06f))),
orangeChroma = clampLch(lerpNeutral(0f, chromaWeight(warmAffinity = 0.88f, coolAffinity = 0.04f))),
yellowChroma = clampLch(lerpNeutral(0f, chromaWeight(warmAffinity = 0.64f, coolAffinity = 0.08f))),
greenChroma = clampLch(lerpNeutral(0f, chromaWeight(warmAffinity = 0.18f, coolAffinity = 0.26f, neutralAffinity = 0.06f))),
cyanChroma = clampLch(lerpNeutral(0f, chromaWeight(warmAffinity = 0.06f, coolAffinity = 0.54f))),
blueChroma = clampLch(lerpNeutral(0f, chromaWeight(warmAffinity = 0.04f, coolAffinity = 0.74f))),
purpleChroma = clampLch(lerpNeutral(0f, chromaWeight(warmAffinity = 0.20f, coolAffinity = 0.50f))),
magentaChroma = clampLch(lerpNeutral(0f, chromaWeight(warmAffinity = 0.34f, coolAffinity = 0.32f))),
skinLightness = clampLch(lerpNeutral(0f, lightnessWeight(brightAffinity = 0.24f, darkAffinity = 0.10f, warmAffinity = 0.20f))),
redLightness = clampLch(lerpNeutral(0f, lightnessWeight(brightAffinity = 0.18f, darkAffinity = 0.16f, warmAffinity = 0.12f))),
orangeLightness = clampLch(lerpNeutral(0f, lightnessWeight(brightAffinity = 0.26f, darkAffinity = 0.10f, warmAffinity = 0.22f))),
yellowLightness = clampLch(lerpNeutral(0f, lightnessWeight(brightAffinity = 0.42f, darkAffinity = 0.08f, warmAffinity = 0.16f))),
greenLightness = clampLch(lerpNeutral(0f, lightnessWeight(brightAffinity = 0.16f, darkAffinity = 0.18f, coolAffinity = 0.12f))),
cyanLightness = clampLch(lerpNeutral(0f, lightnessWeight(brightAffinity = 0.18f, darkAffinity = 0.16f, coolAffinity = 0.16f))),
blueLightness = clampLch(lerpNeutral(0f, lightnessWeight(brightAffinity = 0.12f, darkAffinity = 0.24f, coolAffinity = 0.20f))),
purpleLightness = clampLch(lerpNeutral(0f, lightnessWeight(brightAffinity = 0.12f, darkAffinity = 0.22f, coolAffinity = 0.14f))),
magentaLightness = clampLch(lerpNeutral(0f, lightnessWeight(brightAffinity = 0.14f, darkAffinity = 0.20f, warmAffinity = 0.08f, coolAffinity = 0.08f))),
paletteDensity = state.density
)
}
@@ -143,49 +57,32 @@ object ColorPaletteMapper {
minValue: Float,
maxValue: Float
): Float {
return (manualValue + (paletteValue - defaultValue)).coerceIn(minValue, maxValue)
return (manualValue + paletteValue - defaultValue).coerceIn(minValue, maxValue)
}
fun combineZeroDefault(
manualValue: Float,
paletteValue: Float
): Float = (manualValue + paletteValue).coerceIn(-1f, 1f)
return manualParams.copy(
exposure = combine(manualParams.exposure, paletteContribution.exposure, 0f, -2f, 2f),
contrast = combine(manualParams.contrast, paletteContribution.contrast, 1f, 0.5f, 1.5f),
saturation = combine(manualParams.saturation, paletteContribution.saturation, 1f, 0f, 2f),
fade = combine(manualParams.fade, paletteContribution.fade, 0f, 0f, 1f),
color = combine(manualParams.color, paletteContribution.color, 0f, -1f, 1f),
highlights = combine(manualParams.highlights, paletteContribution.highlights, 0f, -1f, 1f),
shadows = combine(manualParams.shadows, paletteContribution.shadows, 0f, -1f, 1f),
toneToe = combine(manualParams.toneToe, paletteContribution.toneToe, 0f, -1f, 1f),
toneShoulder = combine(manualParams.toneShoulder, paletteContribution.toneShoulder, 0f, -1f, 1f),
tonePivot = combine(manualParams.tonePivot, paletteContribution.tonePivot, 0f, -1f, 1f),
skinChroma = combineZeroDefault(manualParams.skinChroma, paletteContribution.skinChroma),
redChroma = combineZeroDefault(manualParams.redChroma, paletteContribution.redChroma),
orangeChroma = combineZeroDefault(manualParams.orangeChroma, paletteContribution.orangeChroma),
yellowChroma = combineZeroDefault(manualParams.yellowChroma, paletteContribution.yellowChroma),
greenChroma = combineZeroDefault(manualParams.greenChroma, paletteContribution.greenChroma),
cyanChroma = combineZeroDefault(manualParams.cyanChroma, paletteContribution.cyanChroma),
blueChroma = combineZeroDefault(manualParams.blueChroma, paletteContribution.blueChroma),
purpleChroma = combineZeroDefault(manualParams.purpleChroma, paletteContribution.purpleChroma),
magentaChroma = combineZeroDefault(manualParams.magentaChroma, paletteContribution.magentaChroma),
skinLightness = combineZeroDefault(manualParams.skinLightness, paletteContribution.skinLightness),
redLightness = combineZeroDefault(manualParams.redLightness, paletteContribution.redLightness),
orangeLightness = combineZeroDefault(manualParams.orangeLightness, paletteContribution.orangeLightness),
yellowLightness = combineZeroDefault(manualParams.yellowLightness, paletteContribution.yellowLightness),
greenLightness = combineZeroDefault(manualParams.greenLightness, paletteContribution.greenLightness),
cyanLightness = combineZeroDefault(manualParams.cyanLightness, paletteContribution.cyanLightness),
blueLightness = combineZeroDefault(manualParams.blueLightness, paletteContribution.blueLightness),
purpleLightness = combineZeroDefault(manualParams.purpleLightness, paletteContribution.purpleLightness),
magentaLightness = combineZeroDefault(manualParams.magentaLightness, paletteContribution.magentaLightness),
saturation = combine(
manualValue = manualParams.saturation,
paletteValue = paletteContribution.saturation,
defaultValue = RecipeParam.SATURATION.defaultValue,
minValue = RecipeParam.SATURATION.minValue,
maxValue = RecipeParam.SATURATION.maxValue
)
)
}
/**
* 用当前参数近似反推调色盘落点,保证首次进入不会总是从中心跳变。
*/
fun basicToneAmount(params: ColorRecipeParams): Float {
return basicToneAmount(deriveFromParams(params))
}
fun basicToneAmount(paletteState: ColorPaletteState): Float {
val state = paletteState.normalized()
return (
state.toneValue / ColorPaletteState.AXIS_MAX *
state.density
).coerceIn(-1f, 1f)
}
fun deriveFromParams(params: ColorRecipeParams): ColorPaletteState {
return ColorPaletteState(
x = params.paletteX,
@@ -193,18 +90,4 @@ object ColorPaletteMapper {
density = params.paletteDensity
).normalized()
}
private fun smoothSignedBias(value: Float): Float {
return value * abs(value) * (2f - abs(value))
}
private fun positiveBias(value: Float): Float {
val clamped = value.coerceIn(0f, 1f)
return clamped * clamped * (2f - clamped)
}
private fun clampLch(value: Float): Float {
return value.coerceIn(-1f, 1f)
}
}
@@ -3,8 +3,8 @@ package com.hinnka.mycamera.model
/**
* 调色盘交互状态。
*
* x: 0..1,左侧更灰、更淡;右侧更纯、更鲜。
* y: 0..1,上侧更高调、更柔和;下侧更低调、更扎实。
* x: 0..1,连续对应饱和度 -100..+100。
* y: 0..1,连续对应影调 +100..-100(BasicTone High Key 到 Low Key)。
* density: 0..1,调色盘效果施加浓度。
*/
data class ColorPaletteState(
@@ -12,6 +12,12 @@ data class ColorPaletteState(
val y: Float = 0.5f,
val density: Float = 1f,
) {
val saturationValue: Float
get() = positionToValue(x)
val toneValue: Float
get() = -positionToValue(y)
fun normalized(): ColorPaletteState {
return copy(
x = x.coerceIn(0f, 1f),
@@ -20,7 +26,29 @@ data class ColorPaletteState(
)
}
fun withValues(
saturation: Float = saturationValue,
tone: Float = toneValue
): ColorPaletteState {
return copy(
x = valueToPosition(saturation),
y = valueToPosition(-tone)
)
}
companion object {
const val AXIS_MIN = -100f
const val AXIS_MAX = 100f
val DEFAULT = ColorPaletteState()
fun positionToValue(position: Float): Float {
return AXIS_MIN + position.coerceIn(0f, 1f) * (AXIS_MAX - AXIS_MIN)
}
fun valueToPosition(value: Float): Float {
val clamped = value.coerceIn(AXIS_MIN, AXIS_MAX)
return (clamped - AXIS_MIN) / (AXIS_MAX - AXIS_MIN)
}
}
}
@@ -1,26 +1,46 @@
package com.hinnka.mycamera.ui.components
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hinnka.mycamera.R
import com.hinnka.mycamera.model.ColorPaletteState
import kotlin.math.min
import kotlin.math.roundToInt
private val PaletteHorizontalInset = 18.dp
private val PaletteVerticalInset = 16.dp
private val PaletteAccent = Color(0xFFFFC76B)
private const val PaletteReferenceStepCount = 9
@Composable
fun ColorRecipePalettePanel(
@@ -30,137 +50,304 @@ fun ColorRecipePalettePanel(
) {
val currentOnPaletteStateChange = rememberUpdatedState(onPaletteStateChange)
val currentPaletteState = rememberUpdatedState(paletteState)
val normalizedState = paletteState.normalized()
val toneLabel = stringResource(R.string.recipe_palette_tone)
val saturationLabel = stringResource(R.string.recipe_param_saturation)
Column(
modifier = modifier.fillMaxWidth()
modifier = modifier.fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
Box(
modifier = Modifier
.fillMaxWidth()
.aspectRatio(2f)
.padding(horizontal = 8.dp)
.pointerInput(Unit) {
detectTapGestures(
onDoubleTap = { offset ->
val width = size.width.toFloat().coerceAtLeast(1f)
val height = size.height.toFloat().coerceAtLeast(1f)
val currentState = currentPaletteState.value
val thumbX = currentState.x.coerceIn(0f, 1f) * width
val thumbY = currentState.y.coerceIn(0f, 1f) * height
val thumbRadius = min(width, height) * 0.065f
val dx = offset.x - thumbX
val dy = offset.y - thumbY
if (dx * dx + dy * dy <= thumbRadius * thumbRadius * 2.2f) {
currentOnPaletteStateChange.value(
currentState.copy(x = 0.5f, y = 0.5f)
)
}
},
onTap = { offset ->
val width = size.width.toFloat().coerceAtLeast(1f)
val height = size.height.toFloat().coerceAtLeast(1f)
currentOnPaletteStateChange.value(
currentPaletteState.value.copy(
x = (offset.x / width).coerceIn(0f, 1f),
y = (offset.y / height).coerceIn(0f, 1f)
)
)
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
PaletteAxisValue(
label = toneLabel,
value = normalizedState.toneValue,
modifier = Modifier.weight(1f)
)
PaletteAxisValue(
label = saturationLabel,
value = normalizedState.saturationValue,
modifier = Modifier.weight(1f)
)
}
Box(modifier = Modifier.fillMaxWidth()) {
Box(
modifier = Modifier
.padding(start = 30.dp)
.fillMaxWidth()
.aspectRatio(2f)
.semantics {
contentDescription =
"$toneLabel ${formatAxisValue(normalizedState.toneValue)}, " +
"$saturationLabel ${formatAxisValue(normalizedState.saturationValue)}"
}
.pointerInput(Unit) {
fun stateAt(position: Offset): ColorPaletteState {
val left = PaletteHorizontalInset.toPx()
val right = size.width - left
val top = PaletteVerticalInset.toPx()
val bottom = size.height - top
val horizontalPosition =
((position.x - left) / (right - left).coerceAtLeast(1f))
.coerceIn(0f, 1f)
val verticalPosition =
((position.y - top) / (bottom - top).coerceAtLeast(1f))
.coerceIn(0f, 1f)
return currentPaletteState.value.copy(
x = horizontalPosition,
y = verticalPosition
).normalized()
}
)
}
.pointerInput(Unit) {
detectDragGestures { change, _ ->
change.consume()
val width = size.width.toFloat().coerceAtLeast(1f)
val height = size.height.toFloat().coerceAtLeast(1f)
currentOnPaletteStateChange.value(
currentPaletteState.value.copy(
x = (change.position.x / width).coerceIn(0f, 1f),
y = (change.position.y / height).coerceIn(0f, 1f)
)
detectTapGestures(
onDoubleTap = { position ->
val current = currentPaletteState.value.normalized()
val left = PaletteHorizontalInset.toPx()
val top = PaletteVerticalInset.toPx()
val gridWidth = size.width - left * 2f
val gridHeight = size.height - top * 2f
val thumb = Offset(
x = left + current.x * gridWidth,
y = top + current.y * gridHeight
)
val distance = position - thumb
val hitRadius = 24.dp.toPx()
if (distance.getDistanceSquared() <= hitRadius * hitRadius) {
currentOnPaletteStateChange.value(
currentPaletteState.value.withValues(
saturation = 0f,
tone = 0f
)
)
}
},
onTap = { position ->
val next = stateAt(position)
val current = currentPaletteState.value.normalized()
if (next.x != current.x || next.y != current.y) {
currentOnPaletteStateChange.value(next)
}
}
)
}
.pointerInput(Unit) {
detectDragGestures { change, _ ->
change.consume()
val left = PaletteHorizontalInset.toPx()
val right = size.width - left
val top = PaletteVerticalInset.toPx()
val bottom = size.height - top
val horizontalPosition =
((change.position.x - left) / (right - left).coerceAtLeast(1f))
.coerceIn(0f, 1f)
val verticalPosition =
((change.position.y - top) / (bottom - top).coerceAtLeast(1f))
.coerceIn(0f, 1f)
val current = currentPaletteState.value
val next = current.copy(
x = horizontalPosition,
y = verticalPosition
).normalized()
val normalizedCurrent = current.normalized()
if (next.x != normalizedCurrent.x || next.y != normalizedCurrent.y) {
currentOnPaletteStateChange.value(next)
}
}
}
) {
Canvas(modifier = Modifier.fillMaxSize()) {
drawPalette(
x = normalizedState.x,
y = normalizedState.y
)
}
) {
Canvas(modifier = Modifier.matchParentSize()) {
val corner = CornerRadius(20.dp.toPx(), 20.dp.toPx())
}
drawRoundRect(
brush = Brush.horizontalGradient(
listOf(
Color(0xFF6C8A91),
Color(0xFFB8AEA0),
Color(0xFFD36A72)
)
),
size = size,
cornerRadius = corner
)
drawRoundRect(
brush = Brush.verticalGradient(
0f to Color.White.copy(alpha = 0.36f),
0.52f to Color.Transparent,
1f to Color(0xFF001F22).copy(alpha = 0.88f)
),
size = size,
cornerRadius = corner
)
drawRoundRect(
color = Color.Black.copy(alpha = 0.16f),
style = Stroke(width = 1.5.dp.toPx()),
size = size,
cornerRadius = corner
)
drawPaletteGrid(size)
val thumbX = paletteState.x.coerceIn(0f, 1f) * size.width
val thumbY = paletteState.y.coerceIn(0f, 1f) * size.height
val thumbRadius = min(size.width, size.height) * 0.065f
drawCircle(
color = Color.Black.copy(alpha = 0.18f),
radius = thumbRadius * 1.35f,
center = Offset(thumbX, thumbY)
)
drawCircle(
color = Color.White,
radius = thumbRadius,
center = Offset(thumbX, thumbY)
)
drawCircle(
color = Color.Black.copy(alpha = 0.18f),
radius = thumbRadius,
center = Offset(thumbX, thumbY),
style = Stroke(width = 1.dp.toPx())
)
Column(
modifier = Modifier
.matchParentSize()
.padding(top = 9.dp, end = 6.dp, bottom = 9.dp),
verticalArrangement = Arrangement.SpaceBetween,
horizontalAlignment = Alignment.Start
) {
AxisMarker("+100")
AxisMarker("0")
AxisMarker("-100")
}
}
Row(
modifier = Modifier
.fillMaxWidth()
.padding(start = 48.dp, end = 18.dp),
horizontalArrangement = Arrangement.SpaceBetween
) {
AxisMarker("-100")
AxisMarker("0")
AxisMarker("+100")
}
}
}
private fun androidx.compose.ui.graphics.drawscope.DrawScope.drawPaletteGrid(size: Size) {
val columns = 9
val rows = 9
val dotRadius = min(size.width, size.height) * 0.012f
val left = size.width * 0.07f
val top = size.height * 0.10f
val gridWidth = size.width * 0.86f
val gridHeight = size.height * 0.80f
@Composable
private fun PaletteAxisValue(
label: String,
value: Float,
modifier: Modifier = Modifier
) {
Row(
modifier = modifier
.clip(RoundedCornerShape(8.dp))
.background(Color.White.copy(alpha = 0.08f))
.padding(horizontal = 12.dp, vertical = 8.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = label,
color = Color.White.copy(alpha = 0.68f),
fontSize = 11.sp,
maxLines = 1
)
Text(
text = formatAxisValue(value),
color = if (value == 0f) Color.White.copy(alpha = 0.72f) else PaletteAccent,
fontSize = 13.sp,
fontWeight = FontWeight.SemiBold,
fontFamily = FontFamily.Monospace
)
}
}
for (row in 0 until rows) {
for (column in 0 until columns) {
val x = left + gridWidth * column / (columns - 1)
val y = top + gridHeight * row / (rows - 1)
val alpha = 0.18f + (column.toFloat() / (columns - 1)) * 0.34f + (1f - row.toFloat() / (rows - 1)) * 0.18f
@Composable
private fun AxisMarker(text: String) {
Text(
text = text,
color = Color.White.copy(alpha = 0.42f),
fontSize = 9.sp,
fontFamily = FontFamily.Monospace
)
}
private fun formatAxisValue(value: Float): String {
val rounded = value.roundToInt()
return when {
rounded > 0 -> "+$rounded"
else -> rounded.toString()
}
}
private fun androidx.compose.ui.graphics.drawscope.DrawScope.drawPalette(
x: Float,
y: Float
) {
val corner = CornerRadius(14.dp.toPx(), 14.dp.toPx())
val horizontalInset = PaletteHorizontalInset.toPx()
val verticalInset = PaletteVerticalInset.toPx()
val grid = Rect(
left = horizontalInset,
top = verticalInset,
right = size.width - horizontalInset,
bottom = size.height - verticalInset
)
drawRoundRect(
brush = Brush.horizontalGradient(
colors = listOf(
Color(0xFF777A78),
Color(0xFF9B8173),
Color(0xFFDF694E)
)
),
size = size,
cornerRadius = corner
)
drawRoundRect(
brush = Brush.verticalGradient(
colorStops = arrayOf(
0f to Color.White.copy(alpha = 0.40f),
0.48f to Color.Transparent,
1f to Color(0xFF081113).copy(alpha = 0.72f)
)
),
size = size,
cornerRadius = corner
)
drawRoundRect(
color = Color.White.copy(alpha = 0.12f),
size = size,
cornerRadius = corner,
style = Stroke(width = 1.dp.toPx())
)
for (index in 0 until PaletteReferenceStepCount) {
val fraction = index.toFloat() / (PaletteReferenceStepCount - 1)
val x = grid.left + grid.width * fraction
val y = grid.top + grid.height * fraction
val isCenter = index == PaletteReferenceStepCount / 2
drawLine(
color = Color.White.copy(alpha = if (isCenter) 0.28f else 0.10f),
start = Offset(x, grid.top),
end = Offset(x, grid.bottom),
strokeWidth = if (isCenter) 1.25.dp.toPx() else 0.75.dp.toPx()
)
drawLine(
color = Color.White.copy(alpha = if (isCenter) 0.28f else 0.10f),
start = Offset(grid.left, y),
end = Offset(grid.right, y),
strokeWidth = if (isCenter) 1.25.dp.toPx() else 0.75.dp.toPx()
)
}
val selectedX = grid.left + grid.width * x.coerceIn(0f, 1f)
val selectedY = grid.top + grid.height * y.coerceIn(0f, 1f)
drawLine(
color = PaletteAccent.copy(alpha = 0.30f),
start = Offset(selectedX, grid.top),
end = Offset(selectedX, grid.bottom),
strokeWidth = 1.dp.toPx()
)
drawLine(
color = PaletteAccent.copy(alpha = 0.30f),
start = Offset(grid.left, selectedY),
end = Offset(grid.right, selectedY),
strokeWidth = 1.dp.toPx()
)
for (row in 0 until PaletteReferenceStepCount) {
for (column in 0 until PaletteReferenceStepCount) {
val x =
grid.left + grid.width * column / (PaletteReferenceStepCount - 1)
val y =
grid.top + grid.height * row / (PaletteReferenceStepCount - 1)
drawCircle(
color = Color.White.copy(alpha = alpha.coerceIn(0.14f, 0.62f)),
radius = dotRadius,
color = Color.White.copy(alpha = if (row == 4 && column == 4) 0.72f else 0.42f),
radius = if (row == 4 && column == 4) 2.1.dp.toPx() else 1.5.dp.toPx(),
center = Offset(x, y)
)
}
}
val thumbCenter = Offset(selectedX, selectedY)
drawCircle(
color = Color.Black.copy(alpha = 0.30f),
radius = 12.dp.toPx(),
center = thumbCenter
)
drawCircle(
color = PaletteAccent,
radius = 9.dp.toPx(),
center = thumbCenter
)
drawCircle(
color = Color.White.copy(alpha = 0.92f),
radius = 4.dp.toPx(),
center = thumbCenter
)
}
@@ -6281,7 +6281,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
try {
val lutInfo = contentRepository.lutManager.getLutInfo(lutId)
val name = lutInfo?.getName() ?: "BakedLUT"
BakedLutExporter.exportBakedCube(lutConfig, recipe, name)
BakedLutExporter.exportBakedCube(getApplication(), lutConfig, recipe, name)
} catch (e: Exception) {
PLog.e("CameraViewModel", "Failed to bake LUT to cube", e)
null
@@ -6296,7 +6296,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
val recipe = contentRepository.lutManager.loadColorRecipeParams(lutId)
try {
BakedLutExporter.exportBakedHaldPng(lutConfig, recipe)
BakedLutExporter.exportBakedHaldPng(getApplication(), lutConfig, recipe)
} catch (e: Exception) {
PLog.e("CameraViewModel", "Failed to bake LUT to HALD PNG", e)
null
@@ -412,6 +412,7 @@
<string name="recipe_param_exposure">曝光</string>
<string name="recipe_param_contrast">對比度</string>
<string name="recipe_param_saturation">飽和度</string>
<string name="recipe_palette_tone">影調</string>
<string name="recipe_param_temperature">色溫</string>
<string name="recipe_param_tint">色調</string>
<string name="recipe_param_fade">褪色</string>
@@ -412,6 +412,7 @@
<string name="recipe_param_exposure">曝光</string>
<string name="recipe_param_contrast">对比度</string>
<string name="recipe_param_saturation">饱和度</string>
<string name="recipe_palette_tone">影调</string>
<string name="recipe_param_temperature">色温</string>
<string name="recipe_param_tint">色调</string>
<string name="recipe_param_fade">褪色</string>
@@ -412,6 +412,7 @@
<string name="recipe_param_exposure">曝光</string>
<string name="recipe_param_contrast">對比度</string>
<string name="recipe_param_saturation">飽和度</string>
<string name="recipe_palette_tone">影調</string>
<string name="recipe_param_temperature">色溫</string>
<string name="recipe_param_tint">色調</string>
<string name="recipe_param_fade">褪色</string>
@@ -412,6 +412,7 @@
<string name="recipe_param_exposure">曝光</string>
<string name="recipe_param_contrast">對比度</string>
<string name="recipe_param_saturation">飽和度</string>
<string name="recipe_palette_tone">影調</string>
<string name="recipe_param_temperature">色溫</string>
<string name="recipe_param_tint">色調</string>
<string name="recipe_param_fade">褪色</string>
+1
View File
@@ -417,6 +417,7 @@
<string name="recipe_param_exposure">Exposure</string>
<string name="recipe_param_contrast">Contrast</string>
<string name="recipe_param_saturation">Saturation</string>
<string name="recipe_palette_tone">High/Low Key</string>
<string name="recipe_param_temperature">Temp</string>
<string name="recipe_param_tint">Tint</string>
<string name="recipe_param_fade">Fade</string>
@@ -0,0 +1,33 @@
package com.hinnka.mycamera.lut
import java.nio.ByteBuffer
import java.nio.ByteOrder
import org.junit.Assert.assertArrayEquals
import org.junit.Test
class BasicToneLutTest {
@Test
fun planarOplusData_isInterleavedWithoutChannelSwap() {
val red = FloatArray(8) { it / 10f }
val green = FloatArray(8) { 1f + it / 10f }
val blue = FloatArray(8) { 2f + it / 10f }
val raw = ByteBuffer.allocate((red.size + green.size + blue.size) * Float.SIZE_BYTES)
.order(ByteOrder.LITTLE_ENDIAN)
.apply {
red.forEach(::putFloat)
green.forEach(::putFloat)
blue.forEach(::putFloat)
}
.array()
val actual = BasicToneLut.parsePlanarFloat32(raw, size = 2)
val expected = FloatArray(8 * 3)
for (index in 0 until 8) {
expected[index * 3] = red[index]
expected[index * 3 + 1] = green[index]
expected[index * 3 + 2] = blue[index]
}
assertArrayEquals(expected, actual, 0f)
}
}
@@ -0,0 +1,86 @@
package com.hinnka.mycamera.model
import org.junit.Assert.assertEquals
import org.junit.Test
class ColorPaletteMapperTest {
@Test
fun paletteAxes_useContinuousOplusValueRange() {
assertEquals(-100f, ColorPaletteState(x = 0f).saturationValue, 0.0001f)
assertEquals(-25f, ColorPaletteState(x = 0.375f).saturationValue, 0.0001f)
assertEquals(0f, ColorPaletteState(x = 0.5f).saturationValue, 0.0001f)
assertEquals(100f, ColorPaletteState(x = 1f).saturationValue, 0.0001f)
assertEquals(100f, ColorPaletteState(y = 0f).toneValue, 0.0001f)
assertEquals(0f, ColorPaletteState(y = 0.5f).toneValue, 0.0001f)
assertEquals(-100f, ColorPaletteState(y = 1f).toneValue, 0.0001f)
val state = ColorPaletteState.DEFAULT.withValues(saturation = -81.25f, tone = 62.5f)
assertEquals(0.09375f, state.x, 0.0001f)
assertEquals(0.1875f, state.y, 0.0001f)
}
@Test
fun paletteContribution_keepsAxesIndependent() {
val saturationOnly = ColorPaletteMapper.buildPaletteContribution(
ColorPaletteState.DEFAULT.withValues(saturation = 100f, tone = 0f)
)
assertEquals(1.6f, saturationOnly.saturation, 0.0001f)
assertEquals(0f, saturationOnly.toneToe, 0.0001f)
assertEquals(0f, saturationOnly.toneShoulder, 0.0001f)
assertEquals(0f, saturationOnly.exposure, 0.0001f)
assertEquals(0f, saturationOnly.temperature, 0.0001f)
assertEquals(0f, saturationOnly.color, 0.0001f)
assertEquals(
0.4f,
ColorPaletteMapper.buildPaletteContribution(
ColorPaletteState.DEFAULT.withValues(saturation = -100f, tone = 0f)
).saturation,
0.0001f
)
val toneOnly = ColorPaletteMapper.buildPaletteContribution(
ColorPaletteState.DEFAULT.withValues(saturation = 0f, tone = -100f)
)
assertEquals(1f, toneOnly.saturation, 0.0001f)
assertEquals(0f, toneOnly.toneToe, 0.0001f)
assertEquals(0f, toneOnly.toneShoulder, 0.0001f)
assertEquals(0f, toneOnly.tonePivot, 0.0001f)
assertEquals(-1f, ColorPaletteMapper.basicToneAmount(toneOnly), 0.0001f)
assertEquals(
0.375f,
ColorPaletteMapper.basicToneAmount(
ColorPaletteState.DEFAULT
.withValues(tone = 75f)
.copy(density = 0.5f)
),
0.0001f
)
}
@Test
fun merge_preservesManualParametersOutsidePaletteAxes() {
val params = ColorRecipeParams.DEFAULT.copy(
exposure = 0.7f,
temperature = -0.3f,
color = 0.25f,
saturation = 0.9f,
toneToe = -0.2f,
toneShoulder = 0.3f,
tonePivot = 0.1f,
paletteX = ColorPaletteState.valueToPosition(50f),
paletteY = ColorPaletteState.valueToPosition(-75f)
)
val effective = ColorPaletteMapper.mergeIntoEffectiveParams(params)
assertEquals(0.7f, effective.exposure, 0.0001f)
assertEquals(-0.3f, effective.temperature, 0.0001f)
assertEquals(0.25f, effective.color, 0.0001f)
assertEquals(1.2f, effective.saturation, 0.0001f)
assertEquals(-0.2f, effective.toneToe, 0.0001f)
assertEquals(0.3f, effective.toneShoulder, 0.0001f)
assertEquals(0.1f, effective.tonePivot, 0.0001f)
assertEquals(0.75f, ColorPaletteMapper.basicToneAmount(effective), 0.0001f)
}
}