d2phap/ImageGlass Β· error Β· ArgumentException

'{channelsStr}' is not a valid color channel. ---------- πŸ‘‰

Error message

'{channelsStr}' is not a valid color channel.

----------
πŸ‘‰πŸΌ Method: IG_SetColorChannels

What it means

Thrown by the string overload of IG_SetColorChannels when Enum.TryParse<ColorChannels>(channelsStr) fails. ColorChannels is a [Flags] enum; valid names are R, G, B, A, and the combined aliases RGB, RGBA, RA, GA, BA. Enum.TryParse accepts comma-separated flag combinations (e.g., 'R,G') case-insensitively. Any token not in the set fails the whole parse.

Source

Thrown at source/ImageGlass.Lib/Common/ServiceProviders/AppAPIs/AppAPIProvider.cs:1595

    /// Sets the image loading order value.
    /// </summary>
    public static void IG_SetLoadingOrderType(ImageOrderType orderType)
    {
        if (orderType == Core.Config.ImageLoadingOrderType) return;

        Core.Config.ImageLoadingOrderType = orderType;
        IG_ReloadList();
    }


    /// <summary>
    /// Sets the image color channels.
    /// </summary>
    public void IG_SetColorChannels(string? channelsStr)
    {
        if (!Enum.TryParse<ColorChannels>(channelsStr, out var channels))
        {
            throw new ArgumentException($"""
                '{channelsStr}' is not a valid color channel.

                ----------
                πŸ‘‰πŸΌ Method: {nameof(IG_SetColorChannels)}
                """,
                nameof(channelsStr));
        }

        IG_SetColorChannels(channels);
    }


    /// <summary>
    /// Sets the image color channels.
    /// </summary>
    public static void IG_SetColorChannels(ColorChannels channels)
    {
        if (Viewer.SourceKind == PhotoSource.None || Core.IsBusy) return;

View on GitHub (pinned to 4a3c4fecef)

Solutions

  1. Use single-letter flags (R, G, B, A), the defined combined names (RGB, RGBA, RA, GA, BA), or a comma-separated combination of single letters like 'R,G'.
  2. Validate with Enum.TryParse<ColorChannels> upstream and call the typed overload.
  3. Trim each token in a comma-separated list before parsing.

Example fix

// before
api.IG_SetColorChannels(userInput);

// after
if (Enum.TryParse<ColorChannels>(userInput?.Trim(), true, out var channels))
{
    api.IG_SetColorChannels(channels);
}
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.TryParse<ColorChannels>(channelsStr?.Trim(), true, out var channels))
    throw new ArgumentException($"Unknown color channel '{channelsStr}'.");
api.IG_SetColorChannels(channels);

Type guard

static bool IsValidColorChannels(string? s) => Enum.TryParse<ColorChannels>(s?.Trim(), true, out _);

Try / catch

try { api.IG_SetColorChannels(channelsStr); }
catch (ArgumentException ex) when (ex.ParamName == nameof(channelsStr))
{ /* valid: R, G, B, A, RGB, RGBA, RA, GA, BA, or comma combos like 'R,G' */ }

Prevention

When it happens

Trigger: Calling IG_SetColorChannels(string) with 'red'/'green'/'blue'/'alpha', 'rgb ' (trailing space inside a combined token), 'RG' (not a defined name; use 'R,G' or 'RGB'), or 'All'.

Common situations: User types color names instead of single-letter flags; config with a non-defined combination like 'RB'; trailing whitespace inside a comma list.

Related errors


AI-assisted analysis of d2phap/ImageGlass@4a3c4fecef (2026-08-13). Data as JSON: /api/errors/1c30641a2d1c7648. Report an issue: GitHub.