Created
August 24, 2023 21:21
-
-
Save lucaspcamargo/ce7daf152c90439e901ff5adfafcba26 to your computer and use it in GitHub Desktop.
Crude patch for LibreSprite 6ffe8472194bf :: Only allow selecting Sega MegaDrive 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
From 158217365be2080e7d5f5de838bfae034f39c402 Mon Sep 17 00:00:00 2001 | |
From: Lucas Pires Camargo <[email protected]> | |
Date: Thu, 24 Aug 2023 22:48:14 +0200 | |
Subject: [PATCH] limit colors to genesis palette | |
--- | |
src/app/color_utils.cpp | 21 +++++++++++++++++++++ | |
src/app/color_utils.h | 4 ++++ | |
src/app/commands/cmd_palette_editor.cpp | 10 ++++++---- | |
src/app/ui/color_sliders.cpp | 3 ++- | |
src/app/ui/color_spectrum.cpp | 10 +++++----- | |
src/app/ui/color_tint_shade_tone.cpp | 8 +++++--- | |
src/app/ui/color_wheel.cpp | 10 +++++++--- | |
7 files changed, 50 insertions(+), 16 deletions(-) | |
diff --git a/src/app/color_utils.cpp b/src/app/color_utils.cpp | |
index 5d3b334f9..17b9072f1 100644 | |
--- a/src/app/color_utils.cpp | |
+++ b/src/app/color_utils.cpp | |
@@ -18,6 +18,10 @@ | |
#include "doc/layer.h" | |
#include "doc/palette.h" | |
#include "doc/sprite.h" | |
+#include "color_utils.h" | |
+ | |
+#include <math.h> | |
+ | |
namespace app { | |
@@ -163,4 +167,21 @@ doc::color_t color_utils::color_for_target(const app::Color& color, const ColorT | |
return c; | |
} | |
+#define approx(C) (static_cast<uint8_t>(roundf((C)/255.0*7.0)/7.0*255.0)) | |
+ | |
+app::Color color_utils::apply_limitations(const app::Color &color) | |
+{ | |
+ return app::Color::fromRgb(approx(color.getRed()), | |
+ approx(color.getGreen()), | |
+ approx(color.getBlue()), | |
+ approx(color.getAlpha())); | |
+} | |
+ | |
+gfx::Color color_utils::apply_limitations(const gfx::Color &color) | |
+{ | |
+ return gfx::rgba( approx(gfx::getr(color)), | |
+ approx(gfx::getg(color)), | |
+ approx(gfx::getb(color)), | |
+ approx(gfx::geta(color))); | |
+} | |
} // namespace app | |
diff --git a/src/app/color_utils.h b/src/app/color_utils.h | |
index d8f1ed3be..087ef9beb 100644 | |
--- a/src/app/color_utils.h | |
+++ b/src/app/color_utils.h | |
@@ -23,11 +23,15 @@ namespace app { | |
gfx::Color blackandwhite(gfx::Color color); | |
gfx::Color blackandwhite_neg(gfx::Color color); | |
+ app::Color apply_limitations(const app::Color& color); | |
+ gfx::Color apply_limitations(const gfx::Color& color); | |
+ | |
gfx::Color color_for_ui(const app::Color& color); | |
doc::color_t color_for_image(const app::Color& color, doc::PixelFormat format); | |
doc::color_t color_for_layer(const app::Color& color, doc::Layer* layer); | |
doc::color_t color_for_target_mask(const app::Color& color, const ColorTarget& colorTarget); | |
doc::color_t color_for_target(const app::Color& color, const ColorTarget& colorTarget); | |
+ | |
} // namespace color_utils | |
} // namespace app | |
diff --git a/src/app/commands/cmd_palette_editor.cpp b/src/app/commands/cmd_palette_editor.cpp | |
index 7270f1bed..68365ff05 100644 | |
--- a/src/app/commands/cmd_palette_editor.cpp | |
+++ b/src/app/commands/cmd_palette_editor.cpp | |
@@ -423,10 +423,11 @@ void PaletteEntryEditor::onFgBgColorChange(const app::Color& _color) | |
void PaletteEntryEditor::onColorSlidersChange(ColorSlidersChangeEvent& ev) | |
{ | |
- setColor(ev.color()); | |
+ auto color = color_utils::apply_limitations(ev.color()); | |
+ setColor(color); | |
if (ev.mode() == ColorSliders::Absolute) | |
- setAbsolutePaletteEntryChannel(ev.channel(), ev.color()); | |
+ setAbsolutePaletteEntryChannel(ev.channel(), color); | |
else | |
setRelativePaletteEntryChannel(ev.channel(), ev.delta()); | |
@@ -440,8 +441,9 @@ void PaletteEntryEditor::onColorHexEntryChange(const app::Color& color) | |
// is writting in the text field. | |
m_disableHexUpdate = true; | |
- setColor(color); | |
- setPaletteEntry(color); | |
+ app::Color adj_color = color_utils::apply_limitations(color); | |
+ setColor(adj_color); | |
+ setPaletteEntry(adj_color); | |
updateCurrentSpritePalette("Color Change"); | |
updateColorBar(); | |
diff --git a/src/app/ui/color_sliders.cpp b/src/app/ui/color_sliders.cpp | |
index c9c9f6aba..752b9037c 100644 | |
--- a/src/app/ui/color_sliders.cpp | |
+++ b/src/app/ui/color_sliders.cpp | |
@@ -72,7 +72,8 @@ namespace { | |
color = color_utils::color_for_ui(app::Color::fromGray(255 * x / w)); | |
break; | |
} | |
- g->drawVLine(color, rc.x+x, rc.y, rc.h); | |
+ | |
+ g->drawVLine(color_utils::apply_limitations(color), rc.x+x, rc.y, rc.h); | |
} | |
} | |
diff --git a/src/app/ui/color_spectrum.cpp b/src/app/ui/color_spectrum.cpp | |
index 2224c61dd..2b454176c 100644 | |
--- a/src/app/ui/color_spectrum.cpp | |
+++ b/src/app/ui/color_spectrum.cpp | |
@@ -59,10 +59,10 @@ app::Color ColorSpectrum::getColorByPosition(const gfx::Point& pos) | |
double sat = (v < vmid ? 100.0 * v / vmid : 100.0); | |
double val = (v < vmid ? 100.0 : 100.0-(100.0 * (v-vmid) / vmid)); | |
- return app::Color::fromHsv( | |
+ return color_utils::apply_limitations (app::Color::fromHsv( | |
MID(0.0, hue, 360.0), | |
MID(0.0, sat, 100.0), | |
- MID(0.0, val, 100.0)); | |
+ MID(0.0, val, 100.0))); | |
} | |
void ColorSpectrum::onPaint(ui::PaintEvent& ev) | |
@@ -99,13 +99,13 @@ void ColorSpectrum::onPaint(ui::PaintEvent& ev) | |
double sat = (v < vmid ? 100.0 * v / vmid : 100.0); | |
double val = (v < vmid ? 100.0 : 100.0-(100.0 * (v-vmid) / vmid)); | |
- gfx::Color color = color_utils::color_for_ui( | |
+ gfx::Color color = color_utils::apply_limitations(color_utils::color_for_ui( | |
app::Color::fromHsv( | |
MID(0.0, hue, 360.0), | |
MID(0.0, sat, 100.0), | |
- MID(0.0, val, 100.0))); | |
+ MID(0.0, val, 100.0)))); | |
- g->putPixel(color, rc.x+x, rc.y+y); | |
+ //g->putPixel(color, rc.x+x, rc.y+y); | |
} | |
} | |
diff --git a/src/app/ui/color_tint_shade_tone.cpp b/src/app/ui/color_tint_shade_tone.cpp | |
index 20842a76e..41c968992 100644 | |
--- a/src/app/ui/color_tint_shade_tone.cpp | |
+++ b/src/app/ui/color_tint_shade_tone.cpp | |
@@ -64,10 +64,10 @@ app::Color ColorTintShadeTone::getColorByPosition(const gfx::Point& pos) | |
val = (100.0 - 100.0 * v / vmax); | |
} | |
- return app::Color::fromHsv( | |
+ return color_utils::apply_limitations(app::Color::fromHsv( | |
MID(0.0, hue, 360.0), | |
MID(0.0, sat, 100.0), | |
- MID(0.0, val, 100.0)); | |
+ MID(0.0, val, 100.0))); | |
} | |
void ColorTintShadeTone::onPaint(ui::PaintEvent& ev) | |
@@ -99,7 +99,7 @@ void ColorTintShadeTone::onPaint(ui::PaintEvent& ev) | |
hue, | |
MID(0.0, sat, 100.0), | |
MID(0.0, val, 100.0))); | |
- | |
+ color = color_utils::apply_limitations(color); | |
g->putPixel(color, rc.x+x, rc.y+y); | |
} | |
} | |
@@ -110,6 +110,7 @@ void ColorTintShadeTone::onPaint(ui::PaintEvent& ev) | |
gfx::Color color = color_utils::color_for_ui( | |
app::Color::fromHsv( | |
(360.0 * x / rc.w), 100.0, 100.0)); | |
+ color = color_utils::apply_limitations(color); | |
g->putPixel(color, rc.x+x, rc.y+y); | |
} | |
@@ -160,6 +161,7 @@ bool ColorTintShadeTone::onProcessMessage(ui::Message* msg) | |
m_capturedInHue = inHueBarArea(mouseMsg->position()); | |
app::Color color = getColorByPosition(mouseMsg->position()); | |
+ color = color_utils::apply_limitations(color); | |
if (color != app::Color::fromMask()) { | |
StatusBar::instance()->showColor(0, "", color); | |
if (hasCapture()) | |
diff --git a/src/app/ui/color_wheel.cpp b/src/app/ui/color_wheel.cpp | |
index 309da870f..015ea02b3 100644 | |
--- a/src/app/ui/color_wheel.cpp | |
+++ b/src/app/ui/color_wheel.cpp | |
@@ -111,10 +111,10 @@ app::Color ColorWheel::getColorInClientPos(const gfx::Point& pos) | |
sat = int(100.0 * d / m_wheelRadius); | |
} | |
- return app::Color::fromHsv( | |
+ return color_utils::apply_limitations(app::Color::fromHsv( | |
MID(0, hue, 360), | |
MID(0, sat, 100), | |
- 100); | |
+ 100)); | |
} | |
// Pick harmonies | |
@@ -134,6 +134,8 @@ app::Color ColorWheel::getColorInClientPos(const gfx::Point& pos) | |
color = app::Color::fromHsv(convertHueAngle(int(color.getHue()), 1), | |
color.getSaturation(), | |
color.getValue()); | |
+ | |
+ color = color_utils::apply_limitations(color); | |
return color; | |
} | |
} | |
@@ -178,9 +180,11 @@ app::Color ColorWheel::getColorInHarmony(int j) const | |
j = MID(0, j, harmonies[i].n-1); | |
double hue = convertHueAngle(int(m_color.getHue()), -1) + harmonies[i].hues[j]; | |
double sat = m_color.getSaturation() * harmonies[i].sats[j] / 100.0; | |
- return app::Color::fromHsv(std::fmod(hue, 360), | |
+ app::Color color = app::Color::fromHsv(std::fmod(hue, 360), | |
MID(0.0, sat, 100.0), | |
m_color.getValue()); | |
+ color = color_utils::apply_limitations(color); | |
+ return color; | |
} | |
void ColorWheel::onResize(ui::ResizeEvent& ev) | |
-- | |
2.41.0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment