How to make a simple Machine Learning Website from scratch

Pradyuman Dixit
HackerNoon.com
7 min readFeb 9, 2019

--

Learn to make a simple machine learning website which adapts text colour 🖌 according to changeable background contrast.

This might be a little bit turbulent for people who are very new to Machine Learning. If you’re new to Machine Learning, I’d suggest you to go through my this post here:

How to Understand Machine Learning with simple Code Examples

Are you enthusiastic about machine learning? Are you trying to implement a simple machine learning webpage from scratch?

Do you want to make something cool with HTML/CSS and JavaScript?

If yes, then you’re at the perfect place.

Machine Learning, AI

Here we will be making a simple webpage that changes the text colour according to the background color, using a simple machine learning library brain.js.

Let’s kick off our excitement with the result itself.

Here is the link to the webpage.

Go to the above link and try changing the RGB values of the slider, you’d notice the auto changing text colour with respect to the background contrast.

This is what we would be building here.

What? You didn’t open that link? You might miss something cool to see, but anyways here are the screenshots for you.

Green background, black text
Violet-ish background, white text

Didn’t quite get the feel from screenshots? Here is a GIF:

Colour-Learning GIF

Here is the full source code that you may want to see.

Let’s get into some code.

First off, we would make a simple html file, where we would structure our webpage. We would also be needing simple sliders for R, G, B values.

So the code for index.html would look something like this:

Here we’re also using the script brain.js which is a machine learning JavaScript library.

Now let’s design our webpage, that lets it change colour on movement of the slider. Yes, welcome CSS to your project.

The code for style.css would look something like this:

Yes, we’ve done it. Now go to your webpage and see how the slider changes the colours.

What? The text colour is not changing and neither is the background?

Well, don’t worry, it’s because we have not yet added the machine learning code that would train our neural network and make it understand which colour to change the text to.

I have tried to comment on the blocks of the code, so it would be more easier to understand.

Also we’re yet to add the code to change the background colour. So let’s first get done with that. The code will look something like this:

So let’s get into some machine learning code with JavaScript.

This above code creates objects for our neural network and also put the light or dark label that would be our labels, to change the text colour.

Now let’s get into training our neural network with data, and of course code. The code would look something like this:

Please refer to the comments I have provided, it would help you understand this big but straight forward piece of code, better.

And now we should add the code that would work the wonders in our machine learning code. This piece of code helps to change the text colour according to need and the training.

And yes, that’s it.

Take a look at your webpage and auto changing text colour according to the contrast, using machine learning.

If you’ve some difficulties in the code above, please take a look at the source code here.

You’ve successfully built yourself a machine learning website using simple JavaScript, HTML and CSS.

What are you waiting for?

Go and Show Off!!

In case we’re meeting for the first time here, I am Pradyuman Dixit and I mostly write about Machine learning, Android Development and sometimes about Web Development.

You can read my other Machine Learning posts here:

How to Understand Machine Learning with simple Code Examples

How to make a Machine Learning Android Game as a beginner

--

--