Keep fg/bg color indexes in the palette range

This commit is contained in:
David Capello 2015-06-29 11:00:46 -03:00
parent 34183ba207
commit 49b3502318
3 changed files with 30 additions and 2 deletions

View File

@ -198,6 +198,7 @@ ColorBar::ColorBar(int align)
m_fgConn = Preferences::instance().colorBar.fgColor.AfterChange.connect(Bind<void>(&ColorBar::onFgColorChangeFromPreferences, this));
m_bgConn = Preferences::instance().colorBar.bgColor.AfterChange.connect(Bind<void>(&ColorBar::onBgColorChangeFromPreferences, this));
m_paletteView.FocusEnter.connect(&ColorBar::onFocusPaletteView, this);
m_appPalChangeConn = App::instance()->PaletteChange.connect(&ColorBar::onAppPaletteChange, this);
}
ColorBar::~ColorBar()
@ -255,6 +256,12 @@ void ColorBar::onActiveSiteChange(const doc::Site& site)
}
}
void ColorBar::onAppPaletteChange()
{
fixColorIndex(m_fgColor);
fixColorIndex(m_bgColor);
}
void ColorBar::onFocusPaletteView()
{
App::instance()->inputChain().prioritize(this);
@ -748,4 +755,19 @@ void ColorBar::onCancel(Context* ctx)
invalidate();
}
// static
void ColorBar::fixColorIndex(ColorButton& colorButton)
{
app::Color color = colorButton.getColor();
if (color.getType() == Color::IndexType) {
int oldIndex = color.getIndex();
int newIndex = MID(0, oldIndex, get_current_palette()->size()-1);
if (oldIndex != newIndex) {
color = Color::fromIndex(newIndex);
colorButton.setColor(color);
}
}
}
} // namespace app

View File

@ -71,6 +71,7 @@ namespace app {
void onCancel(Context* ctx) override;
protected:
void onAppPaletteChange();
void onFocusPaletteView();
void onBeforeExecuteCommand(Command* command);
void onPaletteButtonClick();
@ -100,6 +101,7 @@ namespace app {
void applyRemap(const doc::Remap& remap, const doc::Palette* newPalette, const std::string& actionText);
void setPalette(const doc::Palette* newPalette, const std::string& actionText);
void setTransparentIndex(int index);
static void fixColorIndex(ColorButton& color);
class ScrollableView : public ui::View {
public:
@ -123,6 +125,7 @@ namespace app {
ScopedConnection m_conn;
ScopedConnection m_fgConn;
ScopedConnection m_bgConn;
ScopedConnection m_appPalChangeConn;
};
} // namespace app

View File

@ -229,7 +229,7 @@ public:
const app::Color& fgColor,
const app::Color& bgColor)
: ToolLoopBase(editor, tool, ink, document,
button,fgColor, bgColor)
button, fgColor, bgColor)
, m_context(context)
, m_canceled(false)
, m_transaction(m_context,
@ -405,10 +405,13 @@ tools::ToolLoop* create_tool_loop(Editor* editor, Context* context)
app::Color fg = colorbar->getFgColor();
app::Color bg = colorbar->getBgColor();
ASSERT(fg.isValid());
ASSERT(bg.isValid());
if (!fg.isValid() || !bg.isValid()) {
Alert::show(PACKAGE
"<<The current selected foreground and/or background color"
"<<is out of range. Select valid colors in the color-bar."
"<<is out of range. Select a valid color in the color-bar."
"||&Close");
return NULL;
}