From 6eeff6672848c9c3e9e260f8e69e5383701ce137 Mon Sep 17 00:00:00 2001 From: faiface Date: Thu, 23 Mar 2017 23:03:07 +0100 Subject: [PATCH] add ToNRGBA function for performance --- color.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/color.go b/color.go index 7345e4a..45d424c 100644 --- a/color.go +++ b/color.go @@ -75,10 +75,9 @@ func clamp(x, low, high float64) float64 { return x } -// NRGBAModel converts colors to NRGBA format. -var NRGBAModel = color.ModelFunc(nrgbaModel) - -func nrgbaModel(c color.Color) color.Color { +// ToNRGBA converts a color to NRGBA format. Using this function is preferred to using NRGBAModel, +// for performance (using NRGBAModel introduced additional unnecessary allocations). +func ToNRGBA(c color.Color) NRGBA { if c, ok := c.(NRGBA); ok { return c } @@ -101,3 +100,10 @@ func nrgbaModel(c color.Color) color.Color { float64(a) / 0xffff, } } + +// NRGBAModel converts colors to NRGBA format. +var NRGBAModel = color.ModelFunc(nrgbaModel) + +func nrgbaModel(c color.Color) color.Color { + return ToNRGBA(c) +}