Repository: PixiEditor/PixiEditor
File: src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/Effects/PosterizationNode.cs (C#)

Code before:
﻿using Drawie.Backend.Core;
using Drawie.Backend.Core.Numerics;
using Drawie.Backend.Core.Shaders;
using Drawie.Backend.Core.Surfaces;
using Drawie.Backend.Core.Surfaces.ImageData;
using Drawie.Backend.Core.Surfaces.PaintImpl;
using Drawie.Numerics;
using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
using PixiEditor.ChangeableDocument.Rendering;

namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Effects;

[NodeInfo("Posterization")]
public class PosterizationNode : RenderNode, IRenderInput
{
    public RenderInputProperty Background { get; }
    public InputProperty<PosterizationMode> Mode { get; }
    public InputProperty<int> Levels { get; }
    public InputProperty<bool> UseDocumentColorSpace { get; }
    
    private Paint paint;
    private Shader shader;
    
    private Shader? lastImageShader;
    private VecI lastDocumentSize;
    
    private string shaderCode = """
                                 uniform shader iImage;
                                 uniform int iMode;
                                 uniform float iLevels;
                                 
                                 half posterize(half value, float levels) {
                                     return clamp(floor(value * (levels - 1) + 0.5) / (levels - 1), 0.0, 1.0);
                                 }
                                 
                                 half4 posterizeRgb(half4 color, float levels) {
                                     return half4(
                                         posterize(color.r, levels),
                                         posterize(color.g, levels),
                                         posterize(color.b, levels),
                                         color.a
                                     );
                                 }
                                 

Review comment (suggestion):
After some consideration, I came into conclusion it would be better to make this input
` public InputProperty<ColorSpaceType> ColorSpace { get; }`

If we add more color spaces in the future, it will be trivial to add them to this node. This enum has `Inherit` mode that does exactly what you have in this bool

Code after:
﻿using Drawie.Backend.Core;
using Drawie.Backend.Core.Numerics;
using Drawie.Backend.Core.Shaders;
using Drawie.Backend.Core.Surfaces;
using Drawie.Backend.Core.Surfaces.ImageData;
using Drawie.Backend.Core.Surfaces.PaintImpl;
using Drawie.Numerics;
using PixiEditor.ChangeableDocument.Changeables.Graph.ColorSpaces;
using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
using PixiEditor.ChangeableDocument.Rendering;

namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.Effects;

[NodeInfo("Posterization")]
public class PosterizationNode : RenderNode, IRenderInput
{
    public RenderInputProperty Background { get; }
    public InputProperty<PosterizationMode> Mode { get; }
    public InputProperty<int> Levels { get; }
    public InputProperty<ColorSpaceType> PosterizationColorSpace { get; }
    
    private Paint paint;
    private Shader shader;
    
    private Shader? lastImageShader;
    private VecI lastDocumentSize;
    
    private string shaderCode = """
                                 uniform shader iImage;
                                 uniform int iMode;
                                 uniform float iLevels;
                                 
                                 half posterize(half value, float levels) {
                                     return clamp(floor(value * (levels - 1) + 0.5) / (levels - 1), 0.0, 1.0);
                                 }
                                 
                                 half4 posterizeRgb(half4 color, float levels) {
                                     return half4(
                                         posterize(color.r, levels),
                                         posterize(color.g, levels),
                                         posterize(color.b, levels),
                                         color.a
                                     );
                                 }