Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsModel/ColorScheme.cpp (C++)

Code before:
    JsonUtils::SetValueForKey(json, ForegroundKey, _Foreground);
    JsonUtils::SetValueForKey(json, BackgroundKey, _Background);
    JsonUtils::SetValueForKey(json, SelectionBackgroundKey, _SelectionBackground);
    JsonUtils::SetValueForKey(json, CursorColorKey, _CursorColor);

    for (unsigned int i = 0; i < TableColors.size(); ++i)
    {
        JsonUtils::SetValueForKey(json, til::at(TableColors, i), _table.at(i));
    }

    return json;
}

winrt::com_array<Color> ColorScheme::Table() const noexcept
{
    winrt::com_array<Color> result{ base::checked_cast<uint32_t>(_table.size()) };
    std::transform(_table.begin(), _table.end(), result.begin(), [](til::color c) -> Color { return c; });
    return result;
}

// Method Description:
// - Set a color in the color table
// Arguments:
// - index: the index of the desired color within the table
// - value: the color value we are setting the color table color to
// Return Value:
// - none
void ColorScheme::SetColorTableEntry(uint8_t index, const winrt::Windows::UI::Color& value) noexcept
{
    THROW_HR_IF(E_INVALIDARG, index > _table.size() - 1);
    _table[index] = value;
}

// Method Description:
// - Validates a given color scheme
// - A color scheme is valid if it contains values for _all_ the fields
// Arguments:
// - The color scheme to validate
// Return Value:
// - true if the scheme is valid, false otherwise
bool ColorScheme::ValidateColorScheme(const Json::Value scheme)
{
    for (const auto& key : ColorSchemeKeys)
    {
        if (!scheme.isMember(JsonKey(key)))
        {
            return false;
        }
    }
    return true;
}

Review comment (suggestion):
I do not necessarily agree with this -- must a color scheme set a selection color? Our default is likely better, and our default can _change_ (to allow for the default to be inverse, or something else)

Code after:

// Method Description:
// - Set a color in the color table
// Arguments:
// - index: the index of the desired color within the table
// - value: the color value we are setting the color table color to
// Return Value:
// - none
void ColorScheme::SetColorTableEntry(uint8_t index, const winrt::Windows::UI::Color& value) noexcept
{
    THROW_HR_IF(E_INVALIDARG, index > _table.size() - 1);
    _table[index] = value;
}

// Method Description:
// - Validates a given color scheme
// - A color scheme is valid if it has a name and defines all the colors
// Arguments:
// - The color scheme to validate
// Return Value:
// - true if the scheme is valid, false otherwise
bool ColorScheme::ValidateColorScheme(const Json::Value& scheme)
{
    for (const auto& key : TableColors)
    {
        if (!scheme.isMember(JsonKey(key)))
        {
            return false;
        }
    }
    if (!scheme.isMember(JsonKey(NameKey)))
    {
        return false;
    }
    return true;
}

// Method Description:
// - Parse the name from the JSON representation of a ColorScheme.
// Arguments:
// - json: an object which should be a serialization of a ColorScheme object.
// Return Value:
// - the name of the color scheme represented by `json` as a std::wstring optional
//   i.e. the value of the `name` property.
// - returns std::nullopt if `json` doesn't have the `name` property
std::optional<std::wstring> ColorScheme::GetNameFromJson(const Json::Value& json)
{
    return JsonUtils::GetValueForKey<std::optional<std::wstring>>(json, NameKey);
}