Displaying the Weather With Serverless and Colors

Post pobrano z: Displaying the Weather With Serverless and Colors

I like to jog. Sometimes it’s cold out. Sometimes it’s cold out, but it looks like it isn’t. The sun is shining, the birds are chirping. Then you step outside in shorts and a t-shirt and realize you have roughly 2 minutes before exposure sets in.

I decided to solve this first world problem using a lightbulb to display a certain color based on what the temperature outside is. It works better than I expected, and that’s saying something because usually nothing works out like I want it to.

This was a fun project to build, and since it is essentially a hosted service running on a timer, it’s a perfect use case for Serverless.

Now you might be thinking, “um, wouldn’t it be easier to just check the weather?” Well, it would, but then I wouldn’t have an excuse to buy an expensive lightbulb or write an article with the word “Serverless.”

So let’s look at how you can build your own Weather Bulb. The final code is not complicated, but it does have some interesting pieces that are worth noting. By the way, did I mention that it’s Serverless?

Building the Weather Bulb

The first thing you are going to need is the bulb. You can’t have a Weather Bulb sans bulb. Say the word “bulb” out loud about 10 times and you’ll notice what a bizarre word it is. Bulb, bulb, bulb, bulb — see? Weird.

I am using the LIFX Mini Color. It’s not *too* expensive, but more importantly, it’s got an API that is wide open.

The API has two methods of authentication. The first contains the word “OAuth” and I’m already sorry that you had to read that. Don’t worry, there is an easier way that doesn’t involve OAu…. that which won’t be named.

The second way is to register an application with LIFX. You get back a key and all you have to do is pass that key with any HTTP request. That’s what I’m using for this demo.

For instance, if we wanted to change the bulb color to blue, we can just pass color: blue to the /state endpoint.

The API supports a few different color formats, including named colors (like red, blue), hex values, RBG, Kevlin, hue brightness and saturation. This is important because it factors into what proved to be the hardest part of this project: turning temperature into color.

Representing Temperature With Color

If you’ve ever watched a weather report, you’ll be familiar with the way that meteorology represents weather conditions with color on a map.

Usually, this is done to visualize precipitation. You have probably seen that ominous green strip of storms bearing down on you on a weather map while you try to figure out if you should get in the bathtub because you’re in the path of a tornado. Or maybe that’s just all of us unlucky souls here in America’s Tornado Alley.

Color is also used to represent temperature. This is precisely what I wanted to do with the bulb. The tough thing is that there doesn’t seem to be a standardized way to do this. Some maps show it as solid colors in bands. In this case, blue might represent the band from 0℉ – 32℉.

Others have it as a gradient scale which is more precise. This is what I was after for the Weather Bulb.

My first stab at solving this was just to Google “temperature color scale” and other various iterations of that search term. I got back a lot of information about Kelvin.

Kelvin is a representation of the temperature of a color. Literally. For any light source (light bulb, the sun, ect) the actual temperature of that source will affect the color of the light it emits. A fire burns a yellowish red color. The hotter that fire gets, the more it moves towards white. Hence the saying, “white hot”. So if someone ever says “red hot,” you can correct them in front of everyone because who doesn’t love a pedantic jerk?

The LIFX bulb supports Kelvin, so you might think that this would work. After all, this is the Kelvin scale….

The problem is that there is simply not enough color variation because these are not actual colors, but rather the tinge of color that a light is emitting based on it’s “temperature.” Here is the Kelvin color wheel that comes with the LIFX app.

These colors are barely distinguishable from one another on the bulb. Not exactly what I was after.

That leaves me with trying to convert the color to either Hex, RGB or some other format. This is tough because where do you begin? I spent an embarrassing amount of time adjust RGB scale values between blue for cold (0, 0, 255) and red for hot (255, 0, 0). It was about this time that it dawned on me that maybe HSL would be a better way to go here. Why? Because hue is a whole lot easier to understand.

Hue

Hue is a representation of color on a scale between 0 and 360. This is why we often see color represented on a wheel (360°). That’s a vast oversimplification, but unless you want me to start talking about wavelengths, let’s go with that definition.

The hue color wheel looks like this….

If we flatten it out, it’s easier to reason about.

We’re ready to convert temperature to color. The first thing we need to do is figure out a set temperature range. I went with 0℉ to 100℉. We can’t work with infinite temperature color combinations. Numbers go on forever, colors do not. It can only get so hot before our bulb is just bright red all the time, and that’s 100℉. The same is true for cold.

If light blue represents 0℉, I can start at about the 200 mark on the hue scale. Red will represent 100℉. You can see that red is at both extremes, so I can move either left OR right, depending on what colors I want to use to represent the temperature. It’s not the same as the colors they use in actual weather programs, but who cares? Obviously not me.

I chose to go right because there is no pink on the left and pink is my favorite color. I also felt like pink represents warm a bit better than green. Green is rain and tornadoes.

Now we can back into a hue based on temperature. Ready? Here we go.

Let’s pretend it’s a brisk 50℉ outside.

If 100℉ is the hottest we go (360) and 0℉ is the coldest (200), then we have a color scale of 160 points. To figure out where in that 160 point range we need to be, we can divide the current temperature by the upper bound of 100℉ which will give us the exact percentage we need to move in our range, or 50%. If we move 50% of the way into a 160 point range, that leaves us at 80. Since we are starting at 200, that gives us a hue of 280.

That sounds complicated, but only because word problems in math SUCK. Here’s how the code looks when it’s all said and done…

let hue = 200 + (160 * ( temperature / 100 ));

OK! We’ve got a dynamic color scale based on hue, and wouldn’t you know it, we can just pass the hue to LIFX as simply as we pass a named color.

Now we just need to find out what the current temperature is, back into a hue and do that every few minutes. Serverless, here we come!

Serverless Timer Functions

Serverless is all the rage. It’s like HTML5 used to be: it doesn’t matter what it is, it only matters that you know the word and are not afraid to use it in a blog post.

For this example, we’ll use Azure Functions because there is support for timer triggers, and we can test those timer triggers locally before we deploy using VS Code. One of the things about Serverless that irritates me to no end is when I can’t debug it locally.

Using the Azure Functions Core Tools and the Azure Functions Extension for VS Code, I can create a new Serverless project and select a Timer Trigger.

Timer Triggers in Azure Functions are specified as Cron Expressions. Don’t worry, I didn’t know what that was either.

Cron Expressions allow you to get very specific with interval definition. Cron breaks things down into second, minute, hour, day, month, year. So if you wanted to run something every second of every minute of every hour of every day of every year, your expression would look like this…

* * * * * *

If you wanted to run it every day at 10:15, it would look like this…

* 15 10 * * *

If you wanted to run it every 5 minutes (which is what Azure defaults to), you specify that by saying “when minutes is divisible by 5.”

0 */5 * * * *

For the purposes of this function, we set it to 2 minutes.

I am using a 2 minute interval because that’s how often we can call the weather API for free 💰.

Getting the Forecast From DarkSky

DarkSky has a wonderful weather API that you can call up to 1,000 times per day for free. If there are 1,440 minutes in a day (and there are), that means we can call DarkSky every 1.44 minutes per day and stay in the free zone. I just rounded up to 2 minutes because temperature doesn’t change that fast.

This is what our function looks like when we call the DarkSky API. All of my tokens, keys, latitude and longitude settings are in environment variables so they aren’t hardcoded. Those are set in the local.settings.json file. I used axios for my HTTP requests because it is a magical, magical package.

const axios = require('axios');

module.exports = function (context, myTimer) {
  // build up the DarkSky endpoint
  let endpoint = `${process.env.DS_API}/${process.env.DS_SECRET}/${process.env.LAT}, 
                  ${process.env.LNG}`;
  
  // use axios to call DarkSky for weather
  axios
    .get(endpoint)
    .then(response => {
      let temp = Math.round(response.data.currently.temperature);
    
      // TODO: Set the color of the LIFX bulb
    })
    .catch(err => {
      context.log(err.message);
    });
};

Now that I have the temperature, I need to call the LIFX API. And wouldn’t you know it, someone has already created an npm package to do this called lifx-http-api. This is why you love JavaScript.

Setting the Bulb Hue

After the weather result comes back, I need to use the LIFX API instance and call the setState method. This method returns a promise which means that we need to nest promises. Nesting promises can get out of hand and could land us right back in callback hell, which is what we’re trying to avoid with promises in the first place.

Instead, we’ll handle the first promise and then return Promise.all which we can handle at another top-level then. This just prevents us from nesting then statements.

Remember kids, promises are just socially acceptable callbacks.

const axios = require('axios');
const LIFX = require('lifx-http-api');

let client = new LIFX({
  bearerToken: process.env.LIFX_TOKEN
});

module.exports = function (context, myTimer) {
  // build up the DarkSky endpoint
  let endpoint = <code>${process.env.DS_API}/${process.env.DS_SECRET}/${
    process.env.LAT
    },${process.env.LNG}<code>;
    
  // use axios to call DarkSky for weather
  axios
    .get(endpoint)
    .then(response => {
      let temp = Math.round(response.data.currently.temperature);
      
      // make sure the temp isn't above 100 because that's as high as we can go
      temp = temp < 100 ? temp : 100;
      
      // determine the hue
      let hue = 200 + (160 * (temp / 100));

      // return Promise.all so we can resolve at the top level
      return Promise.all([
        data,
        client.setState('all', { color: <code>hue:${hue}<code> })
      ]);
    })
    .then(result => {
      // result[0] contains the darksky result
      // result[1] contains the LIFX result
      context.log(result[1]);
    })
    .catch(err => {
      context.log(err.message);
    });
};

Now we can run this thing locally and watch our timer do it’s thang.

That’s it! Let’s deploy it.

Deploying Weather Bulb

I can create a new Functions project from the VS Code extension.

I can right-click that to “Open in portal” where I can define a deployment source so it sucks my code in from Github and deploys it. This is ideal because now whenever I push a change to Github, my application automatically gets redeployed.

All Hail the Weather Bulb

Now just sit back and behold the soft glow of the Weather Bulb! Why look at the actual temperature when you can look at this beautiful shade of hot pink instead?

Can you guess what the temperature is based on what you know from this article? The person who leaves a comment and gets the closest will get a free LIFX lightbulb from me (because I ❤️ all of you), or the cost of the bulb if you are outside the U.S. (~$40).

You can grab all of the code for this project from Github.

The post Displaying the Weather With Serverless and Colors appeared first on CSS-Tricks.

Have you noticed these web design trends in 2018?

Post pobrano z: Have you noticed these web design trends in 2018?

As a space of ideas, fresh design and inspiration, the internet is a space which is constantly changing and evolving.  As designers add new elements of imagination each and every day, there is no wonder design is subject to shifts.

Web designers have grown with the increase in technology.  Sites are now more dynamic but still user-friendly and precise.

Each day designers create websites which fit in with business needs, adapt to new screen sizes and are aesthetic all at once.

In 2017, mobile browsing changed the face of web design.  More and more users switched from desktop browsing to tablets or mobile phones.

In 2018, there is a need for designers to stay relevant by adjusting their designs to a range of different screen sizes.  As a designer, you might be wondering which other trends are emerging for 2018, and how this will influence the work you are doing.

The striking impact of subtle shadows

Flat design has been trendy for a while now.  This trend removed all shadow from a site, creating a two-dimensional effect.  2017 saw the return of subtle shadows.

However, 2018 brings a range of new options.  As designers play with grids, shadows are being used more than ever.  These shadows evoke imager of a world that exists beyond the screen.

Shadows not only enhance the aesthetics of the page but also assist with directing user attention.  Shadows can be used to highlight a link or emphasize a call to action.

In 2018, shadows are being combined with vibrant colors and gradients in order to enhance the effect and create a striking impact.

A new vibrancy

2018 has seen the introduction of a range of vibrant new colors.  The subtle, safe colors of the past are being replaced with a saturated selection.

These colors have a dramatic impact.  Headers are no longer timid, and now give a shout out to viewers with cheerful hues and slashed designs.  Gone are the subdued sites as the internet starts to brighten up.

Brands who want to set themselves apart from traditional designs or corporate colors are now presented with an inspiring new palette to work with.

Particle backgrounds

As the design is shifting and changing towards images in motion, so are websites.  The particle effect enables designers to create an evolving background image which loads quickly.

Sites with great visual impact resonate with viewers, creating a memorable user experience.  Designers use images to strike an emotional chord.  A moving image has a much greater impact.

In addition, moving images can be used on social media sites, with a backlink to your site.

Design for mobile devices

2017 saw a massive increase in internet browsing via mobile devices.  These devices are highly personal and can be easily used while out of office or on the run.

As a result, designers have had to adjust, ensuring that navigation panels are effective and easy to use.  Imagery has to be adapted to a small screen as well.

This ensures that a carefully selected image has the impact that designers intended.  Sites are becoming simpler in order to adjust to small screens.

Broken grid layouts

Grids give the designer the ability to add content, images and even videos in an orderly format.  Grids bring a sense of harmony to designs.

In a quest for a new level of innovation, designers are now using broken grids to keep order without the constraint.  Broken grids allow designs to drift while keeping the flow.

Illustrations become predominant

Imagery is always evocative and resonates deeply with the viewer.  This is why a picture will always say a thousand words.  As 2018 starts to unfold, illustration is starting to take center stage.

Both GIFs and photographic imagery still have a role to play.  However, the unique very human touch that illustration brings is gaining new prominence.

Illustration creates a sense of nostalgia, taking us back to the world of 1960’s fashion.  As technology grows and advances at a rapid level, illustration provides a human touch to both our websites and our lives.

Organic shapes

Sharp edges and right angles have dominated website design for the past few years, giving a clear and modern feel.  However, in 2017, this started to change.

A new trend began to emerge, with big players such as Google, Twitter and Facebook adding a softer element to their designs.

However, it isn’t just site designs which are changing.  Large blobs of color are starting to give a new and very organic feel to background designs.

With the corresponding rise of illustration, this means that real-world imagery is now being given a playful and almost cartoonish element.

Superimposed images and duotones

As the internet begins to burst with color, it is time for designers to be more playful.  Google launched Material Design in 2014 and shifted the boundaries of internet design.

As vibrant colors become more popular web designers are also bringing in additional features such as dual toned backgrounds or superimposed images in order to create aesthetic appeal.

Contrasting colors such as red and green create both depth and interest.  Add gradients and photo saturation and the end result is intriguing.  Websites are becoming far more vibrant and multidimensional.

Innovative Typography Choices

In recent years, typography has focused on clarity and simplicity, but this is beginning to change.  Minimalism is being replaced by a sense of adventure and innovation.

Typography is becoming an art in its own right and is no longer willing to take a backseat in web design.  As a result, 2018 will bring fonts which are bolder, brighter and more innovative.

Think cutouts, misshapen words, transparent text and larger, bolder lettering.  Legibility is still key, so designers need to utilize a great deal more skill to achieve a cutting-edge effect.

Spacing is unusual in 2018.  This still requires the technical eye of a designer to prevent the results from being utterly chaotic.  Words are more widely spaced and this impact can be very effective when done well.

Alongside the recent growth in handmade fonts, typographers will be bringing new innovations into text this year.  The results will have a massive impact.

Subtle animations

With fonts becoming bigger and bolder and colors more vibrant, the animation is becoming more subtle.  Designers are saying hello to a range of micro-interactions which guide users on how to use the site.

Think of the button which shakes when you enter your password incorrectly or the text which unfolds to assist you with finding your way around a site.

These subtle animations delight viewers as they offer a deeper level of engagement.  However, they largely stay in the background, allowing vivid colors and bold typography to take center stage.  Balance is a large part of web design trends for 2018.

Summary

As 2018 unfolds, web design presents a brighter, bolder opportunity for designers to create a massive impact.  Sites are taking a more playful approach to design, with bright colors, bold text, and integrative animations.

The mobile phone era is changing the face of design, offering up the opportunity for brighter colors and simple imagery.  Use of illustration is making a comeback, adding a very human touch to a technological world.

As a web designer, 2018 is a time of innovation and change.  Working towards expressive sites and subtle effects allow designers to forge personality into brands or sites more easily than ever before.  The move away from minimalist sites and flat design can only be a welcome change.

Agregator najlepszych postów o designie, webdesignie, cssie i Internecie