39 lines
870 B
Go
39 lines
870 B
Go
package utilities
|
|
|
|
import (
|
|
"fmt"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type Ratio struct {
|
|
Numerator int
|
|
Denominator int
|
|
}
|
|
|
|
func (r Ratio) Float64() float64 {
|
|
return float64(r.Numerator) / float64(r.Denominator)
|
|
}
|
|
|
|
func (r *Ratio) UnmarshalJSON(buf []byte) error {
|
|
_, err := fmt.Sscanf(string(buf), "\"%d:%d\"", &r.Numerator, &r.Denominator)
|
|
return err
|
|
}
|
|
|
|
func (r *Ratio) UnmarshalYAML(node *yaml.Node) error {
|
|
_, err := fmt.Sscanf(node.Value, "%d:%d", &r.Numerator, &r.Denominator)
|
|
return err
|
|
}
|
|
|
|
func (r Ratio) MarshalJSON() ([]byte, error) {
|
|
return []byte("\"" + r.String() + "\""), nil
|
|
}
|
|
|
|
func (r Ratio) String() string {
|
|
return fmt.Sprintf("%d:%d", r.Numerator, r.Denominator)
|
|
}
|
|
|
|
// Reciprocal get the reciprocal, for example, to convert frame rate into time base
|
|
func (r Ratio) Reciprocal() Ratio {
|
|
return Ratio{Numerator: r.Denominator, Denominator: r.Numerator}
|
|
}
|