Skip to content

Commit

Permalink
Optimise creation of a SimpleColor from a UInt32
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
KristofferC authored and tecosaur committed Aug 4, 2024
1 parent 6d3f44d commit 4d04102
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/faces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4d04102

Please sign in to comment.