From 73a500482abff71a652c6112ab79ba37d6aad514 Mon Sep 17 00:00:00 2001 From: gagliardetto Date: Fri, 6 Jan 2023 16:59:00 +0100 Subject: [PATCH] Remove `github.com/aybabtme/rgbterm` dependency; copy only needed parts --- go.mod | 1 - go.sum | 2 - text/codes.go | 577 ++++++++++++++++++++++++++++++++++++++++++++++++ text/encoder.go | 12 +- text/rgbterm.go | 180 +++++++++++++++ text/tools.go | 45 ++-- 6 files changed, 787 insertions(+), 30 deletions(-) create mode 100644 text/codes.go create mode 100644 text/rgbterm.go diff --git a/go.mod b/go.mod index a026efea..0d254afe 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,6 @@ require ( filippo.io/edwards25519 v1.0.0-rc.1 github.com/AlekSi/pointer v1.1.0 github.com/GeertJohan/go.rice v1.0.0 - github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 github.com/buger/jsonparser v1.1.1 github.com/davecgh/go-spew v1.1.1 github.com/fatih/color v1.9.0 diff --git a/go.sum b/go.sum index 79000028..f5686ee2 100644 --- a/go.sum +++ b/go.sum @@ -48,8 +48,6 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmV github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/aws/aws-sdk-go v1.22.1/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/aws/aws-sdk-go v1.23.20/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= -github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 h1:WWB576BN5zNSZc/M9d/10pqEx5VHNhaQ/yOVAkmj5Yo= -github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= diff --git a/text/codes.go b/text/codes.go new file mode 100644 index 00000000..291df30b --- /dev/null +++ b/text/codes.go @@ -0,0 +1,577 @@ +package text + +// This code is a modified version of the rgbterm package: +// see https://github.com/aybabtme/rgbterm/blob/master/LICENSE +// Original license: +// +// The MIT License + +// Copyright (c) 2014, Antoine Grondin. + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +// > Except for functions HSLtoRGB and RGBtoHSL, under a BSD 2 clause license: + +// Copyright (c) 2012 Rodrigo Moraes. All rights reserved. + +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: + +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. + +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// \033[ + +var fgTermRGB = [...][]byte{ + []byte("38;5;0"), + []byte("38;5;1"), + []byte("38;5;2"), + []byte("38;5;3"), + []byte("38;5;4"), + []byte("38;5;5"), + []byte("38;5;6"), + []byte("38;5;7"), + []byte("38;5;8"), + []byte("38;5;9"), + []byte("38;5;10"), + []byte("38;5;11"), + []byte("38;5;12"), + []byte("38;5;13"), + []byte("38;5;14"), + []byte("38;5;15"), + []byte("38;5;16"), + []byte("38;5;17"), + []byte("38;5;18"), + []byte("38;5;19"), + []byte("38;5;20"), + []byte("38;5;21"), + []byte("38;5;22"), + []byte("38;5;23"), + []byte("38;5;24"), + []byte("38;5;25"), + []byte("38;5;26"), + []byte("38;5;27"), + []byte("38;5;28"), + []byte("38;5;29"), + []byte("38;5;30"), + []byte("38;5;31"), + []byte("38;5;32"), + []byte("38;5;33"), + []byte("38;5;34"), + []byte("38;5;35"), + []byte("38;5;36"), + []byte("38;5;37"), + []byte("38;5;38"), + []byte("38;5;39"), + []byte("38;5;40"), + []byte("38;5;41"), + []byte("38;5;42"), + []byte("38;5;43"), + []byte("38;5;44"), + []byte("38;5;45"), + []byte("38;5;46"), + []byte("38;5;47"), + []byte("38;5;48"), + []byte("38;5;49"), + []byte("38;5;50"), + []byte("38;5;51"), + []byte("38;5;52"), + []byte("38;5;53"), + []byte("38;5;54"), + []byte("38;5;55"), + []byte("38;5;56"), + []byte("38;5;57"), + []byte("38;5;58"), + []byte("38;5;59"), + []byte("38;5;60"), + []byte("38;5;61"), + []byte("38;5;62"), + []byte("38;5;63"), + []byte("38;5;64"), + []byte("38;5;65"), + []byte("38;5;66"), + []byte("38;5;67"), + []byte("38;5;68"), + []byte("38;5;69"), + []byte("38;5;70"), + []byte("38;5;71"), + []byte("38;5;72"), + []byte("38;5;73"), + []byte("38;5;74"), + []byte("38;5;75"), + []byte("38;5;76"), + []byte("38;5;77"), + []byte("38;5;78"), + []byte("38;5;79"), + []byte("38;5;80"), + []byte("38;5;81"), + []byte("38;5;82"), + []byte("38;5;83"), + []byte("38;5;84"), + []byte("38;5;85"), + []byte("38;5;86"), + []byte("38;5;87"), + []byte("38;5;88"), + []byte("38;5;89"), + []byte("38;5;90"), + []byte("38;5;91"), + []byte("38;5;92"), + []byte("38;5;93"), + []byte("38;5;94"), + []byte("38;5;95"), + []byte("38;5;96"), + []byte("38;5;97"), + []byte("38;5;98"), + []byte("38;5;99"), + []byte("38;5;100"), + []byte("38;5;101"), + []byte("38;5;102"), + []byte("38;5;103"), + []byte("38;5;104"), + []byte("38;5;105"), + []byte("38;5;106"), + []byte("38;5;107"), + []byte("38;5;108"), + []byte("38;5;109"), + []byte("38;5;110"), + []byte("38;5;111"), + []byte("38;5;112"), + []byte("38;5;113"), + []byte("38;5;114"), + []byte("38;5;115"), + []byte("38;5;116"), + []byte("38;5;117"), + []byte("38;5;118"), + []byte("38;5;119"), + []byte("38;5;120"), + []byte("38;5;121"), + []byte("38;5;122"), + []byte("38;5;123"), + []byte("38;5;124"), + []byte("38;5;125"), + []byte("38;5;126"), + []byte("38;5;127"), + []byte("38;5;128"), + []byte("38;5;129"), + []byte("38;5;130"), + []byte("38;5;131"), + []byte("38;5;132"), + []byte("38;5;133"), + []byte("38;5;134"), + []byte("38;5;135"), + []byte("38;5;136"), + []byte("38;5;137"), + []byte("38;5;138"), + []byte("38;5;139"), + []byte("38;5;140"), + []byte("38;5;141"), + []byte("38;5;142"), + []byte("38;5;143"), + []byte("38;5;144"), + []byte("38;5;145"), + []byte("38;5;146"), + []byte("38;5;147"), + []byte("38;5;148"), + []byte("38;5;149"), + []byte("38;5;150"), + []byte("38;5;151"), + []byte("38;5;152"), + []byte("38;5;153"), + []byte("38;5;154"), + []byte("38;5;155"), + []byte("38;5;156"), + []byte("38;5;157"), + []byte("38;5;158"), + []byte("38;5;159"), + []byte("38;5;160"), + []byte("38;5;161"), + []byte("38;5;162"), + []byte("38;5;163"), + []byte("38;5;164"), + []byte("38;5;165"), + []byte("38;5;166"), + []byte("38;5;167"), + []byte("38;5;168"), + []byte("38;5;169"), + []byte("38;5;170"), + []byte("38;5;171"), + []byte("38;5;172"), + []byte("38;5;173"), + []byte("38;5;174"), + []byte("38;5;175"), + []byte("38;5;176"), + []byte("38;5;177"), + []byte("38;5;178"), + []byte("38;5;179"), + []byte("38;5;180"), + []byte("38;5;181"), + []byte("38;5;182"), + []byte("38;5;183"), + []byte("38;5;184"), + []byte("38;5;185"), + []byte("38;5;186"), + []byte("38;5;187"), + []byte("38;5;188"), + []byte("38;5;189"), + []byte("38;5;190"), + []byte("38;5;191"), + []byte("38;5;192"), + []byte("38;5;193"), + []byte("38;5;194"), + []byte("38;5;195"), + []byte("38;5;196"), + []byte("38;5;197"), + []byte("38;5;198"), + []byte("38;5;199"), + []byte("38;5;200"), + []byte("38;5;201"), + []byte("38;5;202"), + []byte("38;5;203"), + []byte("38;5;204"), + []byte("38;5;205"), + []byte("38;5;206"), + []byte("38;5;207"), + []byte("38;5;208"), + []byte("38;5;209"), + []byte("38;5;210"), + []byte("38;5;211"), + []byte("38;5;212"), + []byte("38;5;213"), + []byte("38;5;214"), + []byte("38;5;215"), + []byte("38;5;216"), + []byte("38;5;217"), + []byte("38;5;218"), + []byte("38;5;219"), + []byte("38;5;220"), + []byte("38;5;221"), + []byte("38;5;222"), + []byte("38;5;223"), + []byte("38;5;224"), + []byte("38;5;225"), + []byte("38;5;226"), + []byte("38;5;227"), + []byte("38;5;228"), + []byte("38;5;229"), + []byte("38;5;230"), + []byte("38;5;231"), + []byte("38;5;232"), + []byte("38;5;233"), + []byte("38;5;234"), + []byte("38;5;235"), + []byte("38;5;236"), + []byte("38;5;237"), + []byte("38;5;238"), + []byte("38;5;239"), + []byte("38;5;240"), + []byte("38;5;241"), + []byte("38;5;242"), + []byte("38;5;243"), + []byte("38;5;244"), + []byte("38;5;245"), + []byte("38;5;246"), + []byte("38;5;247"), + []byte("38;5;248"), + []byte("38;5;249"), + []byte("38;5;250"), + []byte("38;5;251"), + []byte("38;5;252"), + []byte("38;5;253"), + []byte("38;5;254"), + []byte("38;5;255"), +} + +var bgTermRGB = [...][]byte{ + []byte("48;5;0"), + []byte("48;5;1"), + []byte("48;5;2"), + []byte("48;5;3"), + []byte("48;5;4"), + []byte("48;5;5"), + []byte("48;5;6"), + []byte("48;5;7"), + []byte("48;5;8"), + []byte("48;5;9"), + []byte("48;5;10"), + []byte("48;5;11"), + []byte("48;5;12"), + []byte("48;5;13"), + []byte("48;5;14"), + []byte("48;5;15"), + []byte("48;5;16"), + []byte("48;5;17"), + []byte("48;5;18"), + []byte("48;5;19"), + []byte("48;5;20"), + []byte("48;5;21"), + []byte("48;5;22"), + []byte("48;5;23"), + []byte("48;5;24"), + []byte("48;5;25"), + []byte("48;5;26"), + []byte("48;5;27"), + []byte("48;5;28"), + []byte("48;5;29"), + []byte("48;5;30"), + []byte("48;5;31"), + []byte("48;5;32"), + []byte("48;5;33"), + []byte("48;5;34"), + []byte("48;5;35"), + []byte("48;5;36"), + []byte("48;5;37"), + []byte("48;5;38"), + []byte("48;5;39"), + []byte("48;5;40"), + []byte("48;5;41"), + []byte("48;5;42"), + []byte("48;5;43"), + []byte("48;5;44"), + []byte("48;5;45"), + []byte("48;5;46"), + []byte("48;5;47"), + []byte("48;5;48"), + []byte("48;5;49"), + []byte("48;5;50"), + []byte("48;5;51"), + []byte("48;5;52"), + []byte("48;5;53"), + []byte("48;5;54"), + []byte("48;5;55"), + []byte("48;5;56"), + []byte("48;5;57"), + []byte("48;5;58"), + []byte("48;5;59"), + []byte("48;5;60"), + []byte("48;5;61"), + []byte("48;5;62"), + []byte("48;5;63"), + []byte("48;5;64"), + []byte("48;5;65"), + []byte("48;5;66"), + []byte("48;5;67"), + []byte("48;5;68"), + []byte("48;5;69"), + []byte("48;5;70"), + []byte("48;5;71"), + []byte("48;5;72"), + []byte("48;5;73"), + []byte("48;5;74"), + []byte("48;5;75"), + []byte("48;5;76"), + []byte("48;5;77"), + []byte("48;5;78"), + []byte("48;5;79"), + []byte("48;5;80"), + []byte("48;5;81"), + []byte("48;5;82"), + []byte("48;5;83"), + []byte("48;5;84"), + []byte("48;5;85"), + []byte("48;5;86"), + []byte("48;5;87"), + []byte("48;5;88"), + []byte("48;5;89"), + []byte("48;5;90"), + []byte("48;5;91"), + []byte("48;5;92"), + []byte("48;5;93"), + []byte("48;5;94"), + []byte("48;5;95"), + []byte("48;5;96"), + []byte("48;5;97"), + []byte("48;5;98"), + []byte("48;5;99"), + []byte("48;5;100"), + []byte("48;5;101"), + []byte("48;5;102"), + []byte("48;5;103"), + []byte("48;5;104"), + []byte("48;5;105"), + []byte("48;5;106"), + []byte("48;5;107"), + []byte("48;5;108"), + []byte("48;5;109"), + []byte("48;5;110"), + []byte("48;5;111"), + []byte("48;5;112"), + []byte("48;5;113"), + []byte("48;5;114"), + []byte("48;5;115"), + []byte("48;5;116"), + []byte("48;5;117"), + []byte("48;5;118"), + []byte("48;5;119"), + []byte("48;5;120"), + []byte("48;5;121"), + []byte("48;5;122"), + []byte("48;5;123"), + []byte("48;5;124"), + []byte("48;5;125"), + []byte("48;5;126"), + []byte("48;5;127"), + []byte("48;5;128"), + []byte("48;5;129"), + []byte("48;5;130"), + []byte("48;5;131"), + []byte("48;5;132"), + []byte("48;5;133"), + []byte("48;5;134"), + []byte("48;5;135"), + []byte("48;5;136"), + []byte("48;5;137"), + []byte("48;5;138"), + []byte("48;5;139"), + []byte("48;5;140"), + []byte("48;5;141"), + []byte("48;5;142"), + []byte("48;5;143"), + []byte("48;5;144"), + []byte("48;5;145"), + []byte("48;5;146"), + []byte("48;5;147"), + []byte("48;5;148"), + []byte("48;5;149"), + []byte("48;5;150"), + []byte("48;5;151"), + []byte("48;5;152"), + []byte("48;5;153"), + []byte("48;5;154"), + []byte("48;5;155"), + []byte("48;5;156"), + []byte("48;5;157"), + []byte("48;5;158"), + []byte("48;5;159"), + []byte("48;5;160"), + []byte("48;5;161"), + []byte("48;5;162"), + []byte("48;5;163"), + []byte("48;5;164"), + []byte("48;5;165"), + []byte("48;5;166"), + []byte("48;5;167"), + []byte("48;5;168"), + []byte("48;5;169"), + []byte("48;5;170"), + []byte("48;5;171"), + []byte("48;5;172"), + []byte("48;5;173"), + []byte("48;5;174"), + []byte("48;5;175"), + []byte("48;5;176"), + []byte("48;5;177"), + []byte("48;5;178"), + []byte("48;5;179"), + []byte("48;5;180"), + []byte("48;5;181"), + []byte("48;5;182"), + []byte("48;5;183"), + []byte("48;5;184"), + []byte("48;5;185"), + []byte("48;5;186"), + []byte("48;5;187"), + []byte("48;5;188"), + []byte("48;5;189"), + []byte("48;5;190"), + []byte("48;5;191"), + []byte("48;5;192"), + []byte("48;5;193"), + []byte("48;5;194"), + []byte("48;5;195"), + []byte("48;5;196"), + []byte("48;5;197"), + []byte("48;5;198"), + []byte("48;5;199"), + []byte("48;5;200"), + []byte("48;5;201"), + []byte("48;5;202"), + []byte("48;5;203"), + []byte("48;5;204"), + []byte("48;5;205"), + []byte("48;5;206"), + []byte("48;5;207"), + []byte("48;5;208"), + []byte("48;5;209"), + []byte("48;5;210"), + []byte("48;5;211"), + []byte("48;5;212"), + []byte("48;5;213"), + []byte("48;5;214"), + []byte("48;5;215"), + []byte("48;5;216"), + []byte("48;5;217"), + []byte("48;5;218"), + []byte("48;5;219"), + []byte("48;5;220"), + []byte("48;5;221"), + []byte("48;5;222"), + []byte("48;5;223"), + []byte("48;5;224"), + []byte("48;5;225"), + []byte("48;5;226"), + []byte("48;5;227"), + []byte("48;5;228"), + []byte("48;5;229"), + []byte("48;5;230"), + []byte("48;5;231"), + []byte("48;5;232"), + []byte("48;5;233"), + []byte("48;5;234"), + []byte("48;5;235"), + []byte("48;5;236"), + []byte("48;5;237"), + []byte("48;5;238"), + []byte("48;5;239"), + []byte("48;5;240"), + []byte("48;5;241"), + []byte("48;5;242"), + []byte("48;5;243"), + []byte("48;5;244"), + []byte("48;5;245"), + []byte("48;5;246"), + []byte("48;5;247"), + []byte("48;5;248"), + []byte("48;5;249"), + []byte("48;5;250"), + []byte("48;5;251"), + []byte("48;5;252"), + []byte("48;5;253"), + []byte("48;5;254"), + []byte("48;5;255"), +} diff --git a/text/encoder.go b/text/encoder.go index 02627996..fb1d14ae 100644 --- a/text/encoder.go +++ b/text/encoder.go @@ -23,10 +23,12 @@ import ( "github.com/fatih/color" ) -var EncoderColorCyan = color.New(color.FgCyan) -var EncoderColorYellow = color.New(color.FgYellow) -var EncoderColorGreen = color.New(color.FgGreen) -var EncoderColorWhite = color.New(color.FgWhite) +var ( + EncoderColorCyan = color.New(36) + EncoderColorYellow = color.New(33) + EncoderColorGreen = color.New(32) + EncoderColorWhite = color.New(37) +) type Encoder struct { output io.Writer @@ -42,6 +44,7 @@ func NewEncoder(w io.Writer) *Encoder { func isZero(rv reflect.Value) (b bool) { return rv.Kind() == 0 } + func isNil(rv reflect.Value) (b bool) { defer func(b bool) { if err := recover(); err != nil { @@ -50,6 +53,7 @@ func isNil(rv reflect.Value) (b bool) { }(b) return rv.IsNil() } + func (e *Encoder) Encode(v interface{}, option *Option) (err error) { if option == nil { option = &Option{} diff --git a/text/rgbterm.go b/text/rgbterm.go new file mode 100644 index 00000000..968080d2 --- /dev/null +++ b/text/rgbterm.go @@ -0,0 +1,180 @@ +package text + +// This code is a modified version of the rgbterm package: +// see https://github.com/aybabtme/rgbterm/blob/master/LICENSE +// Original license: +// +// The MIT License + +// Copyright (c) 2014, Antoine Grondin. + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +// > Except for functions HSLtoRGB and RGBtoHSL, under a BSD 2 clause license: + +// Copyright (c) 2012 Rodrigo Moraes. All rights reserved. + +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: + +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. + +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +var ( + before = []byte("\033[") + after = []byte("m") + reset = []byte("\033[0;00m") + fgcolors = fgTermRGB[16:232] + bgcolors = bgTermRGB[16:232] +) + +// FgString colorizes the foreground of the input with the terminal color +// that matches the closest the RGB color. +// +// This is simply a helper for Bytes. +func FgString(in string, r, g, b uint8) string { + return string(FgBytes([]byte(in), r, g, b)) +} + +// BgString colorizes the background of the input with the terminal color +// that matches the closest the RGB color. +// +// This is simply a helper for Bytes. +func BgString(in string, r, g, b uint8) string { + return string(BgBytes([]byte(in), r, g, b)) +} + +// Bytes colorizes the foreground with the terminal color that matches +// the closest the RGB color. +func FgBytes(in []byte, r, g, b uint8) []byte { + return colorize(color_(r, g, b, true), in) +} + +// BgBytes colorizes the background of the input with the terminal color +// that matches the closest the RGB color. +func BgBytes(in []byte, r, g, b uint8) []byte { + return colorize(color_(r, g, b, false), in) +} + +func colorize(color, in []byte) []byte { + return append(append(append(append(before, color...), after...), in...), reset...) +} + +func color_(r, g, b uint8, foreground bool) []byte { + // if all colors are equal, it might be in the grayscale range + if r == g && g == b { + color, ok := grayscale(r, foreground) + if ok { + return color + } + } + + // the general case approximates RGB by using the closest color. + r6 := ((uint16(r) * 5) / 255) + g6 := ((uint16(g) * 5) / 255) + b6 := ((uint16(b) * 5) / 255) + i := 36*r6 + 6*g6 + b6 + if foreground { + return fgcolors[i] + } else { + return bgcolors[i] + } +} + +func grayscale(scale uint8, foreground bool) ([]byte, bool) { + var source [256][]byte + + if foreground { + source = fgTermRGB + } else { + source = bgTermRGB + } + + switch scale { + case 0x08: + return source[232], true + case 0x12: + return source[233], true + case 0x1c: + return source[234], true + case 0x26: + return source[235], true + case 0x30: + return source[236], true + case 0x3a: + return source[237], true + case 0x44: + return source[238], true + case 0x4e: + return source[239], true + case 0x58: + return source[240], true + case 0x62: + return source[241], true + case 0x6c: + return source[242], true + case 0x76: + return source[243], true + case 0x80: + return source[244], true + case 0x8a: + return source[245], true + case 0x94: + return source[246], true + case 0x9e: + return source[247], true + case 0xa8: + return source[248], true + case 0xb2: + return source[249], true + case 0xbc: + return source[250], true + case 0xc6: + return source[251], true + case 0xd0: + return source[252], true + case 0xda: + return source[253], true + case 0xe4: + return source[254], true + case 0xee: + return source[255], true + } + return nil, false +} diff --git a/text/tools.go b/text/tools.go index 387304c9..a09009f2 100644 --- a/text/tools.go +++ b/text/tools.go @@ -21,8 +21,6 @@ import ( "math" "strings" "sync" - - "github.com/aybabtme/rgbterm" ) var DisableColors = false @@ -30,9 +28,11 @@ var DisableColors = false func S(a ...interface{}) string { return fmt.Sprint(a...) } + func Sf(format string, a ...interface{}) string { return fmt.Sprintf(format, a...) } + func Ln(a ...interface{}) string { return fmt.Sprintln(a...) } @@ -56,81 +56,81 @@ func Black(str string) string { if DisableColors { return str } - return rgbterm.FgString(str, 0, 0, 0) + return FgString(str, 0, 0, 0) } func White(str string) string { if DisableColors { return str } - return rgbterm.FgString(str, 255, 255, 255) + return FgString(str, 255, 255, 255) } func BlackBG(str string) string { if DisableColors { return str } - return rgbterm.BgString(str, 0, 0, 0) + return BgString(str, 0, 0, 0) } func WhiteBG(str string) string { if DisableColors { return str } - return Black(rgbterm.BgString(str, 255, 255, 255)) + return Black(BgString(str, 255, 255, 255)) } func Lime(str string) string { if DisableColors { return str } - return rgbterm.FgString(str, 252, 255, 43) + return FgString(str, 252, 255, 43) } func LimeBG(str string) string { if DisableColors { return str } - return Black(rgbterm.BgString(str, 252, 255, 43)) + return Black(BgString(str, 252, 255, 43)) } func Yellow(str string) string { if DisableColors { return str } - return BlackBG(rgbterm.FgString(str, 255, 255, 0)) + return BlackBG(FgString(str, 255, 255, 0)) } func YellowBG(str string) string { - return Black(rgbterm.BgString(str, 255, 255, 0)) + return Black(BgString(str, 255, 255, 0)) } func Orange(str string) string { if DisableColors { return str } - return rgbterm.FgString(str, 255, 165, 0) + return FgString(str, 255, 165, 0) } func OrangeBG(str string) string { if DisableColors { return str } - return Black(rgbterm.BgString(str, 255, 165, 0)) + return Black(BgString(str, 255, 165, 0)) } func Red(str string) string { if DisableColors { return str } - return rgbterm.FgString(str, 255, 0, 0) + return FgString(str, 255, 0, 0) } func RedBG(str string) string { if DisableColors { return str } - return White(rgbterm.BgString(str, 220, 20, 60)) + return White(BgString(str, 220, 20, 60)) } // light blue? @@ -138,42 +138,42 @@ func Shakespeare(str string) string { if DisableColors { return str } - return rgbterm.FgString(str, 82, 179, 217) + return FgString(str, 82, 179, 217) } func ShakespeareBG(str string) string { if DisableColors { return str } - return White(rgbterm.BgString(str, 82, 179, 217)) + return White(BgString(str, 82, 179, 217)) } func Purple(str string) string { if DisableColors { return str } - return rgbterm.FgString(str, 255, 0, 255) + return FgString(str, 255, 0, 255) } func PurpleBG(str string) string { if DisableColors { return str } - return Black(rgbterm.BgString(str, 255, 0, 255)) + return Black(BgString(str, 255, 0, 255)) } func Indigo(str string) string { if DisableColors { return str } - return rgbterm.FgString(str, 75, 0, 130) + return FgString(str, 75, 0, 130) } func IndigoBG(str string) string { if DisableColors { return str } - return rgbterm.BgString(str, 75, 0, 130) + return BgString(str, 75, 0, 130) } func Bold(str string) string { @@ -219,7 +219,7 @@ func StringToColor(str string) func(string) string { bgColor = BlackBG } return func(str string) string { - return bgColor(rgbterm.FgString(str, uint8(r), uint8(g), uint8(b))) + return bgColor(FgString(str, uint8(r), uint8(g), uint8(b))) } } @@ -232,7 +232,7 @@ func StringToColorBG(str string) func(string) string { textColor = Black } return func(str string) string { - return textColor(rgbterm.BgString(str, uint8(r), uint8(g), uint8(b))) + return textColor(BgString(str, uint8(r), uint8(g), uint8(b))) } } @@ -263,7 +263,6 @@ func calcColor(color uint64) (red, green, blue, alpha uint64) { // IsLight returns whether the color is perceived to be a light color func IsLight(rr, gg, bb uint64) bool { - r := float64(rr) g := float64(gg) b := float64(bb)