{"record":{"id":"42c5e33752f37141","repo":"stride3d/stride","slug":"there-must-be-3-or-4-float-values-for-color4","errorCode":null,"errorMessage":"There must be 3 or 4 float[] values for Color4.","messagePattern":"There must be 3 or 4 float\\[\\] values for Color4\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Mathematics/Color4.cs","lineNumber":169,"sourceCode":"    public Color4(int rgba)\n    {\n        A = ((rgba >> 24) & 255) / 255.0f;\n        B = ((rgba >> 16) & 255) / 255.0f;\n        G = ((rgba >> 8) & 255) / 255.0f;\n        R = (rgba & 255) / 255.0f;\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"Color4\"/> struct.\n    /// </summary>\n    /// <param name=\"values\">The values to assign to the red, green, blue, and 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 Color4(float[] values)\n    {\n        ArgumentNullException.ThrowIfNull(values);\n        if (values.Length is not 3 and not 4)\n            throw new ArgumentOutOfRangeException(nameof(values), \"There must be 3 or 4 float[] values for Color4.\");\n\n        R = values[0];\n        G = values[1];\n        B = values[2];\n        A = values.Length >= 4 ? values[3] : 1f;\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"Color4\"/> struct.\n    /// </summary>\n    /// <param name=\"color\"><see cref=\"Color3\"/> used to initialize the color.</param>\n    public Color4(Color3 color)\n    {\n        R = color.R;\n        G = color.G;\n        B = color.B;\n        A = 1.0f;\n    }","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Mathematics/Color4.cs#L151-L187","documentation":"The Color4(float[]) constructor accepts arrays of exactly 3 or 4 floats: RGB (alpha defaults to 1f) or RGBA. Any other length throws ArgumentOutOfRangeException. The guard keeps ambiguous or truncated input from silently producing a wrong color.","triggerScenarios":"new Color4(floatArray) where floatArray.Length is 0, 1, 2, 5 or more; passing an empty array or a padded buffer.","commonSituations":"Passing a whole vertex buffer slice with stride padding; parsing hex/config values into wrong-sized arrays; reusing buffers sized for Vector2/Vector3 code paths (2 elements).","solutions":["Ensure the array has exactly 3 or 4 elements before constructing.","If the source array is larger, slice the first 3-4 elements: new Color4(values[..4]).","If it is smaller, build via the scalar constructor new Color4(r, g, b) or new Color4(r, g, b, a)."],"exampleFix":"// before\nvar color = new Color4(shortArray); // shortArray.Length == 2\n// after\nvar color = new Color4(shortArray[0], shortArray[1], 0f);","handlingStrategy":"validation","validationCode":"if (values is { Length: 3 } or { Length: 4 })\n    var c = new Color4(values);\nelse\n    throw new ArgumentException(\"Color4 requires 3 or 4 floats\", nameof(values));","typeGuard":"bool IsRgbOrRgbaArray(float[] v) => v is { Length: 3 } or { Length: 4 };","tryCatchPattern":"try { var c = new Color4(values); }\ncatch (ArgumentOutOfRangeException) { /* construct from scalars or pad array */ }","preventionTips":["Slice larger buffers to the first 3-4 elements before constructing.","Prefer scalar constructors new Color4(r, g, b[, a]) when element counts are uncertain.","Validate buffer sizes where data enters the app (file/network/config)."],"tags":["argument-out-of-range","constructor","csharp"],"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"}