babalae/better-genshin-impact · error · InvalidOperationException
{fieldName} 必须是 1 到 4 个数字
Error message
{fieldName} 必须是 1 到 4 个数字 What it means
BuildScalar constructs an OpenCvSharp Scalar for the lowerColor/upperColor color-range filters and supports only 1 to 4 channel values. If the JSON array is empty or has more than four entries, InvalidOperationException is thrown because a Scalar cannot represent that many channels.
Source
Thrown at BetterGenshinImpact/Core/Recognition/RecognitionObjectJsonLoader.cs:490
private static Rect BuildRect(object? x, object? y, object? width, object? height)
{
return new Rect(
(int)Math.Round(ToDouble(x)),
(int)Math.Round(ToDouble(y)),
(int)Math.Round(ToDouble(width)),
(int)Math.Round(ToDouble(height)));
}
private static Scalar BuildScalar(IReadOnlyList<double> values, string fieldName)
{
return values.Count switch
{
1 => new Scalar(values[0]),
2 => new Scalar(values[0], values[1]),
3 => new Scalar(values[0], values[1], values[2]),
4 => new Scalar(values[0], values[1], values[2], values[3]),
_ => throw new InvalidOperationException($"{fieldName} 必须是 1 到 4 个数字"),
};
}
private static TEnum ParseEnumExact<TEnum>(string? value, string fieldName) where TEnum : struct, Enum
{
if (string.IsNullOrWhiteSpace(value))
{
throw new InvalidOperationException($"{fieldName} 不能为空");
}
if (Enum.TryParse<TEnum>(value, false, out var parsed))
{
return parsed;
}
throw new InvalidOperationException($"{fieldName} 的值 {value} 不是有效的 {typeof(TEnum).Name}");
}
View on GitHub (pinned to a7cb36712d)
Solutions
- Provide exactly 1 to 4 numeric values for lowerColor/upperColor.
- Match the element count to the color space (e.g. 3 for BGR).
- Validate the array length against the Scalar channel limit before runtime.
Example fix
// before // "lowerColor": [100, 100, 100, 100, 100] -> throws // after // "lowerColor": [100, 100, 100]
Defensive patterns
Strategy: validation
Validate before calling
static void AssertScalarShape(IReadOnlyList<double> values, string fieldName)
{
if (values.Count is < 1 or > 4)
throw new InvalidOperationException($"{fieldName} must have 1-4 values, got {values.Count}.");
}
AssertScalarShape(config.LowerColor, nameof(config.LowerColor));
AssertScalarShape(config.UpperColor, nameof(config.UpperColor)); Type guard
static bool IsValidScalar(IReadOnlyList<double> values) => values.Count is >= 1 and <= 4;
Try / catch
try { return RecognitionObjectJsonLoader.Load(config, objectName, context); }
catch (InvalidOperationException ex) when (ex.Message.Contains("必须是 1 到 4 个数字"))
{ Logger.LogError(ex, "Color scalar has invalid channel count."); throw; } Prevention
- Match color array length to the colorspace (1 grayscale, 3 BGR).
- Validate lowerColor/upperColor lengths in authoring tooling.
- Avoid adding an alpha channel to BGR ranges.
When it happens
Trigger: A RecognitionObject has "lowerColor": [] or "lowerColor": [0,0,0,0,0] (five values) in Recognition.json.
Common situations: Misunderstood channel count for the colorspace; accidentally added an alpha channel; pasted a 5+ element array from another tool.
Related errors
- 未找到名称为 {objectName} 的 RecognitionObject 配置
- 对象 {objectName} 缺少 template 配置
- 表达式 {expression} 未返回 Rect
- 变量 {name} 存在循环引用
- {fieldName} 不能为空
AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13).
Data as JSON: /api/errors/90502bea80f1fecb.
Report an issue: GitHub.