From 4d04102adf0da8fe7c9448d51b750788fa65e02a Mon Sep 17 00:00:00 2001 From: Kristoffer Date: Fri, 2 Aug 2024 13:25:59 +0200 Subject: [PATCH] Optimise creation of a SimpleColor from a UInt32 Instead of creating a temporary array that we reinterpret into another temporary array, we can just pick out the bytes we want from within the UInt32. --- src/faces.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/faces.jl b/src/faces.jl index cc6e64e..a3fdf71 100644 --- a/src/faces.jl +++ b/src/faces.jl @@ -25,7 +25,11 @@ struct SimpleColor end SimpleColor(r::Integer, g::Integer, b::Integer) = SimpleColor((; r=UInt8(r), g=UInt8(g), b=UInt8(b))) -SimpleColor(rgb::UInt32) = SimpleColor(reverse(reinterpret(UInt8, [rgb]))[2:end]...) + +function SimpleColor(rgb::UInt32) + _, g, b, r = reinterpret(NTuple{4, UInt8}, rgb) + SimpleColor(r, g, b) +end Base.convert(::Type{SimpleColor}, (; r, g, b)::RGBTuple) = SimpleColor((; r, g, b)) Base.convert(::Type{SimpleColor}, namedcolor::Symbol) = SimpleColor(namedcolor)