-
-
Notifications
You must be signed in to change notification settings - Fork 539
Added color saturation slider #3251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nathaniel-jb
wants to merge
2
commits into
SuperTux:master
Choose a base branch
from
nathaniel-jb:color_saturation-slider-feature
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -198,4 +198,4 @@ ItemColorChannelRGBA::get_color() const | |
return m_channel; | ||
} | ||
|
||
/* EOF */ | ||
/* EOF */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line at the end. |
This file contains hidden or 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 |
---|---|---|
|
@@ -67,4 +67,4 @@ class ItemColorChannelRGBA final : public MenuItem | |
|
||
#endif | ||
|
||
/* EOF */ | ||
/* EOF */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a new line at the end. |
This file contains hidden or 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,209 @@ | ||
// SuperTux | ||
// Copyright (C) 2015 Hume2 <[email protected]> | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#include "gui/item_colorchannel_saturation.hpp" | ||
|
||
#include <sstream> | ||
|
||
#include "math/util.hpp" | ||
#include "supertux/resources.hpp" | ||
#include "video/drawing_context.hpp" | ||
|
||
namespace { | ||
|
||
std::string sat_value_to_string(float v_raw) | ||
{ | ||
float percent = v_raw * 100.0f; | ||
// Round to nearest integer percentage | ||
percent = 0.01f * floorf(percent * 100.0f + 0.5f); | ||
std::ostringstream os; | ||
os << v_raw << " (" << percent << " %" << ")"; | ||
return os.str(); | ||
} | ||
|
||
std::string float_to_string(float v) | ||
{ | ||
std::ostringstream os; | ||
os << v; | ||
return os.str(); | ||
} | ||
|
||
} // namespace | ||
|
||
ItemColorChannelSaturation::ItemColorChannelSaturation(Color* color, ColorOKLCh* okl, int id) : | ||
MenuItem(sat_value_to_string(okl->C), id), | ||
m_color(color), | ||
m_okl(okl), | ||
m_sat(&okl->C), | ||
m_sat_prev(okl->C), | ||
m_edit_mode(false), | ||
m_flickw(static_cast<int>(Resources::normal_font->get_text_width("_"))) | ||
//m_channel(channel) | ||
{ | ||
} | ||
|
||
void | ||
ItemColorChannelSaturation::draw(DrawingContext& context, const Vector& pos, | ||
int menu_width, bool active) | ||
{ | ||
if (!m_edit_mode && *m_sat != m_sat_prev) { | ||
set_text(sat_value_to_string(*m_sat)); | ||
m_sat_prev = *m_sat; | ||
} | ||
|
||
MenuItem::draw(context, pos, menu_width, active); | ||
*m_okl = ColorOKLCh(*m_color); | ||
float maxC = m_okl->get_max_chroma(); | ||
float fraction = 0.0f; | ||
if (maxC > 0.0f) fraction = std::clamp((*m_sat / 1.0f), 0.0f, 1.0f); | ||
const float lw = float(menu_width - 32) * (fraction); | ||
context.color().draw_filled_rect(Rectf(pos + Vector(16, -4), | ||
pos + Vector(16 + lw, 4)), | ||
Color::WHITE, 0.0f, LAYER_GUI-1); | ||
} | ||
|
||
int | ||
ItemColorChannelSaturation::get_width() const | ||
{ | ||
return static_cast<int>(Resources::normal_font->get_text_width(get_text()) + 16 + static_cast<float>(m_flickw)); | ||
} | ||
|
||
void | ||
ItemColorChannelSaturation::enable_edit_mode() | ||
{ | ||
if (m_edit_mode) | ||
// Do nothing if it is already enabled | ||
return; | ||
m_edit_mode = true; | ||
set_text(float_to_string(*m_sat)); | ||
} | ||
|
||
|
||
void | ||
ItemColorChannelSaturation::event(const SDL_Event& ev) | ||
{ | ||
if (ev.type == SDL_TEXTINPUT) { | ||
std::string txt = ev.text.text; | ||
for (auto& c : txt) { | ||
add_char(c); | ||
} | ||
} | ||
} | ||
|
||
void | ||
ItemColorChannelSaturation::add_char(char c) | ||
{ | ||
enable_edit_mode(); | ||
std::string text = get_text(); | ||
|
||
if (c == '.' || c == ',') | ||
{ | ||
const bool has_comma = (text.find('.') != std::string::npos); | ||
if (!has_comma) | ||
{ | ||
if (text.empty()) { | ||
text = "0."; | ||
} else { | ||
text.push_back('.'); | ||
} | ||
} | ||
} | ||
else if (isdigit(c)) | ||
{ | ||
text.push_back(c); | ||
} | ||
else | ||
{ | ||
return; | ||
} | ||
|
||
float number = std::stof(text); | ||
if (0.0f <= number && number <= 1.0f) { | ||
*m_sat = number; | ||
set_text(text); | ||
} | ||
} | ||
|
||
void | ||
ItemColorChannelSaturation::remove_char() | ||
{ | ||
enable_edit_mode(); | ||
std::string text = get_text(); | ||
|
||
if (text.empty()) | ||
{ | ||
*m_sat = 0.0f; | ||
} | ||
else | ||
{ | ||
text.pop_back(); | ||
|
||
if (!text.empty()) { | ||
*m_sat = std::stof(text); | ||
} else { | ||
*m_sat = 0.0f; | ||
} | ||
} | ||
|
||
set_text(text); | ||
} | ||
|
||
void | ||
ItemColorChannelSaturation::process_action(const MenuAction& action) | ||
{ | ||
switch (action) | ||
{ | ||
case MenuAction::REMOVE: | ||
remove_char(); | ||
break; | ||
|
||
case MenuAction::LEFT: | ||
case MenuAction::RIGHT: { | ||
float maxC = m_okl->get_max_chroma(); | ||
float delta = (action == MenuAction::LEFT ? -0.01f : +0.01f); | ||
|
||
float newSat = *m_sat + delta; | ||
newSat = std::clamp(newSat, 0.0f, maxC); | ||
newSat = roundf(newSat * 100.0f) / 100.0f; | ||
// Snap to reduce jitter at boundaries | ||
if(newSat < 0.005f) newSat = 0.0f; | ||
if(newSat > maxC - 0.005f) newSat = maxC; | ||
|
||
*m_sat = newSat; | ||
|
||
float oldA = m_color->alpha; | ||
*m_color = Color::from_oklch(*m_okl); | ||
m_color->alpha = oldA; // if you want to preserve alpha | ||
m_edit_mode = false; | ||
break; | ||
} | ||
|
||
case MenuAction::UNSELECT: | ||
m_edit_mode = false; | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
|
||
// Color | ||
// ItemColorChannelSaturation::get_color() const | ||
// { | ||
// return m_channel; | ||
// } | ||
|
||
/* EOF */ |
This file contains hidden or 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,69 @@ | ||
// SuperTux | ||
// Copyright (C) 2015 Hume2 <[email protected]> | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#ifndef HEADER_SUPERTUX_GUI_ITEM_COLORCHANNEL_SATURATION_HPP | ||
#define HEADER_SUPERTUX_GUI_ITEM_COLORCHANNEL_SATURATION_HPP | ||
|
||
#include "gui/menu_item.hpp" | ||
|
||
#include "util/colorspace_oklab.hpp" | ||
#include "video/color.hpp" | ||
|
||
|
||
class ItemColorChannelSaturation final : public MenuItem | ||
{ | ||
public: | ||
ItemColorChannelSaturation(Color* color, ColorOKLCh* okl, int id = -1); | ||
|
||
/** Draws the menu item. */ | ||
virtual void draw(DrawingContext&, const Vector& pos, int menu_width, bool active) override; | ||
|
||
/** Returns the minimum width of the menu item. */ | ||
virtual int get_width() const override; | ||
|
||
/** Processes the menu action. */ | ||
virtual void process_action(const MenuAction& action) override; | ||
|
||
/** Processes the given event. */ | ||
virtual void event(const SDL_Event& ev) override; | ||
|
||
//virtual Color get_color() const override; | ||
|
||
virtual bool changes_width() const override { return true; } | ||
|
||
void change_input(const std::string& input_) { set_text(input_); } | ||
|
||
private: | ||
void enable_edit_mode(); | ||
void add_char(char c); | ||
void remove_char(); | ||
|
||
private: | ||
Color* m_color; | ||
ColorOKLCh* m_okl; | ||
float* m_sat; | ||
float m_sat_prev; | ||
bool m_edit_mode; | ||
int m_flickw; | ||
|
||
private: | ||
ItemColorChannelSaturation(const ItemColorChannelSaturation&) = delete; | ||
ItemColorChannelSaturation& operator=(const ItemColorChannelSaturation&) = delete; | ||
}; | ||
|
||
#endif | ||
|
||
/* EOF */ |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please restore this file.