diff --git a/laf b/laf index 5c0fa215c..4f0acaa1f 160000 --- a/laf +++ b/laf @@ -1 +1 @@ -Subproject commit 5c0fa215cd6fba21d9bf230d2da62d2890b21baf +Subproject commit 4f0acaa1ffb456b335201ca457b09f3cb99369cc diff --git a/src/app/cli/cli_processor.cpp b/src/app/cli/cli_processor.cpp index 2c6de13bd..90c49dd10 100644 --- a/src/app/cli/cli_processor.cpp +++ b/src/app/cli/cli_processor.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018-2019 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -23,6 +23,7 @@ #include "app/filename_formatter.h" #include "app/restore_visible_layers.h" #include "app/ui_context.h" +#include "base/clamp.h" #include "base/convert_to.h" #include "base/fs.h" #include "base/split_string.h" @@ -642,8 +643,8 @@ bool CliProcessor::openFile(Context* ctx, CliOpenFile& cof) // --frame-range with --frame-tag if (tag) { selFrames.insert( - tag->fromFrame()+MID(0, cof.fromFrame, tag->frames()-1), - tag->fromFrame()+MID(0, cof.toFrame, tag->frames()-1)); + tag->fromFrame()+base::clamp(cof.fromFrame, 0, tag->frames()-1), + tag->fromFrame()+base::clamp(cof.toFrame, 0, tag->frames()-1)); } // --frame-range without --frame-tag else { diff --git a/src/app/cmd/background_from_layer.cpp b/src/app/cmd/background_from_layer.cpp index d64d971e0..457cf642f 100644 --- a/src/app/cmd/background_from_layer.cpp +++ b/src/app/cmd/background_from_layer.cpp @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2020 Igara Studio S.A. // Copyright (C) 2001-2016 David Capello // // This program is distributed under the terms of @@ -17,6 +18,7 @@ #include "app/cmd/set_cel_opacity.h" #include "app/cmd/set_cel_position.h" #include "app/doc.h" +#include "base/clamp.h" #include "doc/cel.h" #include "doc/image.h" #include "doc/layer.h" @@ -62,7 +64,7 @@ void BackgroundFromLayer::onExecute() bg_image.get(), cel_image, sprite->palette(cel->frame()), cel->x(), cel->y(), - MID(0, cel->opacity(), 255), + base::clamp(cel->opacity(), 0, 255), static_cast(layer)->blendMode()); // now we have to copy the new image (bg_image) to the cel... diff --git a/src/app/color.cpp b/src/app/color.cpp index 5c5d05fb7..cb9a5c7d9 100644 --- a/src/app/color.cpp +++ b/src/app/color.cpp @@ -13,6 +13,7 @@ #include "app/color_utils.h" #include "app/modules/palettes.h" +#include "base/clamp.h" #include "base/debug.h" #include "doc/image.h" #include "doc/palette.h" @@ -203,8 +204,8 @@ std::string Color::toString() const << std::setprecision(2) << std::fixed << m_value.hsv.h << "," - << MID(0.0, m_value.hsv.s*100.0, 100.0) << "," - << MID(0.0, m_value.hsv.v*100.0, 100.0) << "," + << base::clamp(m_value.hsv.s*100.0, 0.0, 100.0) << "," + << base::clamp(m_value.hsv.v*100.0, 0.0, 100.0) << "," << m_value.hsv.a << "}"; break; @@ -213,8 +214,8 @@ std::string Color::toString() const << std::setprecision(2) << std::fixed << m_value.hsl.h << "," - << MID(0.0, m_value.hsl.s*100.0, 100.0) << "," - << MID(0.0, m_value.hsl.l*100.0, 100.0) << "," + << base::clamp(m_value.hsl.s*100.0, 0.0, 100.0) << "," + << base::clamp(m_value.hsl.l*100.0, 0.0, 100.0) << "," << m_value.hsl.a << "}"; break; @@ -267,8 +268,8 @@ std::string Color::toHumanReadableString(PixelFormat pixelFormat, HumanReadableS else { result << "HSV " << int(m_value.hsv.h) << "\xc2\xb0 " - << MID(0, int(m_value.hsv.s*100.0), 100) << "% " - << MID(0, int(m_value.hsv.v*100.0), 100) << "%"; + << base::clamp(int(m_value.hsv.s*100.0), 0, 100) << "% " + << base::clamp(int(m_value.hsv.v*100.0), 0, 100) << "%"; if (pixelFormat == IMAGE_INDEXED) result << " Index " << color_utils::color_for_image(*this, IMAGE_INDEXED); @@ -287,8 +288,8 @@ std::string Color::toHumanReadableString(PixelFormat pixelFormat, HumanReadableS else { result << "HSL " << int(m_value.hsl.h) << "\xc2\xb0 " - << MID(0, int(m_value.hsl.s*100.0), 100) << "% " - << MID(0, int(m_value.hsl.l*100.0), 100) << "%"; + << base::clamp(int(m_value.hsl.s*100.0), 0, 100) << "% " + << base::clamp(int(m_value.hsl.l*100.0), 0, 100) << "%"; if (pixelFormat == IMAGE_INDEXED) result << " Index " << color_utils::color_for_image(*this, IMAGE_INDEXED); @@ -357,8 +358,8 @@ std::string Color::toHumanReadableString(PixelFormat pixelFormat, HumanReadableS } else { result << int(m_value.hsv.h) << "\xc2\xb0" - << MID(0, int(m_value.hsv.s*100.0), 100) << "," - << MID(0, int(m_value.hsv.v*100.0), 100); + << base::clamp(int(m_value.hsv.s*100.0), 0, 100) << "," + << base::clamp(int(m_value.hsv.v*100.0), 0, 100); } break; @@ -368,8 +369,8 @@ std::string Color::toHumanReadableString(PixelFormat pixelFormat, HumanReadableS } else { result << int(m_value.hsl.h) << "\xc2\xb0" - << MID(0, int(m_value.hsl.s*100.0), 100) << "," - << MID(0, int(m_value.hsl.l*100.0), 100); + << base::clamp(int(m_value.hsl.s*100.0), 0, 100) << "," + << base::clamp(int(m_value.hsl.l*100.0), 0, 100); } break; @@ -907,7 +908,7 @@ int Color::getAlpha() const void Color::setAlpha(int alpha) { - alpha = MID(0, alpha, 255); + alpha = base::clamp(alpha, 0, 255); switch (getType()) { diff --git a/src/app/commands/cmd_canvas_size.cpp b/src/app/commands/cmd_canvas_size.cpp index 29944f800..78a7f8051 100644 --- a/src/app/commands/cmd_canvas_size.cpp +++ b/src/app/commands/cmd_canvas_size.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -24,6 +24,7 @@ #include "app/ui/skin/skin_theme.h" #include "app/ui_context.h" #include "base/bind.h" +#include "base/clamp.h" #include "doc/image.h" #include "doc/mask.h" #include "doc/sprite.h" @@ -368,8 +369,8 @@ void CanvasSizeCommand::onExecute(Context* context) api.cropSprite(sprite, gfx::Rect(x1, y1, - MID(1, x2-x1, DOC_SPRITE_MAX_WIDTH), - MID(1, y2-y1, DOC_SPRITE_MAX_HEIGHT)), + base::clamp(x2-x1, 1, DOC_SPRITE_MAX_WIDTH), + base::clamp(y2-y1, 1, DOC_SPRITE_MAX_HEIGHT)), params.trimOutside()); tx.commit(); diff --git a/src/app/commands/cmd_cel_opacity.cpp b/src/app/commands/cmd_cel_opacity.cpp index 9677241cf..c8da4ebf8 100644 --- a/src/app/commands/cmd_cel_opacity.cpp +++ b/src/app/commands/cmd_cel_opacity.cpp @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2020 Igara Studio S.A. // Copyright (C) 2018 David Capello // // This program is distributed under the terms of @@ -18,6 +19,7 @@ #include "app/modules/gui.h" #include "app/tx.h" #include "app/ui/timeline/timeline.h" +#include "base/clamp.h" #include "doc/cel.h" #include "doc/cels_range.h" #include "doc/sprite.h" @@ -51,7 +53,7 @@ CelOpacityCommand::CelOpacityCommand() void CelOpacityCommand::onLoadParams(const Params& params) { m_opacity = params.get_as("opacity"); - m_opacity = MID(0, m_opacity, 255); + m_opacity = base::clamp(m_opacity, 0, 255); } bool CelOpacityCommand::onEnabled(Context* context) diff --git a/src/app/commands/cmd_goto_frame.cpp b/src/app/commands/cmd_goto_frame.cpp index 210f4f90a..ffeb3307a 100644 --- a/src/app/commands/cmd_goto_frame.cpp +++ b/src/app/commands/cmd_goto_frame.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -18,6 +18,7 @@ #include "app/ui/editor/editor.h" #include "app/ui/editor/editor_customization_delegate.h" #include "app/ui/search_entry.h" +#include "base/clamp.h" #include "doc/sprite.h" #include "doc/tag.h" #include "ui/combobox.h" @@ -256,7 +257,9 @@ private: } } - return MID(0, m_frame-docPref.timeline.firstFrame(), editor->sprite()->lastFrame()); + return base::clamp( + m_frame-docPref.timeline.firstFrame(), + 0, editor->sprite()->lastFrame()); } private: diff --git a/src/app/commands/cmd_layer_opacity.cpp b/src/app/commands/cmd_layer_opacity.cpp index 9d3e90d03..d4a56ab0a 100644 --- a/src/app/commands/cmd_layer_opacity.cpp +++ b/src/app/commands/cmd_layer_opacity.cpp @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2020 Igara Studio S.A. // Copyright (C) 2016-2018 David Capello // // This program is distributed under the terms of @@ -18,6 +19,7 @@ #include "app/modules/gui.h" #include "app/tx.h" #include "app/ui/timeline/timeline.h" +#include "base/clamp.h" #include "doc/layer.h" #include "fmt/format.h" @@ -49,7 +51,7 @@ LayerOpacityCommand::LayerOpacityCommand() void LayerOpacityCommand::onLoadParams(const Params& params) { m_opacity = params.get_as("opacity"); - m_opacity = MID(0, m_opacity, 255); + m_opacity = base::clamp(m_opacity, 0, 255); } bool LayerOpacityCommand::onEnabled(Context* context) diff --git a/src/app/commands/cmd_modify_selection.cpp b/src/app/commands/cmd_modify_selection.cpp index 3a7a3f9a9..399ed222d 100644 --- a/src/app/commands/cmd_modify_selection.cpp +++ b/src/app/commands/cmd_modify_selection.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2015-2018 David Capello // // This program is distributed under the terms of @@ -18,6 +18,7 @@ #include "app/modules/gui.h" #include "app/pref/preferences.h" #include "app/tx.h" +#include "base/clamp.h" #include "base/convert_to.h" #include "doc/algorithm/modify_selection.h" #include "doc/brush_type.h" @@ -112,7 +113,7 @@ void ModifySelectionCommand::onExecute(Context* context) return; quantity = window.quantity()->textInt(); - quantity = MID(1, quantity, 100); + quantity = base::clamp(quantity, 1, 100); brush = (window.circle()->isSelected() ? doc::kCircleBrushType: doc::kSquareBrushType); diff --git a/src/app/commands/cmd_new_file.cpp b/src/app/commands/cmd_new_file.cpp index fab9b35bc..0089157da 100644 --- a/src/app/commands/cmd_new_file.cpp +++ b/src/app/commands/cmd_new_file.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018-2019 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -129,7 +129,7 @@ void NewFileCommand::onExecute(Context* ctx) int w = pref.newFile.width(); int h = pref.newFile.height(); int bg = pref.newFile.backgroundColor(); - bg = MID(0, bg, 2); + bg = base::clamp(bg, 0, 2); // If the clipboard contains an image, we can show the size of the // clipboard as default image size. diff --git a/src/app/commands/cmd_options.cpp b/src/app/commands/cmd_options.cpp index e19a110d5..0a257f80e 100644 --- a/src/app/commands/cmd_options.cpp +++ b/src/app/commands/cmd_options.cpp @@ -31,6 +31,7 @@ #include "app/ui/separator_in_view.h" #include "app/ui/skin/skin_theme.h" #include "base/bind.h" +#include "base/clamp.h" #include "base/convert_to.h" #include "base/fs.h" #include "base/string.h" @@ -620,7 +621,7 @@ public: int undo_size_limit_value; undo_size_limit_value = undoSizeLimit()->textInt(); - undo_size_limit_value = MID(0, undo_size_limit_value, 999999); + undo_size_limit_value = base::clamp(undo_size_limit_value, 0, 999999); m_pref.undo.sizeLimit(undo_size_limit_value); m_pref.undo.gotoModified(undoGotoModified()->isSelected()); diff --git a/src/app/commands/cmd_palette_size.cpp b/src/app/commands/cmd_palette_size.cpp index 258f51f9b..ff6fb21cb 100644 --- a/src/app/commands/cmd_palette_size.cpp +++ b/src/app/commands/cmd_palette_size.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -14,6 +14,7 @@ #include "app/commands/params.h" #include "app/context_access.h" #include "app/tx.h" +#include "base/clamp.h" #include "doc/palette.h" #include "doc/sprite.h" @@ -69,7 +70,7 @@ void PaletteSizeCommand::onExecute(Context* context) if (ncolors == palette.size()) return; - palette.resize(MID(1, ncolors, std::numeric_limits::max())); + palette.resize(base::clamp(ncolors, 1, std::numeric_limits::max())); ContextWriter writer(reader); Tx tx(context, "Palette Size", ModifyDocument); diff --git a/src/app/commands/cmd_paste_text.cpp b/src/app/commands/cmd_paste_text.cpp index 9c4b91012..88bc78aa3 100644 --- a/src/app/commands/cmd_paste_text.cpp +++ b/src/app/commands/cmd_paste_text.cpp @@ -22,6 +22,7 @@ #include "app/ui/timeline/timeline.h" #include "app/util/freetype_utils.h" #include "base/bind.h" +#include "base/clamp.h" #include "base/fs.h" #include "base/string.h" #include "doc/image.h" @@ -80,7 +81,7 @@ public: int sizeValue() const { int size = fontSize()->textInt(); - size = MID(1, size, 5000); + size = base::clamp(size, 1, 5000); return size; } @@ -165,7 +166,7 @@ void PasteTextCommand::onExecute(Context* ctx) bool antialias = window.antialias()->isSelected(); std::string faceName = window.faceValue(); int size = window.sizeValue(); - size = MID(1, size, 999); + size = base::clamp(size, 1, 999); pref.textTool.fontFace(faceName); pref.textTool.fontSize(size); pref.textTool.antialias(antialias); diff --git a/src/app/commands/cmd_sprite_size.cpp b/src/app/commands/cmd_sprite_size.cpp index cd9b9c37a..c3b07e0c5 100644 --- a/src/app/commands/cmd_sprite_size.cpp +++ b/src/app/commands/cmd_sprite_size.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -21,6 +21,7 @@ #include "app/sprite_job.h" #include "app/util/resize_image.h" #include "base/bind.h" +#include "base/clamp.h" #include "base/convert_to.h" #include "doc/algorithm/resize_image.h" #include "doc/cel.h" @@ -382,8 +383,8 @@ void SpriteSizeCommand::onExecute(Context* context) } #endif // ENABLE_UI - new_width = MID(1, new_width, DOC_SPRITE_MAX_WIDTH); - new_height = MID(1, new_height, DOC_SPRITE_MAX_HEIGHT); + new_width = base::clamp(new_width, 1, DOC_SPRITE_MAX_WIDTH); + new_height = base::clamp(new_height, 1, DOC_SPRITE_MAX_HEIGHT); { SpriteSizeJob job(reader, new_width, new_height, resize_method); diff --git a/src/app/commands/filters/color_curve_editor.cpp b/src/app/commands/filters/color_curve_editor.cpp index a4fbe317e..637156694 100644 --- a/src/app/commands/filters/color_curve_editor.cpp +++ b/src/app/commands/filters/color_curve_editor.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -11,6 +11,7 @@ #include "app/commands/filters/color_curve_editor.h" +#include "base/clamp.h" #include "filters/color_curve.h" #include "ui/alert.h" #include "ui/entry.h" @@ -139,8 +140,8 @@ bool ColorCurveEditor::onProcessMessage(Message* msg) if (m_editPoint) { gfx::Point mousePos = static_cast(msg)->position(); *m_editPoint = screenToView(mousePos); - m_editPoint->x = MID(m_viewBounds.x, m_editPoint->x, m_viewBounds.x+m_viewBounds.w-1); - m_editPoint->y = MID(m_viewBounds.y, m_editPoint->y, m_viewBounds.y+m_viewBounds.h-1); + m_editPoint->x = base::clamp(m_editPoint->x, m_viewBounds.x, m_viewBounds.x2()-1); + m_editPoint->y = base::clamp(m_editPoint->y, m_viewBounds.y, m_viewBounds.y2()-1); // TODO this should be optional CurveEditorChange(); @@ -208,9 +209,9 @@ void ColorCurveEditor::onPaint(ui::PaintEvent& ev) // Draw curve for (c = client.x; c < client.x+client.w; ++c) { pt = clientToView(gfx::Point(c, 0)); - pt.x = MID(m_viewBounds.x, pt.x, m_viewBounds.x+m_viewBounds.w-1); + pt.x = base::clamp(pt.x, m_viewBounds.x, m_viewBounds.x2()-1); pt.y = values[pt.x - m_viewBounds.x]; - pt.y = MID(m_viewBounds.y, pt.y, m_viewBounds.y+m_viewBounds.h-1); + pt.y = base::clamp(pt.y, m_viewBounds.y, m_viewBounds.y2()-1); pt = viewToClient(pt); g->putPixel(gfx::rgba(255, 255, 255), c, pt.y); @@ -269,8 +270,8 @@ bool ColorCurveEditor::editNodeManually(gfx::Point& viewPt) if (window.closer() == window.ok()) { viewPt.x = window.x()->textInt(); viewPt.y = window.y()->textInt(); - viewPt.x = MID(0, viewPt.x, 255); - viewPt.y = MID(0, viewPt.y, 255); + viewPt.x = base::clamp(viewPt.x, 0, 255); + viewPt.y = base::clamp(viewPt.y, 0, 255); return true; } else if (window.closer() == window.deleteButton()) { diff --git a/src/app/crash/read_document.cpp b/src/app/crash/read_document.cpp index 4e101a337..1d6c3d3df 100644 --- a/src/app/crash/read_document.cpp +++ b/src/app/crash/read_document.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018-2019 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -14,6 +14,7 @@ #include "app/console.h" #include "app/crash/internals.h" #include "app/doc.h" +#include "base/clamp.h" #include "base/convert_to.h" #include "base/exception.h" #include "base/fs.h" @@ -516,8 +517,8 @@ Doc* read_document_with_raw_images(const std::string& dir, info.height = 256; info.filename = "Unknown"; } - info.width = MID(1, info.width, 99999); - info.height = MID(1, info.height, 99999); + info.width = base::clamp(info.width, 1, 99999); + info.height = base::clamp(info.height, 1, 99999); Sprite* spr = new Sprite(ImageSpec(info.mode, info.width, info.height), 256); // Load each image as a new frame diff --git a/src/app/doc_exporter.cpp b/src/app/doc_exporter.cpp index bbb25a9b8..a6e8877c9 100644 --- a/src/app/doc_exporter.cpp +++ b/src/app/doc_exporter.cpp @@ -20,6 +20,7 @@ #include "app/restore_visible_layers.h" #include "app/snap_to_grid.h" #include "app/util/autocrop.h" +#include "base/clamp.h" #include "base/convert_to.h" #include "base/fs.h" #include "base/fstream_path.h" @@ -125,7 +126,7 @@ int DocExporter::Item::frames() const return selFrames->size(); else if (tag) { int result = tag->toFrame() - tag->fromFrame() + 1; - return MID(1, result, doc->sprite()->totalFrames()); + return base::clamp(result, 1, doc->sprite()->totalFrames()); } else return doc->sprite()->totalFrames(); @@ -138,8 +139,8 @@ doc::SelectedFrames DocExporter::Item::getSelectedFrames() const doc::SelectedFrames frames; if (tag) { - frames.insert(MID(0, tag->fromFrame(), doc->sprite()->lastFrame()), - MID(0, tag->toFrame(), doc->sprite()->lastFrame())); + frames.insert(base::clamp(tag->fromFrame(), 0, doc->sprite()->lastFrame()), + base::clamp(tag->toFrame(), 0, doc->sprite()->lastFrame())); } else { frames.insert(0, doc->sprite()->lastFrame()); diff --git a/src/app/file/ase_format.cpp b/src/app/file/ase_format.cpp index 6ea09c5bd..5ad37a036 100644 --- a/src/app/file/ase_format.cpp +++ b/src/app/file/ase_format.cpp @@ -16,6 +16,7 @@ #include "app/file/format_options.h" #include "app/pref/preferences.h" #include "base/cfile.h" +#include "base/clamp.h" #include "base/exception.h" #include "base/file_handle.h" #include "base/fs.h" @@ -962,8 +963,8 @@ static void ase_file_write_tags_chunk(FILE* f, tag->toFrame() < fromFrame) continue; - frame_t from = MID(0, tag->fromFrame()-fromFrame, toFrame-fromFrame); - frame_t to = MID(from, tag->toFrame()-fromFrame, toFrame-fromFrame); + frame_t from = base::clamp(tag->fromFrame()-fromFrame, 0, toFrame-fromFrame); + frame_t to = base::clamp(tag->toFrame()-fromFrame, from, toFrame-fromFrame); fputw(from, f); fputw(to, f); diff --git a/src/app/file/jpeg_format.cpp b/src/app/file/jpeg_format.cpp index 9887fb90d..a0db6e46f 100644 --- a/src/app/file/jpeg_format.cpp +++ b/src/app/file/jpeg_format.cpp @@ -19,6 +19,7 @@ #include "app/find_widget.h" #include "app/load_widget.h" #include "app/pref/preferences.h" +#include "base/clamp.h" #include "base/file_handle.h" #include "base/memory.h" #include "doc/doc.h" @@ -356,7 +357,7 @@ bool JpegFormat::onSave(FileOp* fop) JSAMPARRAY buffer; JDIMENSION buffer_height; const auto jpeg_options = std::static_pointer_cast(fop->formatOptions()); - const int qualityValue = (int)MID(0, 100.0f * jpeg_options->quality, 100); + const int qualityValue = (int)base::clamp(100.0f * jpeg_options->quality, 0.f, 100.f); int c; diff --git a/src/app/file/palette_file.cpp b/src/app/file/palette_file.cpp index aea82b0f6..2ae17a674 100644 --- a/src/app/file/palette_file.cpp +++ b/src/app/file/palette_file.cpp @@ -14,6 +14,7 @@ #include "app/file/file.h" #include "app/file/file_format.h" #include "app/file/file_formats_manager.h" +#include "base/clamp.h" #include "base/fs.h" #include "base/string.h" #include "dio/detect_format.h" @@ -149,7 +150,7 @@ bool save_palette(const char* filename, const Palette* pal, int columns) if (!ff || !ff->support(FILE_SUPPORT_SAVE)) break; - int w = (columns > 0 ? MID(0, columns, pal->size()): pal->size()); + int w = (columns > 0 ? base::clamp(columns, 0, pal->size()): pal->size()); int h = (pal->size() / w) + (pal->size() % w > 0 ? 1: 0); Context tmpContext; diff --git a/src/app/file/png_format.cpp b/src/app/file/png_format.cpp index e361097b0..b86b5dc15 100644 --- a/src/app/file/png_format.cpp +++ b/src/app/file/png_format.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018-2019 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -16,6 +16,7 @@ #include "app/file/format_options.h" #include "app/file/png_format.h" #include "app/file/png_options.h" +#include "base/clamp.h" #include "base/file_handle.h" #include "doc/doc.h" #include "gfx/color_space.h" @@ -613,7 +614,7 @@ bool PngFormat::onSave(FileOp* fop) if (color_type == PNG_COLOR_TYPE_PALETTE) { int c, r, g, b; int pal_size = fop->sequenceGetNColors(); - pal_size = MID(1, pal_size, PNG_MAX_PALETTE_LENGTH); + pal_size = base::clamp(pal_size, 1, PNG_MAX_PALETTE_LENGTH); #if PNG_MAX_PALETTE_LENGTH != 256 #error PNG_MAX_PALETTE_LENGTH should be 256 diff --git a/src/app/file/svg_format.cpp b/src/app/file/svg_format.cpp index f4b5c5589..af287b8ba 100644 --- a/src/app/file/svg_format.cpp +++ b/src/app/file/svg_format.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (c) 2018-2019 Igara Studio S.A. +// Copyright (c) 2018-2020 Igara Studio S.A. // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -17,6 +17,7 @@ #include "app/file/format_options.h" #include "app/pref/preferences.h" #include "base/cfile.h" +#include "base/clamp.h" #include "base/file_handle.h" #include "doc/doc.h" #include "ui/window.h" @@ -81,7 +82,7 @@ bool SvgFormat::onSave(FileOp* fop) const Image* image = fop->sequenceImage(); int x, y, c, r, g, b, a, alpha; const auto svg_options = std::static_pointer_cast(fop->formatOptions()); - const int pixelScaleValue = MID(0, svg_options->pixelScale, 10000); + const int pixelScaleValue = base::clamp(svg_options->pixelScale, 0, 10000); FileHandle handle(open_file_with_exception_sync_on_close(fop->filename(), "wb")); FILE* f = handle.get(); auto printcol = [f](int x, int y,int r, int g, int b, int a, int pxScale) { diff --git a/src/app/file/webp_format.cpp b/src/app/file/webp_format.cpp index 79e802df6..90fb7b4b7 100644 --- a/src/app/file/webp_format.cpp +++ b/src/app/file/webp_format.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018-2019 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // Copyright (C) 2015-2018 David Capello // Copyright (C) 2015 Gabriel Rauter // @@ -21,6 +21,7 @@ #include "app/ini_file.h" #include "app/pref/preferences.h" #include "base/bind.h" +#include "base/clamp.h" #include "base/convert_to.h" #include "base/file_handle.h" #include "doc/doc.h" @@ -242,8 +243,8 @@ static int progress_report(int percent, const WebPPicture* pic) FileOp* fop = wd->fop; double newProgress = (double(wd->f) + double(percent)/100.0) / double(wd->n); - wd->progress = MAX(wd->progress, newProgress); - wd->progress = MID(0.0, wd->progress, 1.0); + wd->progress = std::max(wd->progress, newProgress); + wd->progress = base::clamp(wd->progress, 0.0, 1.0); fop->setProgress(wd->progress); if (fop->isStop()) diff --git a/src/app/modules/gui.cpp b/src/app/modules/gui.cpp index 0c8c26547..2d4dcacf5 100644 --- a/src/app/modules/gui.cpp +++ b/src/app/modules/gui.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018-2019 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -121,7 +121,7 @@ static bool create_main_display(bool gpuAccel, try { if (w > 0 && h > 0) { main_display = os::instance()->createDisplay( - w, h, (scale == 0 ? 2: MID(1, scale, 4))); + w, h, (scale == 0 ? 2: base::clamp(scale, 1, 4))); } } catch (const os::DisplayCreationException& e) { diff --git a/src/app/site.cpp b/src/app/site.cpp index 214f00653..29ec340b1 100644 --- a/src/app/site.cpp +++ b/src/app/site.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -12,7 +12,7 @@ #include "app/site.h" #include "app/pref/preferences.h" -#include "base/base.h" +#include "base/clamp.h" #include "doc/cel.h" #include "doc/layer.h" #include "doc/sprite.h" @@ -57,7 +57,7 @@ Image* Site::image(int* x, int* y, int* opacity) const image = cel->image(); if (x) *x = cel->x(); if (y) *y = cel->y(); - if (opacity) *opacity = MID(0, cel->opacity(), 255); + if (opacity) *opacity = base::clamp(cel->opacity(), 0, 255); } } diff --git a/src/app/thumbnail_generator.cpp b/src/app/thumbnail_generator.cpp index ed9e65eba..ab6007c52 100644 --- a/src/app/thumbnail_generator.cpp +++ b/src/app/thumbnail_generator.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -17,6 +17,7 @@ #include "app/file/file.h" #include "app/file_system.h" #include "base/bind.h" +#include "base/clamp.h" #include "base/scoped_lock.h" #include "base/thread.h" #include "doc/algorithm/rotate.h" @@ -124,8 +125,8 @@ private: thumb_w = w; thumb_h = h; } - thumb_w = MID(1, thumb_w, MAX_THUMBNAIL_SIZE); - thumb_h = MID(1, thumb_h, MAX_THUMBNAIL_SIZE); + thumb_w = base::clamp(thumb_w, 1, MAX_THUMBNAIL_SIZE); + thumb_h = base::clamp(thumb_h, 1, MAX_THUMBNAIL_SIZE); // Stretch the 'image' thumbnailImage.reset( diff --git a/src/app/tools/ink_processing.h b/src/app/tools/ink_processing.h index f5e1af98c..2e73d84f0 100644 --- a/src/app/tools/ink_processing.h +++ b/src/app/tools/ink_processing.h @@ -8,6 +8,7 @@ #include "app/modules/palettes.h" #include "app/util/wrap_point.h" #include "app/util/wrap_value.h" +#include "base/clamp.h" #include "doc/blend_funcs.h" #include "doc/blend_internals.h" #include "doc/image_impl.h" @@ -651,8 +652,8 @@ private: m_srcImageHeight), pt, false); - pt.x = MID(0, pt.x, m_srcImageWidth-1); - pt.y = MID(0, pt.y, m_srcImageHeight-1); + pt.x = base::clamp(pt.x, 0, m_srcImageWidth-1); + pt.y = base::clamp(pt.y, 0, m_srcImageHeight-1); m_color = get_pixel(m_srcImage, pt.x, pt.y); } diff --git a/src/app/ui/color_bar.cpp b/src/app/ui/color_bar.cpp index 8f157c536..c3777b921 100644 --- a/src/app/ui/color_bar.cpp +++ b/src/app/ui/color_bar.cpp @@ -27,8 +27,8 @@ #include "app/commands/quick_command.h" #include "app/console.h" #include "app/context_access.h" -#include "app/doc_undo.h" #include "app/doc_api.h" +#include "app/doc_undo.h" #include "app/i18n/strings.h" #include "app/ini_file.h" #include "app/modules/editors.h" @@ -51,6 +51,7 @@ #include "app/ui_context.h" #include "app/util/clipboard.h" #include "base/bind.h" +#include "base/clamp.h" #include "base/scoped_value.h" #include "doc/cel.h" #include "doc/cels_range.h" @@ -1216,7 +1217,7 @@ void ColorBar::fixColorIndex(ColorButton& colorButton) if (color.getType() == Color::IndexType) { int oldIndex = color.getIndex(); - int newIndex = MID(0, oldIndex, get_current_palette()->size()-1); + int newIndex = base::clamp(oldIndex, 0, get_current_palette()->size()-1); if (oldIndex != newIndex) { color = Color::fromIndex(newIndex); colorButton.setColor(color); diff --git a/src/app/ui/color_button.cpp b/src/app/ui/color_button.cpp index 92293b131..76cf18512 100644 --- a/src/app/ui/color_button.cpp +++ b/src/app/ui/color_button.cpp @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -22,6 +23,7 @@ #include "app/ui/skin/skin_theme.h" #include "app/ui/status_bar.h" #include "app/ui_context.h" +#include "base/clamp.h" #include "doc/layer.h" #include "doc/sprite.h" #include "gfx/rect_io.h" @@ -302,7 +304,7 @@ void ColorButton::openPopup(const bool forcePinned) if (!pinned || (forcePinned && m_hiddenPopupBounds.isEmpty())) { winBounds = gfx::Rect(m_window->bounds().origin(), m_window->sizeHint()); - winBounds.x = MID(0, bounds().x, ui::display_w()-winBounds.w); + winBounds.x = base::clamp(bounds().x, 0, ui::display_w()-winBounds.w); if (bounds().y2() <= ui::display_h()-winBounds.h) winBounds.y = MAX(0, bounds().y2()); else @@ -314,8 +316,8 @@ void ColorButton::openPopup(const bool forcePinned) else { winBounds = m_windowDefaultBounds; } - winBounds.x = MID(0, winBounds.x, ui::display_w()-winBounds.w); - winBounds.y = MID(0, winBounds.y, ui::display_h()-winBounds.h); + winBounds.x = base::clamp(winBounds.x, 0, ui::display_w()-winBounds.w); + winBounds.y = base::clamp(winBounds.y, 0, ui::display_h()-winBounds.h); m_window->setBounds(winBounds); m_window->manager()->dispatchMessages(); diff --git a/src/app/ui/color_selector.cpp b/src/app/ui/color_selector.cpp index 5497add12..21b550a60 100644 --- a/src/app/ui/color_selector.cpp +++ b/src/app/ui/color_selector.cpp @@ -19,6 +19,7 @@ #include "app/modules/gfx.h" #include "app/ui/skin/skin_theme.h" #include "app/ui/status_bar.h" +#include "base/clamp.h" #include "base/concurrent_queue.h" #include "base/scoped_value.h" #include "base/thread.h" @@ -282,7 +283,7 @@ app::Color ColorSelector::getAlphaBarColor(const int u, const int umax) { int alpha = (255 * u / umax); app::Color color = m_color; - color.setAlpha(MID(0, alpha, 255)); + color.setAlpha(base::clamp(alpha, 0, 255)); return color; } diff --git a/src/app/ui/color_sliders.cpp b/src/app/ui/color_sliders.cpp index 550932b0e..5daefa62d 100644 --- a/src/app/ui/color_sliders.cpp +++ b/src/app/ui/color_sliders.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2018-2019 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -185,7 +185,7 @@ namespace { else ++value; - setTextf("%d", MID(minValue(), value, maxValue())); + setTextf("%d", base::clamp(value, minValue(), maxValue())); selectAllText(); onChange(); @@ -459,7 +459,7 @@ void ColorSliders::onEntryChange(const Channel i) Slider* slider = (m_mode == Mode::Absolute ? m_items[i].absSlider: m_items[i].relSlider); - value = MID(slider->getMinValue(), value, slider->getMaxValue()); + value = base::clamp(value, slider->getMinValue(), slider->getMaxValue()); slider->setValue(value); onControlChange(i); diff --git a/src/app/ui/color_spectrum.cpp b/src/app/ui/color_spectrum.cpp index 7f40ec801..2f36dda59 100644 --- a/src/app/ui/color_spectrum.cpp +++ b/src/app/ui/color_spectrum.cpp @@ -14,12 +14,13 @@ #include "app/color_utils.h" #include "app/ui/skin/skin_theme.h" #include "app/ui/status_bar.h" +#include "base/clamp.h" #include "os/surface.h" #include "ui/graphics.h" #include "ui/message.h" #include "ui/paint_event.h" -#include "ui/size_hint_event.h" #include "ui/resize_event.h" +#include "ui/size_hint_event.h" #include "ui/system.h" namespace app { @@ -38,9 +39,9 @@ app::Color ColorSpectrum::getMainAreaColor(const int u, const int umax, double hue = 360.0 * u / umax; double lit = 1.0 - (double(v)/double(vmax)); return app::Color::fromHsl( - MID(0.0, hue, 360.0), + base::clamp(hue, 0.0, 360.0), m_color.getHslSaturation(), - MID(0.0, lit, 1.0), + base::clamp(lit, 0.0, 1.0), getCurrentAlphaForNewColor()); } @@ -49,7 +50,7 @@ app::Color ColorSpectrum::getBottomBarColor(const int u, const int umax) double sat = double(u) / double(umax); return app::Color::fromHsl( m_color.getHslHue(), - MID(0.0, sat, 1.0), + base::clamp(sat, 0.0, 1.0), m_color.getHslLightness(), getCurrentAlphaForNewColor()); } @@ -97,9 +98,9 @@ void ColorSpectrum::onPaintSurfaceInBgThread( gfx::Color color = color_utils::color_for_ui( app::Color::fromHsl( - MID(0.0, hue, 360.0), + base::clamp(hue, 0.0, 360.0), sat, - MID(0.0, lit, 1.0))); + base::clamp(lit, 0.0, 1.0))); s->putPixel(color, main.x+x, main.y+y); } diff --git a/src/app/ui/color_tint_shade_tone.cpp b/src/app/ui/color_tint_shade_tone.cpp index 4b2c3c1d1..aa3af1ea7 100644 --- a/src/app/ui/color_tint_shade_tone.cpp +++ b/src/app/ui/color_tint_shade_tone.cpp @@ -90,8 +90,8 @@ void ColorTintShadeTone::onPaintSurfaceInBgThread( gfx::Color color = color_utils::color_for_ui( app::Color::fromHsv( hue, - MID(0.0, sat, 1.0), - MID(0.0, val, 1.0))); + base::clamp(sat, 0.0, 1.0), + base::clamp(val, 0.0, 1.0))); s->putPixel(color, main.x+x, main.y+y); } diff --git a/src/app/ui/color_wheel.cpp b/src/app/ui/color_wheel.cpp index 82759f5c7..cac5846c3 100644 --- a/src/app/ui/color_wheel.cpp +++ b/src/app/ui/color_wheel.cpp @@ -97,9 +97,9 @@ app::Color ColorWheel::getMainAreaColor(const int _u, const int umax, int b = 255 - di; if (d < m_wheelRadius+2*guiscale()) { return app::Color::fromRgb( - MID(0, r, 255), - MID(0, g, 255), - MID(128, b, 255)); + base::clamp(r, 0, 255), + base::clamp(g, 0, 255), + base::clamp(b, 128, 255)); } else { return app::Color::fromRgb(128, 128, 255); @@ -190,7 +190,7 @@ void ColorWheel::onPaintMainArea(ui::Graphics* g, const gfx::Rect& rc) double angle = std::atan2(m_color.getGreen()-128, m_color.getRed()-128); double dist = (255-m_color.getBlue()) / 128.0; - dist = MID(0.0, dist, 1.0); + dist = base::clamp(dist, 0.0, 1.0); gfx::Point pos = m_wheelBounds.center() + @@ -334,18 +334,18 @@ void ColorWheel::setHarmony(Harmony harmony) int ColorWheel::getHarmonies() const { - int i = MID(0, (int)m_harmony, (int)Harmony::LAST); + int i = base::clamp((int)m_harmony, 0, (int)Harmony::LAST); return harmonies[i].n; } app::Color ColorWheel::getColorInHarmony(int j) const { - int i = MID(0, (int)m_harmony, (int)Harmony::LAST); - j = MID(0, j, harmonies[i].n-1); + int i = base::clamp((int)m_harmony, 0, (int)Harmony::LAST); + j = base::clamp(j, 0, harmonies[i].n-1); double hue = convertHueAngle(int(m_color.getHsvHue()), -1) + harmonies[i].hues[j]; double sat = m_color.getHsvSaturation() * harmonies[i].sats[j] / 100.0; return app::Color::fromHsv(std::fmod(hue, 360), - MID(0.0, sat, 1.0), + base::clamp(sat, 0.0, 1.0), m_color.getHsvValue()); } diff --git a/src/app/ui/context_bar.cpp b/src/app/ui/context_bar.cpp index 145f5d5c6..2cd683a8c 100644 --- a/src/app/ui/context_bar.cpp +++ b/src/app/ui/context_bar.cpp @@ -46,6 +46,7 @@ #include "app/ui/skin/skin_theme.h" #include "app/ui_context.h" #include "base/bind.h" +#include "base/clamp.h" #include "base/fs.h" #include "base/scoped_value.h" #include "doc/brush.h" @@ -643,7 +644,7 @@ private: char buf[32]; int n = get_config_int("shades", "count", 0); - n = MID(0, n, 256); + n = base::clamp(n, 0, 256); for (int i=0; iwidth()-expose.x); const int maxh = std::max(0, m_sprite->height()-expose.y); - expose.w = MID(0, expose.w, maxw); - expose.h = MID(0, expose.h, maxh); + expose.w = base::clamp(expose.w, 0, maxw); + expose.h = base::clamp(expose.h, 0, maxh); if (expose.isEmpty()) return; @@ -735,7 +735,7 @@ void Editor::drawOneSpriteUnclippedRect(ui::Graphics* g, const gfx::Rect& sprite if (m_docPref.pixelGrid.autoOpacity()) { alpha = int(alpha * (m_proj.zoom().scale()-2.) / (16.-2.)); - alpha = MID(0, alpha, 255); + alpha = base::clamp(alpha, 0, 255); } drawGrid(g, enclosingRect, Rect(0, 0, 1, 1), @@ -757,7 +757,7 @@ void Editor::drawOneSpriteUnclippedRect(ui::Graphics* g, const gfx::Rect& sprite double len = (m_proj.applyX(gridrc.w) + m_proj.applyY(gridrc.h)) / 2.; alpha = int(alpha * len / 32.); - alpha = MID(0, alpha, 255); + alpha = base::clamp(alpha, 0, 255); } if (alpha > 8) { @@ -1345,8 +1345,8 @@ gfx::Point Editor::autoScroll(MouseMessage* msg, AutoScroll dir) m_oldPos = mousePos; mousePos = gfx::Point( - MID(vp.x, mousePos.x, vp.x+vp.w-1), - MID(vp.y, mousePos.y, vp.y+vp.h-1)); + base::clamp(mousePos.x, vp.x, vp.x2()-1), + base::clamp(mousePos.y, vp.y, vp.y2()-1)); } else m_oldPos = mousePos; @@ -2430,7 +2430,7 @@ void Editor::pasteImage(const Image* image, const Mask* mask) // In other case, if the center is visible, we put the pasted // image in its original location. else { - x = MID(visibleBounds.x-image->width(), x, visibleBounds.x+visibleBounds.w-1); + x = base::clamp(x, visibleBounds.x-image->width(), visibleBounds.x2()-1); } if (maskCenter.y < visibleBounds.y || @@ -2438,19 +2438,21 @@ void Editor::pasteImage(const Image* image, const Mask* mask) y = visibleBounds.y + visibleBounds.h/2 - image->height()/2; } else { - y = MID(visibleBounds.y-image->height(), y, visibleBounds.y+visibleBounds.h-1); + y = base::clamp(y, visibleBounds.y-image->height(), visibleBounds.y2()-1); } // Limit the image inside the sprite's bounds. if (sprite->width() <= image->width() || sprite->height() <= image->height()) { - x = MID(0, x, sprite->width() - image->width()); - y = MID(0, y, sprite->height() - image->height()); + // TODO review this (I think limits are wrong and high limit can + // be negative here) + x = base::clamp(x, 0, sprite->width() - image->width()); + y = base::clamp(y, 0, sprite->height() - image->height()); } else { // Also we always limit the 1 image pixel inside the sprite's bounds. - x = MID(-image->width()+1, x, sprite->width()-1); - y = MID(-image->height()+1, y, sprite->height()-1); + x = base::clamp(x, -image->width()+1, sprite->width()-1); + y = base::clamp(y, -image->height()+1, sprite->height()-1); } } diff --git a/src/app/ui/editor/moving_symmetry_state.cpp b/src/app/ui/editor/moving_symmetry_state.cpp index f012c1413..5fd7f0632 100644 --- a/src/app/ui/editor/moving_symmetry_state.cpp +++ b/src/app/ui/editor/moving_symmetry_state.cpp @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2020 Igara Studio S.A. // Copyright (C) 2015-2016 David Capello // // This program is distributed under the terms of @@ -12,6 +13,7 @@ #include "app/ui/editor/editor.h" #include "app/ui/status_bar.h" +#include "base/clamp.h" #include "ui/message.h" #include @@ -48,12 +50,12 @@ bool MovingSymmetryState::onMouseMove(Editor* editor, MouseMessage* msg) case app::gen::SymmetryMode::HORIZONTAL: pos = m_symmetryAxisStart + delta.x; pos = std::round(pos*2.0)/2.0; - pos = MID(1.0, pos, editor->sprite()->width()-1.0); + pos = base::clamp(pos, 1.0, editor->sprite()->width()-1.0); break; case app::gen::SymmetryMode::VERTICAL: pos = m_symmetryAxisStart + delta.y; pos = std::round(pos*2.0)/2.0; - pos = MID(1.0, pos, editor->sprite()->height()-1.0); + pos = base::clamp(pos, 1.0, editor->sprite()->height()-1.0); break; } m_symmetryAxis(pos); diff --git a/src/app/ui/editor/state_with_wheel_behavior.cpp b/src/app/ui/editor/state_with_wheel_behavior.cpp index 009da561d..dd2cadecc 100644 --- a/src/app/ui/editor/state_with_wheel_behavior.cpp +++ b/src/app/ui/editor/state_with_wheel_behavior.cpp @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2020 Igara Studio S.A. // Copyright (C) 2001-2017 David Capello // // This program is distributed under the terms of @@ -23,6 +24,7 @@ #include "app/ui/keyboard_shortcuts.h" #include "app/ui/toolbar.h" #include "app/ui_context.h" +#include "base/clamp.h" #include "base/string.h" #include "doc/layer.h" #include "doc/palette.h" @@ -114,7 +116,7 @@ bool StateWithWheelBehavior::onMouseWheel(Editor* editor, MouseMessage* msg) case WheelAction::FgColor: { int lastIndex = get_current_palette()->size()-1; int newIndex = ColorBar::instance()->getFgColor().getIndex() + int(dz); - newIndex = MID(0, newIndex, lastIndex); + newIndex = base::clamp(newIndex, 0, lastIndex); ColorBar::instance()->setFgColor(app::Color::fromIndex(newIndex)); break; } @@ -122,7 +124,7 @@ bool StateWithWheelBehavior::onMouseWheel(Editor* editor, MouseMessage* msg) case WheelAction::BgColor: { int lastIndex = get_current_palette()->size()-1; int newIndex = ColorBar::instance()->getBgColor().getIndex() + int(dz); - newIndex = MID(0, newIndex, lastIndex); + newIndex = base::clamp(newIndex, 0, lastIndex); ColorBar::instance()->setBgColor(app::Color::fromIndex(newIndex)); break; } @@ -187,9 +189,9 @@ bool StateWithWheelBehavior::onMouseWheel(Editor* editor, MouseMessage* msg) ToolPreferences::Brush& brush = Preferences::instance().tool(tool).brush; - brush.size(MID(doc::Brush::kMinBrushSize, - brush.size()+dz, - doc::Brush::kMaxBrushSize)); + brush.size(base::clamp(int(brush.size()+dz), + doc::Brush::kMinBrushSize, + doc::Brush::kMaxBrushSize)); break; } @@ -203,7 +205,7 @@ bool StateWithWheelBehavior::onMouseWheel(Editor* editor, MouseMessage* msg) angle += 180; angle %= 181; - brush.angle(MID(0, angle, 180)); + brush.angle(base::clamp(angle, 0, 180)); break; } @@ -274,7 +276,7 @@ bool StateWithWheelBehavior::onMouseWheel(Editor* editor, MouseMessage* msg) tools::Tool* tool = getActiveTool(); auto& toolPref = Preferences::instance().tool(tool); int opacity = toolPref.opacity(); - opacity = MID(0, opacity+dz*255/10, 255); + opacity = base::clamp(int(opacity+dz*255/10), 0, 255); toolPref.opacity(opacity); break; } @@ -287,7 +289,7 @@ bool StateWithWheelBehavior::onMouseWheel(Editor* editor, MouseMessage* msg) Command* command = Commands::instance()->byId(CommandId::LayerOpacity()); if (command) { int opacity = static_cast(site.layer())->opacity(); - opacity = MID(0, opacity+dz*255/10, 255); + opacity = base::clamp(int(opacity+dz*255/10), 0, 255); Params params; params.set("opacity", @@ -307,7 +309,7 @@ bool StateWithWheelBehavior::onMouseWheel(Editor* editor, MouseMessage* msg) Command* command = Commands::instance()->byId(CommandId::CelOpacity()); if (command) { int opacity = site.cel()->opacity(); - opacity = MID(0, opacity+dz*255/10, 255); + opacity = base::clamp(int(opacity+dz*255/10), 0, 255); Params params; params.set("opacity", base::convert_to(opacity).c_str()); @@ -323,7 +325,7 @@ bool StateWithWheelBehavior::onMouseWheel(Editor* editor, MouseMessage* msg) ColorBar* colorBar = ColorBar::instance(); Color c = colorBar->getFgColor(); int a = c.getAlpha(); - a = MID(0, a+dz*255/10, 255); + a = base::clamp(int(a+dz*255/10), 0, 255); c.setAlpha(a); colorBar->setFgColor(c); break; @@ -345,9 +347,9 @@ bool StateWithWheelBehavior::onMouseWheel(Editor* editor, MouseMessage* msg) case WheelAction::HslSaturation: s = s+dz/10.0; break; case WheelAction::HslLightness: l = l+dz/10.0; break; } - colorBar->setFgColor(Color::fromHsl(MID(0.0, h, 360.0), - MID(0.0, s, 1.0), - MID(0.0, l, 1.0))); + colorBar->setFgColor(Color::fromHsl(base::clamp(h, 0.0, 360.0), + base::clamp(s, 0.0, 1.0), + base::clamp(l, 0.0, 1.0))); break; } @@ -367,9 +369,9 @@ bool StateWithWheelBehavior::onMouseWheel(Editor* editor, MouseMessage* msg) case WheelAction::HsvSaturation: s = s+dz/10.0; break; case WheelAction::HsvValue: v = v+dz/10.0; break; } - colorBar->setFgColor(Color::fromHsv(MID(0.0, h, 360.0), - MID(0.0, s, 1.0), - MID(0.0, v, 1.0))); + colorBar->setFgColor(Color::fromHsv(base::clamp(h, 0.0, 360.0), + base::clamp(s, 0.0, 1.0), + base::clamp(v, 0.0, 1.0))); break; } diff --git a/src/app/ui/file_list.cpp b/src/app/ui/file_list.cpp index e40a5ac85..7649e9009 100644 --- a/src/app/ui/file_list.cpp +++ b/src/app/ui/file_list.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This program is distributed under the terms of @@ -358,7 +358,7 @@ bool FileList::onProcessMessage(Message* msg) } if (bottom > 0) - selectIndex(MID(0, select, bottom-1)); + selectIndex(base::clamp(select, 0, bottom-1)); return true; } diff --git a/src/app/ui/palette_view.cpp b/src/app/ui/palette_view.cpp index 46b392369..fbe73e660 100644 --- a/src/app/ui/palette_view.cpp +++ b/src/app/ui/palette_view.cpp @@ -221,7 +221,7 @@ int PaletteView::getBoxSize() const void PaletteView::setBoxSize(double boxsize) { - m_boxsize = MID(4.0, boxsize, 32.0); + m_boxsize = base::clamp(boxsize, 4.0, 32.0); if (m_delegate) m_delegate->onPaletteViewChangeSize(int(m_boxsize)); @@ -784,7 +784,8 @@ PaletteView::Hit PaletteView::hitTest(const gfx::Point& pos) int colsLimit = m_columns; if (m_state == State::DRAGGING_OUTLINE) --colsLimit; - int i = MID(0, (pos.x-vp.x)/box.w, colsLimit) + MAX(0, pos.y/box.h)*m_columns; + int i = base::clamp((pos.x-vp.x)/box.w, 0, colsLimit) + + std::max(0, pos.y/box.h)*m_columns; return Hit(Hit::POSSIBLE_COLOR, i); } diff --git a/src/app/ui/skin/skin_theme.cpp b/src/app/ui/skin/skin_theme.cpp index f9f0a8cd8..263b7c7ed 100644 --- a/src/app/ui/skin/skin_theme.cpp +++ b/src/app/ui/skin/skin_theme.cpp @@ -26,6 +26,7 @@ #include "app/xml_document.h" #include "app/xml_exception.h" #include "base/bind.h" +#include "base/clamp.h" #include "base/fs.h" #include "base/log.h" #include "base/string.h" @@ -1634,7 +1635,7 @@ void SkinTheme::paintProgressBar(ui::Graphics* g, const gfx::Rect& rc0, double p rc.shrink(1); int u = (int)((double)rc.w*progress); - u = MID(0, u, rc.w); + u = base::clamp(u, 0, rc.w); if (u > 0) g->fillRect(colors.selected(), gfx::Rect(rc.x, rc.y, u, rc.h)); diff --git a/src/app/ui/slider2.cpp b/src/app/ui/slider2.cpp index eba4c9fcd..7e99947a4 100644 --- a/src/app/ui/slider2.cpp +++ b/src/app/ui/slider2.cpp @@ -1,4 +1,5 @@ // Aseprite +// Copyright (C) 2020 Igara Studio S.A. // Copyright (C) 2017 David Capello // // This program is distributed under the terms of @@ -12,6 +13,7 @@ #include "app/ui/skin/skin_property.h" #include "base/bind.h" +#include "base/clamp.h" #include "ui/manager.h" #include "ui/message.h" @@ -55,7 +57,7 @@ bool Slider2::Slider2Entry::onProcessMessage(ui::Message* msg) else ++value; - setTextf("%d", MID(minValue(), value, maxValue())); + setTextf("%d", base::clamp(value, minValue(), maxValue())); selectAllText(); onChange(); @@ -118,7 +120,7 @@ void Slider2::onSliderChange() void Slider2::onEntryChange() { int v = m_entry.textInt(); - v = MID(m_slider.getMinValue(), v, m_slider.getMaxValue()); + v = base::clamp(v, m_slider.getMinValue(), m_slider.getMaxValue()); m_slider.setValue(v); onChange(); diff --git a/src/app/ui/tabs.cpp b/src/app/ui/tabs.cpp index 2485a32fc..30fe57312 100644 --- a/src/app/ui/tabs.cpp +++ b/src/app/ui/tabs.cpp @@ -15,6 +15,7 @@ #include "app/modules/gui.h" #include "app/ui/editor/editor_view.h" #include "app/ui/skin/skin_theme.h" +#include "base/clamp.h" #include "os/font.h" #include "os/surface.h" #include "os/system.h" @@ -258,7 +259,7 @@ void Tabs::setDropViewPreview(const gfx::Point& pos, TabView* view) if (!m_list.empty()) { newIndex = (pos.x - bounds().x) / m_list[0]->width; - newIndex = MID(0, newIndex, (int)m_list.size()); + newIndex = base::clamp(newIndex, 0, (int)m_list.size()); } else newIndex = 0; @@ -429,7 +430,7 @@ bool Tabs::onProcessMessage(Message* msg) if (it != m_list.end()) { int index = (it - m_list.begin()); int newIndex = index + dz; - newIndex = MID(0, newIndex, int(m_list.size())-1); + newIndex = base::clamp(newIndex, 0, int(m_list.size())-1); if (newIndex != index) { selectTabInternal(m_list[newIndex]); } @@ -992,14 +993,14 @@ void Tabs::updateDragTabIndexes(int mouseX, bool startAni) int i = (mouseX - m_border*guiscale() - bounds().x) / m_dragTab->width; if (m_dragCopy) { - i = MID(0, i, int(m_list.size())); + i = base::clamp(i, 0, int(m_list.size())); if (i != m_dragCopyIndex) { m_dragCopyIndex = i; startAni = true; } } else if (hasMouseOver()) { - i = MID(0, i, int(m_list.size())-1); + i = base::clamp(i, 0, int(m_list.size())-1); if (i != m_dragTabIndex) { m_list.erase(m_list.begin()+m_dragTabIndex); m_list.insert(m_list.begin()+i, m_selected); diff --git a/src/app/ui/tag_window.cpp b/src/app/ui/tag_window.cpp index 08a14a597..9e2a71cf4 100644 --- a/src/app/ui/tag_window.cpp +++ b/src/app/ui/tag_window.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2016 David Capello // // This program is distributed under the terms of @@ -14,6 +14,7 @@ #include "app/doc.h" #include "app/pref/preferences.h" #include "app/ui/layer_frame_comboboxes.h" +#include "base/clamp.h" #include "doc/sprite.h" #include "doc/tag.h" @@ -53,8 +54,8 @@ void TagWindow::rangeValue(doc::frame_t& from, doc::frame_t& to) from = this->from()->textInt()-m_base; to = this->to()->textInt()-m_base; - from = MID(first, from, last); - to = MID(from, to, last); + from = base::clamp(from, first, last); + to = base::clamp(to, from, last); } doc::color_t TagWindow::colorValue() diff --git a/src/app/ui/timeline/timeline.cpp b/src/app/ui/timeline/timeline.cpp index b5ab27fe7..ee8cfa66f 100644 --- a/src/app/ui/timeline/timeline.cpp +++ b/src/app/ui/timeline/timeline.cpp @@ -284,7 +284,7 @@ Timeline::~Timeline() void Timeline::setZoom(const double zoom) { - m_zoom = MID(1.0, zoom, 10.0); + m_zoom = base::clamp(zoom, 1.0, 10.0); m_thumbnailsOverlayDirection = gfx::Point(int(frameBoxWidth()*1.0), int(frameBoxWidth()*0.5)); m_thumbnailsOverlayVisible = false; } @@ -944,7 +944,7 @@ bool Timeline::onProcessMessage(Message* msg) if (selectedLayersBounds(selectedLayers(), &layerFirst, &layerLast)) { layer_t layerIdx = m_clk.layer; - layerIdx = MID(layerFirst, layerIdx, layerLast); + layerIdx = base::clamp(layerIdx, layerFirst, layerLast); m_clk.layer = layerIdx; } } @@ -1885,20 +1885,21 @@ void Timeline::setCursor(ui::Message* msg, const Hit& hit) } } -void Timeline::getDrawableLayers(layer_t* firstLayer, layer_t* lastLayer) +void Timeline::getDrawableLayers(layer_t* firstDrawableLayer, + layer_t* lastDrawableLayer) { - layer_t i = this->lastLayer() + layer_t i = lastLayer() - ((viewScroll().y + getCelsBounds().h) / layerBoxHeight()); - i = MID(this->firstLayer(), i, this->lastLayer()); + i = base::clamp(i, firstLayer(), lastLayer()); - layer_t j = this->lastLayer() - viewScroll().y / layerBoxHeight();; + layer_t j = lastLayer() - viewScroll().y / layerBoxHeight();; if (!m_rows.empty()) - j = MID(this->firstLayer(), j, this->lastLayer()); + j = base::clamp(j, firstLayer(), lastLayer()); else j = -1; - *firstLayer = i; - *lastLayer = j; + *firstDrawableLayer = i; + *lastDrawableLayer = j; } void Timeline::getDrawableFrames(frame_t* firstFrame, frame_t* lastFrame) @@ -2476,9 +2477,9 @@ void Timeline::drawTags(ui::Graphics* g) r = gfx::getr(bg)+32; g = gfx::getg(bg)+32; b = gfx::getb(bg)+32; - r = MID(0, r, 255); - g = MID(0, g, 255); - b = MID(0, b, 255); + r = base::clamp(r, 0, 255); + g = base::clamp(g, 0, 255); + b = base::clamp(b, 0, 255); bg = gfx::rgba(r, g, b, gfx::geta(bg)); } g->fillRect(bg, bounds); @@ -3049,11 +3050,11 @@ Timeline::Hit Timeline::hitTest(ui::Message* msg, const gfx::Point& mousePos) hit.veryBottom = true; if (hasCapture()) { - hit.layer = MID(firstLayer(), hit.layer, lastLayer()); + hit.layer = base::clamp(hit.layer, firstLayer(), lastLayer()); if (isMovingCel()) - hit.frame = MAX(firstFrame(), hit.frame); + hit.frame = std::max(firstFrame(), hit.frame); else - hit.frame = MID(firstFrame(), hit.frame, lastFrame()); + hit.frame = base::clamp(hit.frame, firstFrame(), lastFrame()); } else { if (hit.layer > lastLayer()) hit.layer = -1; @@ -3227,8 +3228,8 @@ Timeline::Hit Timeline::hitTestCel(const gfx::Point& mousePos) - m_separator_w + scroll.x) / frameBoxWidth()); - hit.layer = MID(firstLayer(), hit.layer, lastLayer()); - hit.frame = MAX(firstFrame(), hit.frame); + hit.layer = base::clamp(hit.layer, firstLayer(), lastLayer()); + hit.frame = std::max(firstFrame(), hit.frame); return hit; } @@ -3736,8 +3737,8 @@ void Timeline::setViewScroll(const gfx::Point& pt) const gfx::Point oldScroll = viewScroll(); const gfx::Point maxPos = getMaxScrollablePos(); gfx::Point newScroll = pt; - newScroll.x = MID(0, newScroll.x, maxPos.x); - newScroll.y = MID(0, newScroll.y, maxPos.y); + newScroll.x = base::clamp(newScroll.x, 0, maxPos.x); + newScroll.y = base::clamp(newScroll.y, 0, maxPos.y); if (newScroll.y != oldScroll.y) { gfx::Rect rc; diff --git a/src/doc/algo.cpp b/src/doc/algo.cpp index e5340bcf7..149ebe34f 100644 --- a/src/doc/algo.cpp +++ b/src/doc/algo.cpp @@ -1,5 +1,5 @@ // Aseprite Document Library -// Copyright (C) 2018 Igara Studio S.A. +// Copyright (c) 2018-2020 Igara Studio S.A. // Copyright (c) 2001-2018 David Capello // // This file is released under the terms of the MIT license. @@ -11,7 +11,7 @@ #include "doc/algo.h" -#include "base/base.h" +#include "base/clamp.h" #include "base/debug.h" #include @@ -249,7 +249,7 @@ static void draw_rotated_ellipse_rect(int x0, int y0, int x1, int y1, double zd, if (w != 0.0) w = (w-zd) / (w+w); // squared weight of P1 - w = MID(0.0, w, 1.0); + w = base::clamp(w, 0.0, 1.0); xd = std::floor(w*xd + 0.5); yd = std::floor(w*yd + 0.5); @@ -285,7 +285,7 @@ void fill_rotated_ellipse(int cx, int cy, int a, int b, double angle, void* data Rows(int y0, int nrows) : y0(y0), row(nrows, std::make_pair(1, -1)) { } void update(int x, int y) { - int i = MID(0, y-y0, row.size()-1); + int i = base::clamp(y-y0, 0, int(row.size()-1)); auto& r = row[i]; if (r.first > r.second) { r.first = r.second = x; diff --git a/src/doc/blend_funcs.cpp b/src/doc/blend_funcs.cpp index 56ab26408..1a92e346e 100644 --- a/src/doc/blend_funcs.cpp +++ b/src/doc/blend_funcs.cpp @@ -1,5 +1,5 @@ // Aseprite Document Library -// Copyright (c) 2019 Igara Studio S.A. +// Copyright (c) 2019-2020 Igara Studio S.A. // Copyright (c) 2001-2017 David Capello // // This file is released under the terms of the MIT license. @@ -396,8 +396,14 @@ static void set_lum(double& r, double& g, double& b, double l) clip_color(r, g, b); } +// TODO replace this with a better impl (and test this, not sure if it's correct) static void set_sat(double& r, double& g, double& b, double s) { +#undef MID +#define MID(x,y,z) ((x) > (y) ? ((y) > (z) ? (y) : ((x) > (z) ? \ + (z) : (x))) : ((y) > (z) ? ((z) > (x) ? (z) : \ + (x)): (y))) + double& min = MIN(r, MIN(g, b)); double& mid = MID(r, g, b); double& max = MAX(r, MAX(g, b)); diff --git a/src/doc/handle_anidir.cpp b/src/doc/handle_anidir.cpp index 357949931..2a427ff87 100644 --- a/src/doc/handle_anidir.cpp +++ b/src/doc/handle_anidir.cpp @@ -1,5 +1,5 @@ // Aseprite Document Library -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2016 David Capello // // This file is released under the terms of the MIT license. @@ -11,7 +11,7 @@ #include "doc/handle_anidir.h" -#include "base/base.h" +#include "base/clamp.h" #include "doc/frame.h" #include "doc/sprite.h" #include "doc/tag.h" @@ -37,8 +37,8 @@ frame_t calculate_next_frame( loopFrom = tag->fromFrame(); loopTo = tag->toFrame(); - loopFrom = MID(first, loopFrom, last); - loopTo = MID(first, loopTo, last); + loopFrom = base::clamp(loopFrom, first, last); + loopTo = base::clamp(loopTo, first, last); first = loopFrom; last = loopTo; diff --git a/src/doc/mask.cpp b/src/doc/mask.cpp index 65487ce1a..bc67aa0c3 100644 --- a/src/doc/mask.cpp +++ b/src/doc/mask.cpp @@ -1,5 +1,5 @@ // Aseprite Document Library -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This file is released under the terms of the MIT license. @@ -11,7 +11,7 @@ #include "doc/mask.h" -#include "base/base.h" +#include "base/clamp.h" #include "base/memory.h" #include "doc/image_impl.h" @@ -386,10 +386,10 @@ void Mask::crop(const Image *image) beg_x2 = beg_x1 + m_bounds.w - 1; beg_y2 = beg_y1 + m_bounds.h - 1; - beg_x1 = MID(0, beg_x1, m_bounds.w-1); - beg_y1 = MID(0, beg_y1, m_bounds.h-1); - beg_x2 = MID(beg_x1, beg_x2, m_bounds.w-1); - beg_y2 = MID(beg_y1, beg_y2, m_bounds.h-1); + beg_x1 = base::clamp(beg_x1, 0, m_bounds.w-1); + beg_y1 = base::clamp(beg_y1, 0, m_bounds.h-1); + beg_x2 = base::clamp(beg_x2, beg_x1, m_bounds.w-1); + beg_y2 = base::clamp(beg_y2, beg_y1, m_bounds.h-1); /* left */ ADVANCE(x1, x2, y2, <=, ++, diff --git a/src/doc/sprite.cpp b/src/doc/sprite.cpp index 5fa9139c3..80f180577 100644 --- a/src/doc/sprite.cpp +++ b/src/doc/sprite.cpp @@ -1,5 +1,5 @@ // Aseprite Document Library -// Copyright (C) 2018-2019 Igara Studio S.A. +// Copyright (C) 2018-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This file is released under the terms of the MIT license. @@ -11,7 +11,7 @@ #include "doc/sprite.h" -#include "base/base.h" +#include "base/clamp.h" #include "base/memory.h" #include "base/remove_from_container.h" #include "doc/cel.h" @@ -77,7 +77,7 @@ Sprite::Sprite(const ImageSpec& spec, case ColorMode::BITMAP: for (int c=0; c= 0 && frame < m_frames) - m_frlens[frame] = MID(1, msecs, 65535); + m_frlens[frame] = base::clamp(msecs, 1, 65535); } void Sprite::setFrameRangeDuration(frame_t from, frame_t to, int msecs) { std::fill( m_frlens.begin()+(std::size_t)from, - m_frlens.begin()+(std::size_t)to+1, MID(1, msecs, 65535)); + m_frlens.begin()+(std::size_t)to+1, base::clamp(msecs, 1, 65535)); } void Sprite::setDurationForAllFrames(int msecs) { - std::fill(m_frlens.begin(), m_frlens.end(), MID(1, msecs, 65535)); + std::fill(m_frlens.begin(), m_frlens.end(), base::clamp(msecs, 1, 65535)); } ////////////////////////////////////////////////////////////////////// diff --git a/src/filters/brightness_contrast_filter.cpp b/src/filters/brightness_contrast_filter.cpp index e09588ca2..30b2b5a2f 100644 --- a/src/filters/brightness_contrast_filter.cpp +++ b/src/filters/brightness_contrast_filter.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2017 David Capello // // This program is distributed under the terms of @@ -11,6 +11,7 @@ #include "filters/brightness_contrast_filter.h" +#include "base/clamp.h" #include "doc/image.h" #include "doc/palette.h" #include "doc/rgbmap.h" @@ -188,7 +189,7 @@ void BrightnessContrastFilter::updateMap() double x = double(u) / double(max-1); double y = (m_contrast+1.0) * (x - 0.5) + 0.5; y = y*(1.0+m_brightness); - y = MID(0.0, y, 1.0); + y = base::clamp(y, 0.0, 1.0); m_cmap[u] = int(255.5 * y); } } diff --git a/src/filters/color_curve_filter.cpp b/src/filters/color_curve_filter.cpp index 4d143a628..7d28fecd2 100644 --- a/src/filters/color_curve_filter.cpp +++ b/src/filters/color_curve_filter.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2016 David Capello // // This program is distributed under the terms of @@ -11,7 +11,7 @@ #include "filters/color_curve_filter.h" -#include "base/base.h" +#include "base/clamp.h" #include "filters/color_curve.h" #include "filters/filter_indexed_data.h" #include "filters/filter_manager.h" @@ -42,7 +42,7 @@ void ColorCurveFilter::generateMap() // Generate the color convertion map m_curve.getValues(0, 255, m_cmap); for (int c=0; c<256; c++) - m_cmap[c] = MID(0, m_cmap[c], 255); + m_cmap[c] = base::clamp(m_cmap[c], 0, 255); } const char* ColorCurveFilter::getName() @@ -145,7 +145,7 @@ void ColorCurveFilter::applyToIndexed(FilterManager* filterMgr) c = rgbmap->mapColor(r, g, b, a); } - *(dst_address++) = MID(0, c, pal->size()-1); + *(dst_address++) = base::clamp(c, 0, pal->size()-1); } } diff --git a/src/filters/convolution_matrix_filter.cpp b/src/filters/convolution_matrix_filter.cpp index 0b7fee3bf..d7fa53312 100644 --- a/src/filters/convolution_matrix_filter.cpp +++ b/src/filters/convolution_matrix_filter.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2016 David Capello // // This program is distributed under the terms of @@ -11,7 +11,7 @@ #include "filters/convolution_matrix_filter.h" -#include "base/base.h" +#include "base/clamp.h" #include "filters/convolution_matrix.h" #include "filters/filter_indexed_data.h" #include "filters/filter_manager.h" @@ -168,28 +168,28 @@ void ConvolutionMatrixFilter::applyToRgba(FilterManager* filterMgr) if (target & TARGET_RED_CHANNEL) { delegate.r = delegate.r / delegate.div + m_matrix->getBias(); - delegate.r = MID(0, delegate.r, 255); + delegate.r = base::clamp(delegate.r, 0, 255); } else delegate.r = rgba_getr(color); if (target & TARGET_GREEN_CHANNEL) { delegate.g = delegate.g / delegate.div + m_matrix->getBias(); - delegate.g = MID(0, delegate.g, 255); + delegate.g = base::clamp(delegate.g, 0, 255); } else delegate.g = rgba_getg(color); if (target & TARGET_BLUE_CHANNEL) { delegate.b = delegate.b / delegate.div + m_matrix->getBias(); - delegate.b = MID(0, delegate.b, 255); + delegate.b = base::clamp(delegate.b, 0, 255); } else delegate.b = rgba_getb(color); if (target & TARGET_ALPHA_CHANNEL) { delegate.a = delegate.a / m_matrix->getDiv() + m_matrix->getBias(); - delegate.a = MID(0, delegate.a, 255); + delegate.a = base::clamp(delegate.a, 0, 255); } else delegate.a = rgba_geta(color); @@ -235,14 +235,14 @@ void ConvolutionMatrixFilter::applyToGrayscale(FilterManager* filterMgr) if (target & TARGET_GRAY_CHANNEL) { delegate.v = delegate.v / delegate.div + m_matrix->getBias(); - delegate.v = MID(0, delegate.v, 255); + delegate.v = base::clamp(delegate.v, 0, 255); } else delegate.v = graya_getv(color); if (target & TARGET_ALPHA_CHANNEL) { delegate.a = delegate.a / m_matrix->getDiv() + m_matrix->getBias(); - delegate.a = MID(0, delegate.a, 255); + delegate.a = base::clamp(delegate.a, 0, 255); } else delegate.a = graya_geta(color); @@ -290,7 +290,7 @@ void ConvolutionMatrixFilter::applyToIndexed(FilterManager* filterMgr) if (target & TARGET_INDEX_CHANNEL) { delegate.index = delegate.index / m_matrix->getDiv() + m_matrix->getBias(); - delegate.index = MID(0, delegate.index, 255); + delegate.index = base::clamp(delegate.index, 0, 255); *(dst_address++) = delegate.index; } @@ -299,28 +299,28 @@ void ConvolutionMatrixFilter::applyToIndexed(FilterManager* filterMgr) if (target & TARGET_RED_CHANNEL) { delegate.r = delegate.r / delegate.div + m_matrix->getBias(); - delegate.r = MID(0, delegate.r, 255); + delegate.r = base::clamp(delegate.r, 0, 255); } else delegate.r = rgba_getr(color); if (target & TARGET_GREEN_CHANNEL) { delegate.g = delegate.g / delegate.div + m_matrix->getBias(); - delegate.g = MID(0, delegate.g, 255); + delegate.g = base::clamp(delegate.g, 0, 255); } else delegate.g = rgba_getg(color); if (target & TARGET_BLUE_CHANNEL) { delegate.b = delegate.b / delegate.div + m_matrix->getBias(); - delegate.b = MID(0, delegate.b, 255); + delegate.b = base::clamp(delegate.b, 0, 255); } else delegate.b = rgba_getb(color); if (target & TARGET_ALPHA_CHANNEL) { delegate.a = delegate.a / delegate.div + m_matrix->getBias(); - delegate.a = MID(0, delegate.a, 255); + delegate.a = base::clamp(delegate.a, 0, 255); } else delegate.a = rgba_geta(color); diff --git a/src/filters/hue_saturation_filter.cpp b/src/filters/hue_saturation_filter.cpp index 889251f84..1ede7b0cc 100644 --- a/src/filters/hue_saturation_filter.cpp +++ b/src/filters/hue_saturation_filter.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2017-2018 David Capello // // This program is distributed under the terms of @@ -11,6 +11,7 @@ #include "filters/hue_saturation_filter.h" +#include "base/clamp.h" #include "doc/image.h" #include "doc/palette.h" #include "doc/palette_picks.h" @@ -124,7 +125,7 @@ void HueSaturationFilter::applyToGrayscale(FilterManager* filterMgr) gfx::Hsl hsl(gfx::Rgb(k, k, k)); double l = hsl.lightness()*(1.0+m_l); - l = MID(0.0, l, 1.0); + l = base::clamp(l, 0.0, 1.0); hsl.lightness(l); gfx::Rgb rgb(hsl); @@ -133,7 +134,7 @@ void HueSaturationFilter::applyToGrayscale(FilterManager* filterMgr) if (a && (target & TARGET_ALPHA_CHANNEL)) { a = a*(1.0+m_a); - a = MID(0, a, 255); + a = base::clamp(a, 0, 255); } } @@ -213,10 +214,10 @@ void HueSaturationFilter::applyFilterToRgbT(const Target target, doc::color_t& c h = std::fmod(h, 360.0); double s = hsl.saturation()*(1.0+m_s); - s = MID(0.0, s, 1.0); + s = base::clamp(s, 0.0, 1.0); double l = (hsl.*get_lightness)()*(1.0+m_l); - l = MID(0.0, l, 1.0); + l = base::clamp(l, 0.0, 1.0); hsl.hue(h); hsl.saturation(s); @@ -228,7 +229,7 @@ void HueSaturationFilter::applyFilterToRgbT(const Target target, doc::color_t& c if (target & TARGET_BLUE_CHANNEL ) b = rgb.blue(); if (a && (target & TARGET_ALPHA_CHANNEL)) { a = a*(1.0+m_a); - a = MID(0, a, 255); + a = base::clamp(a, 0, 255); } c = rgba(r, g, b, a); diff --git a/src/filters/replace_color_filter.cpp b/src/filters/replace_color_filter.cpp index c4aa22510..fd0bd4458 100644 --- a/src/filters/replace_color_filter.cpp +++ b/src/filters/replace_color_filter.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2016 David Capello // // This program is distributed under the terms of @@ -11,7 +11,7 @@ #include "filters/replace_color_filter.h" -#include "base/base.h" +#include "base/clamp.h" #include "doc/image.h" #include "doc/palette.h" #include "doc/rgbmap.h" @@ -40,7 +40,7 @@ void ReplaceColorFilter::setTo(const color_t to) void ReplaceColorFilter::setTolerance(int tolerance) { - m_tolerance = MID(0, tolerance, 255); + m_tolerance = base::clamp(tolerance, 0, 255); } const char* ReplaceColorFilter::getName() diff --git a/src/render/LICENSE.txt b/src/render/LICENSE.txt index 8e8e061eb..28e9e02a2 100644 --- a/src/render/LICENSE.txt +++ b/src/render/LICENSE.txt @@ -1,5 +1,5 @@ -Copyright (c) 2019 Igara Studio S.A. -Copyright (c) 2001-2018 David Capello +Copyright (c) 2019-2020 Igara Studio S.A. +Copyright (c) 2001-2018 David Capello Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/src/render/error_diffusion.cpp b/src/render/error_diffusion.cpp index b066dbd97..feb86c3f3 100644 --- a/src/render/error_diffusion.cpp +++ b/src/render/error_diffusion.cpp @@ -1,5 +1,5 @@ // Aseprite Render Library -// Copyright (c) 2019 Igara Studio S.A +// Copyright (c) 2019-2020 Igara Studio S.A // Copyright (c) 2017 David Capello // // This file is released under the terms of the MIT license. @@ -68,7 +68,7 @@ doc::color_t ErrorDiffusionDither::ditherRgbToIndex2D( }; for (int i=0; imapColor(r2, g2, b2, a2): palette->findBestfit(r2, g2, b2, a2, m_transparentIndex)); @@ -201,7 +201,7 @@ doc::color_t OrderedDither2::ditherRgbPixelToIndex( if (mix) { if (div) mix /= div; - mix = MID(0, mix, maxMixValue); + mix = base::clamp(mix, 0, maxMixValue); } const int rM = r0 + (r1-r0) * mix / maxMixValue; diff --git a/src/render/render.cpp b/src/render/render.cpp index d8cbcada2..64a547583 100644 --- a/src/render/render.cpp +++ b/src/render/render.cpp @@ -1,5 +1,5 @@ // Aseprite Render Library -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2020 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This file is released under the terms of the MIT license. @@ -11,7 +11,7 @@ #include "render/render.h" -#include "base/base.h" +#include "base/clamp.h" #include "doc/blend_internals.h" #include "doc/blend_mode.h" #include "doc/doc.h" @@ -881,7 +881,7 @@ void Render::renderOnionskin( m_globalOpacity = m_onionskin.opacityBase() - m_onionskin.opacityStep() * ((frameOut - frame)-1); } - m_globalOpacity = MID(0, m_globalOpacity, 255); + m_globalOpacity = base::clamp(m_globalOpacity, 0, 255); if (m_globalOpacity > 0) { BlendMode blendMode = BlendMode::UNSPECIFIED; if (m_onionskin.type() == OnionskinType::MERGE) diff --git a/src/render/zoom.cpp b/src/render/zoom.cpp index a442d2e3a..47b7549c0 100644 --- a/src/render/zoom.cpp +++ b/src/render/zoom.cpp @@ -1,4 +1,5 @@ // Aseprite Render Library +// Copyright (c) 2020 Igara Studio S.A. // Copyright (c) 2001-2016 David Capello // // This file is released under the terms of the MIT license. @@ -8,7 +9,7 @@ #include "config.h" #endif -#include "base/base.h" +#include "base/clamp.h" #include "base/debug.h" #include "render/zoom.h" @@ -104,7 +105,7 @@ Zoom Zoom::fromScale(double scale) // static Zoom Zoom::fromLinearScale(int i) { - i = MID(0, i, scales_size-1); + i = base::clamp(i, 0, scales_size-1); return Zoom(scales[i][0], scales[i][1]); }