summaryrefslogtreecommitdiff
path: root/scripts/gui-common/temperature.js
blob: ba8161a44cbc47a42756a9029fec9dcfc7f0cb4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict';

class Temperature {

    /// Temperature in kelvin
    #temp = 0;

    /**
     * Constructor
     * @param {number} k Temperature in kelvin
     */
    constructor(k) {
        this.#temp = k;
    }

    /**
     * Create a new temperature object using celcius
     * @param {number} c 
     * @returns {Temperature}
     */
    static from_celcius(c) {
        return new Temperature(c + 273.15);
    }

    /**
     * Get the temperature in celcius
     * @returns {number}
     */
    to_celcius() {
        return this.#temp - 273.15;
    }

    /**
     * Create a new temperature object using fahrenheit
     * @param {number} f 
     * @returns {Temperature}
     */
    static from_fahrenheit(f) {
        return new Temperature((f + 459.67) * 5 / 9);
    }

    /**
     * Get the temperature in fahrenheit
     * @returns {number}
     */
    to_fahrenheit() {
        return (this.#temp * 9 / 5) - 459.67;
    }
    
    /**
     * From degrees halc
     * @param {number} h
     * @returns {Temperature} 
     */
    static from_halc(h) {
        return Temperature.from_celcius(h / 2);
    }

    /**
     * To degrees halc
     * @returns {number}
     */
    to_halc() {
        return this.to_celcius() * 2;
    }

    /**
     * Get the temperature in kelvin
     * @returns {number}
     */
    to_kelvin() {
        return this.#temp;
    }

    /**
     * Add the temperature to the current temperature
     * @param {Temperature} t 
     */
    add(t) {
        this.#temp += t.#temp;
    }

    /**
     * Subtract the temperature from the current temperature
     * @param {Temperature} t
     */
    sub(t) {
        this.#temp -= t.#temp;
    }
}