This is one of the many cool things that zsh comes with out of the box. It ships an autoload-able colors¹ script that does largely the same thing as your fragment, but you know it available wherever zsh is.
In much the same way it comes with both a termcap² and terminfo³ module, allowing you to easily avoid hardcoding system specific escapes in to your config.
¹ https://github.com/zsh-users/zsh/blob/master/Functions/Misc/...
² http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#The-...
³ http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#The-...