Skip to content

Commit

Permalink
Refactoring and minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Sep 10, 2023
1 parent 474f321 commit b71006c
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 179 deletions.
5 changes: 5 additions & 0 deletions far/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
--------------------------------------------------------------------------------
drkns 2023-09-10 16:55:08+01:00 - build 6188

1. Refactoring and minor fixes.

--------------------------------------------------------------------------------
drkns 2023-09-09 09:31:11+01:00 - build 6187

Expand Down
65 changes: 24 additions & 41 deletions far/color_picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "color_picker.hpp"

// Internal:
#include "color_picker_common.hpp"
#include "color_picker_256.hpp"
#include "color_picker_rgb.hpp"
#include "farcolor.hpp"
#include "vmenu.hpp"
#include "dialog.hpp"
#include "scrbuf.hpp"
#include "interf.hpp"
#include "config.hpp"
#include "colormix.hpp"
Expand All @@ -56,7 +55,6 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Common:
#include "common.hpp"
#include "common/2d/algorithm.hpp"
#include "common/2d/point.hpp"
#include "common/from_string.hpp"
#include "common/null_iterator.hpp"
#include "common/scope_exit.hpp"
Expand All @@ -66,17 +64,12 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

//----------------------------------------------------------------------------

static constexpr uint8_t distinct(unsigned char const Index)
{
return detail::bg(Index) | detail::fg(colors::index::nt_last - Index);
}

static constexpr auto IndexColors = []
{
std::array<uint8_t, colors::index::nt_size> Result;

for (uint8_t i = 0; i != Result.size(); ++i)
Result[i] = distinct(i);
Result[i] = detail::bg(i) | detail::fg(colors::index::nt_last - i);

return Result;
}();
Expand Down Expand Up @@ -502,22 +495,21 @@ bool GetColorDialog(FarColor& Color, bool const bCentered, const FarColor* const
{ DI_TEXT, {{Fg4X, Fg4Y}, {0, Fg4Y}}, DIF_NONE, msg(lng::MSetColorForeground), },
{ DI_CHECKBOX, {{Fg4X, Fg4Y}, {0, Fg4Y}}, DIF_NONE, msg(lng::MSetColorForeground), },

{ DI_RADIOBUTTON, {{Fg4X+3*0, Fg4Y+1}, {0, Fg4Y+1}}, DIF_MOVESELECT | DIF_GROUP,},
{ DI_RADIOBUTTON, {{Fg4X+3*0, Fg4Y+2}, {0, Fg4Y+2}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Fg4X+3*1, Fg4Y+1}, {0, Fg4Y+1}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Fg4X+3*1, Fg4Y+2}, {0, Fg4Y+2}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Fg4X+3*2, Fg4Y+1}, {0, Fg4Y+1}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Fg4X+3*2, Fg4Y+2}, {0, Fg4Y+2}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Fg4X+3*3, Fg4Y+1}, {0, Fg4Y+1}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Fg4X+3*3, Fg4Y+2}, {0, Fg4Y+2}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Fg4X+3*4, Fg4Y+1}, {0, Fg4Y+1}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Fg4X+3*4, Fg4Y+2}, {0, Fg4Y+2}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Fg4X+3*5, Fg4Y+1}, {0, Fg4Y+1}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Fg4X+3*5, Fg4Y+2}, {0, Fg4Y+2}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Fg4X+3*6, Fg4Y+1}, {0, Fg4Y+1}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Fg4X+3*6, Fg4Y+2}, {0, Fg4Y+2}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Fg4X+3*7, Fg4Y+1}, {0, Fg4Y+1}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Fg4X+3*7, Fg4Y+2}, {0, Fg4Y+2}}, DIF_MOVESELECT, },
#define COLOR_COLUMN(x, y, index) \
COLOR_CELL(x + 3 * index, y + 0), \
COLOR_CELL(x + 3 * index, y + 1)

#define COLOR_PLANE(column, x, y) \
column(x, y, 0), \
column(x, y, 1), \
column(x, y, 2), \
column(x, y, 3), \
column(x, y, 4), \
column(x, y, 5), \
column(x, y, 6), \
column(x, y, 7)

COLOR_PLANE(COLOR_COLUMN, Fg4X, Fg4Y + 1),

{ DI_TEXT, {{30, 2 }, {0, 2 }}, DIF_NONE, msg(Color.IsFgIndex()? lng::MSetColorForeIndex : lng::MSetColorForeAARRGGBB) },
{ DI_FIXEDIT, {{30, 3 }, {37, 3 }}, DIF_MASKEDIT, },
Expand All @@ -527,22 +519,10 @@ bool GetColorDialog(FarColor& Color, bool const bCentered, const FarColor* const
{ DI_TEXT, {{Bg4X, Bg4Y}, {0, Bg4Y}}, DIF_NONE, msg(lng::MSetColorBackground), },
{ DI_CHECKBOX, {{Bg4X, Bg4Y}, {0, Bg4Y}}, DIF_NONE, msg(lng::MSetColorBackground), },

{ DI_RADIOBUTTON, {{Bg4X+3*0, Bg4Y+1}, {0, Bg4Y+1}}, DIF_MOVESELECT | DIF_GROUP, },
{ DI_RADIOBUTTON, {{Bg4X+3*0, Bg4Y+2}, {0, Bg4Y+2}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Bg4X+3*1, Bg4Y+1}, {0, Bg4Y+1}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Bg4X+3*1, Bg4Y+2}, {0, Bg4Y+2}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Bg4X+3*2, Bg4Y+1}, {0, Bg4Y+1}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Bg4X+3*2, Bg4Y+2}, {0, Bg4Y+2}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Bg4X+3*3, Bg4Y+1}, {0, Bg4Y+1}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Bg4X+3*3, Bg4Y+2}, {0, Bg4Y+2}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Bg4X+3*4, Bg4Y+1}, {0, Bg4Y+1}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Bg4X+3*4, Bg4Y+2}, {0, Bg4Y+2}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Bg4X+3*5, Bg4Y+1}, {0, Bg4Y+1}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Bg4X+3*5, Bg4Y+2}, {0, Bg4Y+2}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Bg4X+3*6, Bg4Y+1}, {0, Bg4Y+1}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Bg4X+3*6, Bg4Y+2}, {0, Bg4Y+2}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Bg4X+3*7, Bg4Y+1}, {0, Bg4Y+1}}, DIF_MOVESELECT, },
{ DI_RADIOBUTTON, {{Bg4X+3*7, Bg4Y+2}, {0, Bg4Y+2}}, DIF_MOVESELECT, },
COLOR_PLANE(COLOR_COLUMN, Bg4X, Bg4Y + 1),

#undef COLOR_PLANE
#undef COLOR_COLUMN

{ DI_TEXT, {{30, 7 }, {0, 7 }}, DIF_NONE, msg(Color.IsBgIndex()? lng::MSetColorBackIndex : lng::MSetColorBackAARRGGBB) },
{ DI_FIXEDIT, {{30, 8 }, {37, 8 }}, DIF_MASKEDIT, },
Expand Down Expand Up @@ -576,6 +556,9 @@ bool GetColorDialog(FarColor& Color, bool const bCentered, const FarColor* const
{ DI_BUTTON, {{0, ButtonY}, {0, ButtonY}}, DIF_CENTERGROUP, msg(lng::MSetColorCancel), },
});

ColorDlg[cd_fg_color_first].Flags |= DIF_GROUP;
ColorDlg[cd_bg_color_first].Flags |= DIF_GROUP;

ColorDlg[cd_separator_vertical_before_style].strMask = { BoxSymbols[BS_T_H2V1], BoxSymbols[BS_V1], BoxSymbols[BS_B_H1V1] };
ColorDlg[cd_separator_after_foreground].strMask = ColorDlg[cd_separator_after_background].strMask = { BoxSymbols[BS_L_H1V2], BoxSymbols[BS_H1], BoxSymbols[BS_R_H1V1] };
ColorDlg[cd_separator_style].strMask = { BoxSymbols[BS_H1], BoxSymbols[BS_H1], BoxSymbols[BS_H1] };
Expand Down
124 changes: 83 additions & 41 deletions far/color_picker_256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "lang.hpp"

// Platform:
#include "platform.hpp"

// Common:
#include "common/2d/algorithm.hpp"
Expand Down Expand Up @@ -77,46 +76,82 @@ static FarColor Console256ColorToFarColor(index_color_256 const Color)
};
}

static constexpr index_color_256 distinct_cube_index(uint8_t const Index)
static constexpr uint8_t cube_color(uint8_t const Index)
{
rgb RGB = Index;
// Naive inversion doesn't work nicely in the middle of the spectre.
// This way the distance between foreground and background colors is constant and should always produce readable results.
constexpr uint8_t Mapping[]{ 3, 4, 5, 0, 1, 2 };
RGB.r = Mapping[RGB.r];
RGB.g = Mapping[RGB.g];
RGB.b = Mapping[RGB.b];

return { RGB, Index };
assert(Index < colors::index::cube_count);
return colors::index::cube_first + Index;
}

static constexpr index_color_256 distinct_grey(uint8_t const Value)
static constexpr uint8_t cube_index(uint8_t const Color)
{
// Naive inversion doesn't work nicely in the middle of the spectre.
// This way the distance between foreground and background colors is constant and should always produce readable results.
return
{
static_cast<uint8_t>(colors::index::grey_first + Value + (Value < 12? +12 : -12)),
static_cast<uint8_t>(colors::index::grey_first + Value)
};
assert(colors::index::cube_first <= Color && Color <= colors::index::cube_last);
return Color - colors::index::cube_first;
}

static constexpr uint8_t grey_color(uint8_t const Index)
{
assert(Index < colors::index::grey_count);
return colors::index::grey_first + Index;
}

static constexpr auto distinct_grey_index = []
static constexpr uint8_t grey_index(uint8_t const Color)
{
assert(colors::index::grey_first <= Color && Color <= colors::index::grey_last);
return Color - colors::index::grey_first;
}

// Naive inversion doesn't work nicely in the middle of the spectre.
// This way the distance between foreground and background colors is constant and should always produce readable results.
template<uint8_t count>
static consteval auto foreground_inverse_mapping()
{
std::array<uint8_t, count> Result;
constexpr auto Pivot = count / 2;
std::iota(Result.begin(), Result.begin() + Pivot, Pivot);
std::iota(Result.begin() + Pivot, Result.end(), 0);
return Result;
}

static constexpr auto distinct_cube_map = []
{
std::array<uint8_t, colors::index::cube_count> Result;

constexpr auto CubeInverseMapping = foreground_inverse_mapping<cube_size>();

for (uint8_t i = 0; i != Result.size(); ++i)
{
rgb RGB = cube_color(i);
RGB.r = CubeInverseMapping[RGB.r];
RGB.g = CubeInverseMapping[RGB.g];
RGB.b = CubeInverseMapping[RGB.b];

Result[i] = RGB;
}

return Result;
}();
static_assert(std::size(distinct_cube_map) == colors::index::cube_count);

static constexpr auto distinct_grey_map = []
{
std::array<index_color_256, colors::index::grey_last - colors::index::grey_first + 1> Result;
std::array<uint8_t, colors::index::grey_count> Result;

constexpr auto GreyInverseMapping = foreground_inverse_mapping<colors::index::grey_count>();

for (uint8_t i = 0; i != Result.size(); ++i)
Result[i] = distinct_grey(i);
{
Result[i] = grey_color(GreyInverseMapping[i]);
}

return Result;
}();
static_assert(std::size(distinct_grey_index) == colors::index::grey_last - colors::index::grey_first + 1);
static_assert(std::size(distinct_grey_map) == colors::index::grey_count);

static constexpr auto grey_control_by_color = column_major_iota<uint8_t, 6, 4>();
static_assert(std::size(grey_control_by_color) == std::size(distinct_grey_index));
static constexpr auto grey_control_by_index = column_major_iota<uint8_t, 6, 4>();
static_assert(std::size(grey_control_by_index) == colors::index::grey_count);

static constexpr auto grey_color_by_control = column_major_iota<uint8_t, 4, 6>();
static_assert(std::size(grey_color_by_control) == std::size(distinct_grey_index));
static constexpr auto grey_index_by_control = column_major_iota<uint8_t, 4, 6>();
static_assert(std::size(grey_index_by_control) == colors::index::grey_count);

static constexpr uint8_t grey_stripe_mapping[]
{
Expand All @@ -126,11 +161,16 @@ static constexpr uint8_t grey_stripe_mapping[]
23, 22, 21, 20, 19, 18
};

static_assert(std::size(grey_stripe_mapping) == std::size(distinct_grey_index));
static_assert(std::size(grey_stripe_mapping) == colors::index::grey_count);

static bool is_rgb(uint8_t const Color)
{
return in_closed_range(colors::index::cube_first, static_cast<int>(Color), colors::index::cube_last);
return in_closed_range(colors::index::cube_first, Color, colors::index::cube_last);
}

static bool is_grey(uint8_t const Color)
{
return in_closed_range(colors::index::grey_first, Color, colors::index::grey_last);
}

enum color_256_dialog_items
Expand All @@ -141,7 +181,7 @@ enum color_256_dialog_items
cd_cube_last = cd_cube_first + (cube_size * cube_size - 1),

cd_grey_first,
cd_grey_last = cd_grey_first + (colors::index::grey_last - colors::index::grey_first),
cd_grey_last = cd_grey_first + colors::index::grey_count - 1,

cd_button_up,
cd_button_left,
Expand Down Expand Up @@ -212,7 +252,7 @@ static auto cube_index(color_256_state const& ColorState, color_256_dialog_items

static void init_cube(color_256_state& ColorState)
{
uint8_t Index = colors::index::cube_first;
uint8_t Index{};

for (auto& Plane: ColorState.Cube.Cube)
{
Expand All @@ -222,7 +262,7 @@ static void init_cube(color_256_state& ColorState)
{
Point = Index++;

if (Point == ColorState.CurColor)
if (cube_color(Point) == ColorState.CurColor)
{
ColorState.Cube.Slice = &Plane - &ColorState.Cube.Cube[0];
}
Expand All @@ -243,13 +283,15 @@ static intptr_t GetColorDlgProc(Dialog* Dlg, intptr_t Msg, intptr_t Param1, void

if (in_closed_range(cd_cube_first, Param1, cd_cube_last))
{
Colors.Colors[0] = Console256ColorToFarColor(distinct_cube_index(cube_index(ColorState, static_cast<color_256_dialog_items>(Param1))));
const auto ColorIndex = cube_index(ColorState, static_cast<color_256_dialog_items>(Param1));
Colors.Colors[0] = Console256ColorToFarColor({ distinct_cube_map[ColorIndex], cube_color(ColorIndex) });
return true;
}

if (in_closed_range(cd_grey_first, Param1, cd_grey_last))
{
Colors.Colors[0] = Console256ColorToFarColor(distinct_grey_index[grey_stripe_mapping[grey_color_by_control[Param1 - cd_grey_first]]]);
const auto ColorIndex = grey_stripe_mapping[grey_index_by_control[Param1 - cd_grey_first]];
Colors.Colors[0] = Console256ColorToFarColor({ distinct_grey_map[ColorIndex], grey_color(ColorIndex) });
return true;
}

Expand Down Expand Up @@ -291,14 +333,14 @@ static intptr_t GetColorDlgProc(Dialog* Dlg, intptr_t Msg, intptr_t Param1, void

if (Param2 && in_closed_range(cd_cube_first, Button, cd_cube_last))
{
ColorState.CurColor = cube_index(ColorState, Button);
ColorState.CurColor = cube_color(cube_index(ColorState, Button));
update_rgb_control<color_256_state>(Dlg, ColorState.as_rgb());
return true;
}

if (Param2 && in_closed_range(cd_grey_first, Button, cd_grey_last))
{
ColorState.CurColor = colors::index::grey_first + grey_stripe_mapping[grey_color_by_control[Button - cd_grey_first]];
ColorState.CurColor = grey_color(grey_stripe_mapping[grey_index_by_control[Button - cd_grey_first]]);
update_rgb_control<color_256_state>(Dlg, rgb{});
return true;
}
Expand Down Expand Up @@ -327,7 +369,7 @@ static intptr_t GetColorDlgProc(Dialog* Dlg, intptr_t Msg, intptr_t Param1, void
{
for (const auto& Point: Line)
{
if (Point == ColorState.CurColor)
if (cube_color(Point) == ColorState.CurColor)
{
const auto ControlId = cd_cube_first + cube_rc_mapping[&Point - &Plane[0][0]];
Dlg->SendMessage(DM_SETCHECK, ControlId, ToPtr(BSTATE_CHECKED));
Expand Down Expand Up @@ -422,15 +464,15 @@ bool pick_color_256(uint8_t& Color)
ColorDlg[cd_text_b].strData = ColorState.channel_value(RGB.b);
}

if (in_closed_range(colors::index::cube_first, static_cast<int>(Color), colors::index::cube_last))
if (is_rgb(Color))
{
const auto ControlId = cd_cube_first + cube_rc_mapping[(Color - colors::index::cube_first) % (cube_size * cube_size)];
const auto ControlId = cd_cube_first + cube_rc_mapping[cube_index(Color) % (cube_size * cube_size)];
ColorDlg[ControlId].Selected = BSTATE_CHECKED;
ColorDlg[ControlId].Flags |= DIF_FOCUS;
}
else if (in_closed_range(colors::index::grey_first, static_cast<int>(Color), colors::index::grey_last))
else if (is_grey(Color))
{
const auto ControlId = cd_grey_first + grey_control_by_color[grey_stripe_mapping[Color - colors::index::grey_first]];
const auto ControlId = cd_grey_first + grey_control_by_index[grey_stripe_mapping[grey_index(Color)]];
ColorDlg[ControlId].Selected = BSTATE_CHECKED;
ColorDlg[ControlId].Flags |= DIF_FOCUS;
}
Expand Down
Loading

0 comments on commit b71006c

Please sign in to comment.