summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2017-02-06 18:46:53 -0500
committerLuke Shumaker <lukeshu@sbcglobal.net>2017-02-06 18:47:43 -0500
commit2692e95db86b454a92e760446bb4e1424f79e461 (patch)
tree9f8fc1f62c93a10194f5288e5b9f01ca9e61a17b
parentbaf468faa6cea9de82b057433746bfc3ca6aa24a (diff)
Tidy go color interpolation.
It kinda-ish matches the cleaner JS version. It is more awkward because it has to do type conversions. This is actually a little longer (9 lines), but I think its easier to understand.
-rw-r--r--crtsh-pem2html.go65
-rw-r--r--tls-pem2html.go65
2 files changed, 74 insertions, 56 deletions
diff --git a/crtsh-pem2html.go b/crtsh-pem2html.go
index 0bad106..518c919 100644
--- a/crtsh-pem2html.go
+++ b/crtsh-pem2html.go
@@ -81,50 +81,59 @@ func getNow() time.Time {
var now = getNow()
-type interpolation struct {
- ta, tb time.Time
- ba, bb byte
+type DateRange struct {
+ a, b time.Time
}
-func (i interpolation) interpolate(tc time.Time) byte {
- db := i.tb.Sub(i.ta)
- dc := tc.Sub(i.ta)
+func (dr DateRange) ToPct(point time.Time) float64 {
+ dur_ab := dr.b.Sub(dr.a)
+ dur_ap := point.Sub(dr.a)
+ return float64(dur_ap) / float64(dur_ab)
+}
+
+type ByteRange struct {
+ a, b byte
+}
+
+func (br ByteRange) FromPct(pct float64) byte {
+ ab := int16(br.b) - int16(br.a)
+ ap := int16(pct * float64(ab))
+ return byte(int16(br.a) + ap)
+}
- pct := float64(dc) / float64(db)
+func CapPct(pct float64) float64 {
if pct < 0 {
pct = 0
} else if pct > 1 {
pct = 1
}
-
- sb := int16(i.bb) - int16(i.ba)
- sc := int16(pct * float64(sb))
-
- return byte(int16(i.ba) + sc)
-}
-
-var daysago = interpolation{
- ta: now.AddDate(0, 0, -30),
- tb: now,
- ba: 0xF3,
- bb: 0x00,
+ return pct
}
-var daysuntil = interpolation{
- ta: now,
- tb: now.AddDate(0, 0, 30),
- ba: 0x00,
- bb: 0xF3,
+func mapRange(dr DateRange, br ByteRange, t time.Time) byte {
+ return br.FromPct(CapPct(dr.ToPct(t)))
}
func green(t time.Time) string {
- b := daysago.interpolate(t)
- return fmt.Sprintf("#%02X%02X%02X", b, 0xF3, b)
+ max := byte(0xF3)
+ // 30 days ago: 0 green
+ // now: max green
+ greenness := mapRange(
+ DateRange{now.AddDate(0, 0, -30), now},
+ ByteRange{0, max},
+ t)
+ return fmt.Sprintf("#%02X%02X%02X", max-greenness, max, max-greenness)
}
func red(t time.Time) string {
- b := daysuntil.interpolate(t)
- return fmt.Sprintf("#%02X%02X%02X", 0xF3, b, b)
+ max := byte(0xF3)
+ // now: max red
+ // 30 days from now: 0 red
+ redness := mapRange(
+ DateRange{now, now.AddDate(0, 0, 30)},
+ ByteRange{max, 0},
+ t)
+ return fmt.Sprintf("#%02X%02X%02X", max, max-redness, max-redness)
}
type Cert struct {
diff --git a/tls-pem2html.go b/tls-pem2html.go
index d339fc5..3b55f73 100644
--- a/tls-pem2html.go
+++ b/tls-pem2html.go
@@ -77,50 +77,59 @@ func getNow() time.Time {
var now = getNow()
-type interpolation struct {
- ta, tb time.Time
- ba, bb byte
+type DateRange struct {
+ a, b time.Time
}
-func (i interpolation) interpolate(tc time.Time) byte {
- db := i.tb.Sub(i.ta)
- dc := tc.Sub(i.ta)
+func (dr DateRange) ToPct(point time.Time) float64 {
+ dur_ab := dr.b.Sub(dr.a)
+ dur_ap := point.Sub(dr.a)
+ return float64(dur_ap) / float64(dur_ab)
+}
+
+type ByteRange struct {
+ a, b byte
+}
+
+func (br ByteRange) FromPct(pct float64) byte {
+ ab := int16(br.b) - int16(br.a)
+ ap := int16(pct * float64(ab))
+ return byte(int16(br.a) + ap)
+}
- pct := float64(dc) / float64(db)
+func CapPct(pct float64) float64 {
if pct < 0 {
pct = 0
} else if pct > 1 {
pct = 1
}
-
- sb := int16(i.bb) - int16(i.ba)
- sc := int16(pct * float64(sb))
-
- return byte(int16(i.ba) + sc)
-}
-
-var daysago = interpolation{
- ta: now.AddDate(0, 0, -30),
- tb: now,
- ba: 0xF3,
- bb: 0x00,
+ return pct
}
-var daysuntil = interpolation{
- ta: now,
- tb: now.AddDate(0, 0, 30),
- ba: 0x00,
- bb: 0xF3,
+func mapRange(dr DateRange, br ByteRange, t time.Time) byte {
+ return br.FromPct(CapPct(dr.ToPct(t)))
}
func green(t time.Time) string {
- b := daysago.interpolate(t)
- return fmt.Sprintf("#%02X%02X%02X", b, 0xF3, b)
+ max := byte(0xF3)
+ // 30 days ago: 0 green
+ // now: max green
+ greenness := mapRange(
+ DateRange{now.AddDate(0, 0, -30), now},
+ ByteRange{0, max},
+ t)
+ return fmt.Sprintf("#%02X%02X%02X", max-greenness, max, max-greenness)
}
func red(t time.Time) string {
- b := daysuntil.interpolate(t)
- return fmt.Sprintf("#%02X%02X%02X", 0xF3, b, b)
+ max := byte(0xF3)
+ // now: max red
+ // 30 days from now: 0 red
+ redness := mapRange(
+ DateRange{now, now.AddDate(0, 0, 30)},
+ ByteRange{max, 0},
+ t)
+ return fmt.Sprintf("#%02X%02X%02X", max, max-redness, max-redness)
}
type Cert struct {