{"record":{"id":"6b28f86883ccc350","repo":"stride3d/stride","slug":"there-must-be-four-and-only-four-input-values-for-color","errorCode":null,"errorMessage":"There must be four and only four input values for Color.","messagePattern":"There must be four and only four input values for Color\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Mathematics/Color.cs","lineNumber":189,"sourceCode":"    public Color(int rgba)\n    {\n        A = (byte)((rgba >> 24) & 255);\n        B = (byte)((rgba >> 16) & 255);\n        G = (byte)((rgba >> 8) & 255);\n        R = (byte)(rgba & 255);\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"Color\"/> struct.\n    /// </summary>\n    /// <param name=\"values\">The values to assign to the red, green, and blue, alpha components of the color. This must be an array with four elements.</param>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"values\"/> is <c>null</c>.</exception>\n    /// <exception cref=\"ArgumentOutOfRangeException\">Thrown when <paramref name=\"values\"/> contains more or less than four elements.</exception>\n    public Color(float[] values)\n    {\n        ArgumentNullException.ThrowIfNull(values);\n        if (values.Length != 4)\n            throw new ArgumentOutOfRangeException(nameof(values), \"There must be four and only four input values for Color.\");\n\n        R = ToByte(values[0]);\n        G = ToByte(values[1]);\n        B = ToByte(values[2]);\n        A = ToByte(values[3]);\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"Color\"/> struct.\n    /// </summary>\n    /// <param name=\"values\">The values to assign to the red, green, blue, or alpha components of the color. This must be an array with four elements.</param>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"values\"/> is <c>null</c>.</exception>\n    /// <exception cref=\"ArgumentOutOfRangeException\">Thrown when <paramref name=\"values\"/> contains more or less than four elements.</exception>\n    public Color(byte[] values)\n    {\n        ArgumentNullException.ThrowIfNull(values);\n        if (values.Length != 4)\n            throw new ArgumentOutOfRangeException(nameof(values), \"There must be four and only four input values for Color.\");","sourceCodeStart":171,"sourceCodeEnd":207,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Mathematics/Color.cs#L171-L207","documentation":"The Color(float[]) constructor converts an RGBA float array into byte color channels and requires exactly four elements. A null array yields ArgumentNullException; any other length yields this ArgumentOutOfRangeException.","triggerScenarios":"Calling new Color(floatArray) where floatArray.Length is 3 (RGB-only), 0, or more than 4 — e.g. passing parsed color data without an alpha channel.","commonSituations":"Importing colors from formats that omit alpha (hex #RRGGBB expanded to 3 floats); JSON/CSV parsed arrays with variable length; hand-written arrays missing the alpha component.","solutions":["Ensure the array has exactly 4 elements (R, G, B, A) before constructing.","For RGB-only data, append an alpha of 1f: new[]{ r, g, b, 1f }.","Use the Color(byte r, byte g, byte b, byte a) or Color(float r, float g, float b, float a) constructor instead of an array.","Validate array length and throw a clearer domain error at the parsing boundary."],"exampleFix":"// before\nvar color = new Color(rgbFloats); // rgbFloats.Length == 3 -> throws\n// after\nvar rgba = rgbFloats.Length == 3 ? new[] { rgbFloats[0], rgbFloats[1], rgbFloats[2], 1f } : rgbFloats;\nvar color = new Color(rgba);","handlingStrategy":"validation","validationCode":"if (values is not { Length: 4 })\n    throw new ArgumentException(\"Color requires exactly 4 RGBA float components.\", nameof(values));","typeGuard":"bool IsRgba(float[]? v) => v is { Length: 4 };","tryCatchPattern":"try { var c = new Color(values); }\ncatch (ArgumentOutOfRangeException) { var c = Color.White; /* or log & rethrow with context */ }","preventionTips":["Always store colors as RGBA internally","Normalize RGB-only inputs at parse time by appending alpha","Use tuple/constructor overloads instead of arrays where possible"],"tags":["math","color","argument-out-of-range"],"backgroundTag":"argument-out-of-range","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}