From 6634b3ff6bcdffbab38a049460ae6ea3cd68944f Mon Sep 17 00:00:00 2001 From: sam-anthony Date: Thu, 31 Mar 2022 20:27:39 -0230 Subject: refactor and compressor map image widget --- temperature/temperature.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 temperature/temperature.go (limited to 'temperature/temperature.go') diff --git a/temperature/temperature.go b/temperature/temperature.go new file mode 100644 index 0000000..2a135df --- /dev/null +++ b/temperature/temperature.go @@ -0,0 +1,71 @@ +package temperature + +import ( + "errors" + "fmt" +) + +type unit int + +const ( + Celcius unit = iota + Kelvin + Fahrenheit +) + +// UnitStrings returns a slice of strings, each representing a +// unit. +// This is necessary because giu.Combo only works with strings. +func UnitStrings() []string { + return []string{"°C", "°K", "°F"} +} + +const ( + DefaultUnit unit = Celcius + // DefaultUnitIndex is used to index UnitStrings(). + DefaultUnitIndex int32 = 0 // celcius +) + +func UnitFromString(s string) (unit, error) { + // Each case corresponds to a value in UnitStrings(). + switch s { + case "°C": + return Celcius, nil + case "°K": + return Kelvin, nil + case "°F": + return Fahrenheit, nil + default: + return *new(unit), errors.New(fmt.Sprintf("invalid unit: '%s'", s)) + } +} + +type Temperature struct { + Val float32 + Unit unit +} + +func (t Temperature) AsUnit(u unit) (float32, error) { + // Convert to celcius + var c float32 + switch t.Unit { + case Celcius: + c = t.Val + case Kelvin: + c = t.Val - 272.15 + case Fahrenheit: + c = (t.Val - 32.0) * (5.0 / 9.0) + } + + // Convert to desired unit + switch u { + case Celcius: + return c, nil + case Kelvin: + return c + 272.15, nil + case Fahrenheit: + return c*(9.0/5.0) + 32.0, nil + default: + return 0, errors.New(fmt.Sprintf("invalid unit: '%v'", u)) + } +} -- cgit v1.2.3