-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the command
palette
to extract the color palette from an image.
- Loading branch information
1 parent
c453418
commit 512ed54
Showing
4 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
tiny-cli/src/jvmMain/kotlin/com/github/minigdx/tiny/cli/command/PaletteCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.github.minigdx.tiny.cli.command | ||
|
||
import com.github.ajalt.clikt.core.CliktCommand | ||
import com.github.ajalt.clikt.parameters.arguments.argument | ||
import com.github.ajalt.clikt.parameters.options.default | ||
import com.github.ajalt.clikt.parameters.options.flag | ||
import com.github.ajalt.clikt.parameters.options.option | ||
import com.github.ajalt.clikt.parameters.types.file | ||
import com.github.minigdx.tiny.cli.config.GameParameters | ||
import com.github.minigdx.tiny.cli.exception.MissingTinyConfigurationException | ||
import com.github.minigdx.tiny.file.CommonVirtualFileSystem | ||
import com.github.minigdx.tiny.log.StdOutLogger | ||
import com.github.minigdx.tiny.platform.glfw.GlfwPlatform | ||
import kotlinx.coroutines.runBlocking | ||
import java.io.File | ||
|
||
class PaletteCommand : CliktCommand(name = "palette", help = "Extract the color palette from an image.") { | ||
|
||
val game by option( | ||
help = "The directory containing all game information", | ||
) | ||
.file(mustExist = true, canBeDir = true, canBeFile = false) | ||
.default(File(".")) | ||
|
||
val image by argument( | ||
help = "The image used to extract the palette.", | ||
).file(mustExist = true, canBeFile = true, canBeDir = false) | ||
|
||
val append by option(help = "Append, instead of replacing, the palette information in the game file.") | ||
.flag() | ||
|
||
val print by option(help = "Print in the console the palette information, without updating the game.") | ||
.flag() | ||
|
||
override fun run() { | ||
val tiny = game.resolve("_tiny.json") | ||
if (!tiny.exists()) { | ||
throw MissingTinyConfigurationException(tiny) | ||
} | ||
// Open the _tiny.json | ||
val gameParameters = GameParameters.read(tiny) | ||
val gameOptions = gameParameters.toGameOptions() | ||
val platform = GlfwPlatform(gameOptions, StdOutLogger("whatever"), CommonVirtualFileSystem(), game) | ||
val imageData = runBlocking { | ||
platform.createImageStream(image.relativeTo(game).path).read() | ||
} | ||
|
||
val colors = mutableSetOf<String>() | ||
if (append) { | ||
// Append only new colors | ||
colors.addAll(gameOptions.palette) | ||
} | ||
var extractedColors = emptyList<String>() | ||
|
||
for (index in 0..(imageData.height * imageData.width) step 4) { | ||
val r = imageData.data[index].toInt().coerceIn(0, 255) | ||
val g = imageData.data[index + 1].toInt().coerceIn(0, 255) | ||
val b = imageData.data[index + 2].toInt().coerceIn(0, 255) | ||
|
||
// Convert to hex string | ||
val hexString = String.format("#%02x%02x%02x", r, g, b).uppercase() | ||
if (colors.add(hexString)) { | ||
extractedColors = extractedColors + hexString | ||
} | ||
} | ||
|
||
if (print) { | ||
echo("\uD83C\uDFA8 Colors extracted from the file ${image.name}:") | ||
extractedColors.forEach { | ||
echo("- $it") | ||
} | ||
return | ||
} | ||
|
||
val replacedColors = if (append) { | ||
gameOptions.palette + extractedColors | ||
} else { | ||
extractedColors | ||
} | ||
|
||
gameParameters.setPalette(replacedColors).write(tiny) | ||
echo("\uD83C\uDFA8 Game has been updated with the new color palette (with ${replacedColors.size} colors)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters