Color3 to Buffer

Im trying to write to a buffer containing a color3 for the new WritePixelsBuffer but I cant figure out how to turn the color into the number

1 Like

You can encode the Color3 into a u32 using the encoding ABGR, use this function to convert the Color3 to said u32:

local function encodeColor3(c: Color3): number
  -- conver the color to 0-255
  local r = math.floor(c.R * 255)
  local g = math.floor(c.G * 255)
  local b = math.floor(c.B * 255)

  -- encode the color values in ABGR
  return bit32.lshift(b, 16) + bit32.lshift(g, 8) + r
end
1 Like

If the answer above isn’t what is needed, I believe this may help.

-- This example is for the entire canvas, but you change it to the size you need
-- Just make sure its X*Y*4 for the 4 values provided for each pixel
local pixelsBuffer = buffer.create(editableImage.Size.X*editableImage.Size.Y*4)

for i = 1, editableImage.Size.X * editableImage.Size.Y do
	local pixelIndex = (i - 1) * 4
	buffer.writeu8(pixelsBuffer, pixelIndex, R)
	buffer.writeu8(pixelsBuffer, pixelIndex + 1, G)
	buffer.writeu8(pixelsBuffer, pixelIndex + 2, B)
    buffer.writeu8(pixelsBuffer, pixelIndex + 3, A)
end

-- Once again, change starting position and size accordingly
-- This starts in the top left and spans the entire editableImage
editableImage:WritePixelsBuffer(Vector2.zero, editableImage.Size, pixelsBuffer)

The RGBA values are any integer value between [0-255] with 255 being fully opaque and 0 being transparent for Alpha.

1 Like

Ive already managed to apply the color.
Though, this is the method I used so thank you for the reassurance

1 Like