Created
May 1, 2019 03:42
-
-
Save kmancher/9145079fdd48d6ae98beaeef740fc4ee to your computer and use it in GitHub Desktop.
sky color api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //let h1 = 218; | |
| //let s1 = 35; | |
| //let b1 = 7; | |
| //let h = 218; | |
| //let s = 35; | |
| //let b = 7; | |
| //let hr, mn, currentTime, baltimoreTime; | |
| //let currentTime; | |
| let city = []; | |
| function setup() { | |
| createCanvas(800, 400); | |
| noStroke(); | |
| colorMode(HSB); | |
| background(0); | |
| city[0] = new SkyBox(39.299236, -100.609383, 0, 0); | |
| city[1] = new SkyBox(39.299236, 40.609383, 200, 0); | |
| setTimeout(getCity, 5000); | |
| } | |
| function getCity() { | |
| city[0].update(); | |
| city[0].display(); | |
| // | |
| city[1].update(); | |
| city[1].display(); | |
| setTimeout(getCity, 5000); | |
| } | |
| function draw() { | |
| } | |
| function skyColor(data) { | |
| let hr = hour(); | |
| let mn = minute(); | |
| if (mn < 10) { | |
| mn = "0" + mn; | |
| } | |
| if (hr < 10) { | |
| hr = "0" + hr; | |
| } | |
| // this.timeZone = timeZone; | |
| let currentTime = timeToDec(hr + ":" + mn) - 10; | |
| this.astTwilightStart = timeToDec(data.results.astronomical_twilight_begin); | |
| this.nautTwilightStart = timeToDec(data.results.nautical_twilight_begin); | |
| this.civTwilightStart = timeToDec(data.results.civil_twilight_begin); | |
| this.sunrise = timeToDec(data.results.sunrise); | |
| this.noon = timeToDec(data.results.solar_noon); | |
| this.sunset = timeToDec(data.results.sunset); | |
| this.civTwilightEnd = timeToDec(data.results.civil_twilight_end); | |
| this.nautTwilightEnd = timeToDec(data.results.nautical_twilight_end); | |
| this.astTwilightEnd = timeToDec(data.results.astronomical_twilight_end); | |
| this.midnight = 23.99999999999; | |
| console.log("Time : " + currentTime); | |
| // var d = new Date(); | |
| // var n = d.getTimezoneOffset(); | |
| // console.log (n); | |
| if (astTwilightStart < currentTime && currentTime < sunrise) { | |
| this.h = 218; | |
| this.s = 35; | |
| this.b = map(currentTime, twilightStart, sunrise, 7, 80); | |
| console.log("dawn"); | |
| fill(this.h, this.s, this.b); | |
| } else if (sunrise < currentTime && currentTime < noon) { | |
| h = 218; | |
| s = map(currentTime, sunrise, noon, 35, 73); | |
| b = map(currentTime, sunrise, noon, 80, 100); | |
| console.log("morning"); | |
| fill(h, s, b); | |
| } else if (noon < currentTime && currentTime < sunset) { | |
| h = 218; | |
| s = 73; | |
| b = 100; | |
| console.log("afternoon"); | |
| fill(h, s, b); | |
| } else if (sunset < currentTime && currentTime < ((sunset + civTwilightEnd) / 2)) { | |
| this.h = map(currentTime, sunset, civTwilightEnd, 218, 340); | |
| this.s = 73; | |
| this.b = 100; | |
| console.log("early evening"); | |
| fill(this.h, this.s, this.b); | |
| } else if (((sunset + civTwilightEnd) / 2) < currentTime && currentTime < sunset) { | |
| this.h = map(currentTime, sunset, civTwilightEnd, 340, 230); | |
| this.s = 73; | |
| this.b = 100; | |
| console.log("evening"); | |
| fill(this.h, this.s, this.b); | |
| } else if (sunset < currentTime && currentTime < astTwilightEnd) { | |
| this.h = map(currentTime, sunset, astTwilightEnd, 218, 300); | |
| this.s = map(currentTime, sunset, astTwilightEnd, 73, 35); | |
| this.b = map(currentTime, sunset, astTwilightEnd, 100, 50); | |
| console.log("evening"); | |
| fill(this.h, this.s, this.b); | |
| } else if (astTwilightEnd < currentTime && currentTime < midnight) { | |
| this.h = map(currentTime, astTwilightEnd, midnight, 300, 218); | |
| this.s = 35; | |
| this.b = map(currentTime, astTwilightEnd, midnight, 50, 7); | |
| console.log("night"); | |
| fill(this.h, this.s, this.b); | |
| } else { | |
| this.h = 218; | |
| this.s = 35; | |
| this.b = 7; | |
| console.log("go to sleep"); | |
| fill(this.h, this.s, this.b); | |
| } | |
| // fill(this.h, this.s, this.b); | |
| console.log("s: " + this.s); | |
| // if (city[0]) { | |
| // console.log("good morning baltimore"); | |
| // rect(0, 0, 200, 200); | |
| // } | |
| // | |
| // if (city[1]) { | |
| // rect(200, 0, 200, 200); | |
| // } | |
| } | |
| class SkyBox { | |
| constructor(tempLatitude, tempLongitude, tempX, tempY) { | |
| this.url; | |
| //location & time data variables | |
| this.latitude = tempLatitude; | |
| this.longitude = tempLongitude; | |
| //sun data variables | |
| this.astTwilightStart; | |
| this.nautTwilightStart; | |
| this.civTwilightStart; | |
| this.sunrise; | |
| this.noon; | |
| this.sunset; | |
| this.civTwilightEnd; | |
| this.nautTwilightEnd; | |
| this.astTwilightEnd; | |
| this.midnight = 23.99999999999; | |
| //box position & color; | |
| this.x = tempX; | |
| this.y = tempY; | |
| // this.x; | |
| // this.y; | |
| this.h; | |
| this.s; | |
| this.b; | |
| } | |
| // Custom method for updating the variables | |
| update() { | |
| this.url = "https://api.sunrise-sunset.org/json?lat=" + this.latitude + "&lng=" + this.longitude + "&date=today"; | |
| loadJSON(this.url, skyColor); | |
| this.data = loadJSON(this.url, skyColor); | |
| // console.log("testttttt" + skyColor()); | |
| if (this.mn < 10) { | |
| this.mn = "0" + this.mn; | |
| } | |
| if (this.hr < 10) { | |
| this.hr = "0" + this.hr; | |
| } | |
| // console.log(this.url); | |
| this.currentTime = timeToDec(this.hr + ":" + this.mn) + this.timeZone + 4; | |
| } | |
| // Custom method for drawing the object | |
| display() { | |
| // fill(this.h, this.s, this.b); | |
| // fill(globalH, globalS, globalB); | |
| // rect(this.x, this.y, 200, 200); | |
| rect(this.x, this.y, 200, 200); | |
| } | |
| } | |
| function timeToDec(origTime) { | |
| let timeDec; | |
| let splitTime = splitTokens(origTime, ": "); | |
| if (splitTime[3] == "PM" && splitTime[0] < 12) { | |
| timeDec = (splitTime[0] * 1) + (splitTime[1] / 60) + 12; | |
| } else { | |
| timeDec = (splitTime[0] * 1) + (splitTime[1] / 60); | |
| } | |
| return timeDec; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment