Giter Club home page Giter Club logo

machine-learning-color-sample's Introduction

Brain.js 類神經網路 JS應用

這個應用是因為看到youtube上有人分享了很酷的視頻,決定跟著做做看,非原創 reference1 brain.js

基本式

透過建立一個新NeuralNetwork後給予訓練的data,在這邊的例子是利用顏色rgb color來當作訓練的data 如果顏色是深色,我們的output就會寫dark:1,如果我們的顏色是亮色的,output就會寫light: 1 透過訓練模型讓input的資料透過這些data去得到dark的機率及light的機率,透過brain.likely得到機率最高的那個值

e.g. dark: 0.988888, light: 0.211111,最後會得到dark的值

所以這個範例是透過得到背景是light或是dark來決定Text color

const network = new brain.NeuralNetwork()

network.train([
    { input: { r: 0.62, g: 0.72, b: 0.88 }, output: { light: 1 } },
    { input: { r: 0.1, g: 0.84, b: 0.72 }, output: { light: 1 } },
    { input: { r: 0.33, g: 0.24, b: 0.29 }, output: { dark: 1 } },
    { input: { r: 0.74, g: 0.78, b: 0.86 }, output: { light: 1 } },
    { input: { r: 0.31, g: 0.35, b: 0.41 }, output: { dark: 1 } },
    { input: {r: 1, g: 0.99, b: 0}, output: { light: 1 } },
    { input: {r: 1, g: 0.42, b: 0.52}, output: { dark: 1 } },
  ])

const result = brain.likely(rgb, network)

完整範例

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Color Brain Test</title>
  <link rel="stylesheet" href="/css/style.css">
  <script src="https://unpkg.com/[email protected]/browser.min.js"></script>
  <!-- <script src="./js/color.js" charset="utf-8"></script> -->
</head>
<body>
  <div class="box">
    <input type="color" value="#ff0000"/>
    <div id="example">Text color will change when background color turn bright</div>
  </div>
    <script src="./js/color.js" charset="utf-8"></script>
</body>
</html>
const input = document.querySelector("input")
const example = document.querySelector("#example")

input.addEventListener("change", (e) => {
  const rgb = getRgb(e.target.value);
  const network = new brain.NeuralNetwork()
  console.log(rgb);
  network.train([
    { input: { r: 0.62, g: 0.72, b: 0.88 }, output: { light: 1 } },
    { input: { r: 0.1, g: 0.84, b: 0.72 }, output: { light: 1 } },
    { input: { r: 0.33, g: 0.24, b: 0.29 }, output: { dark: 1 } },
    { input: { r: 0.74, g: 0.78, b: 0.86 }, output: { light: 1 } },
    { input: { r: 0.31, g: 0.35, b: 0.41 }, output: { dark: 1 } },
    { input: {r: 1, g: 0.99, b: 0}, output: { light: 1 } },
    { input: {r: 1, g: 0.42, b: 0.52}, output: { dark: 1 } },
  ])

  const result = brain.likely(rgb, network)
  console.log(result);
  example.style.background = e.target.value
  example.style.color = result === "dark" ? "white" : "black"
})

// color get function learn from LearnCode.academy
//https://www.youtube.com/watch?v=9Hz3P1VgLz4&t=3s
function getRgb(hex) {
  var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  hex = hex.replace(shorthandRegex, function(m, r, g, b) {
      return r + r + g + g + b + b;
      // console.log(r + r + g + g + b + b)
  });

  var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result ? {
      r:
    Math.round(parseInt(result[1], 16)/ 2.55) / 100,
      g: Math.round(parseInt(result[2], 16) / 2.55) / 100,
      b: Math.round(parseInt(result[3], 16) / 2.55) / 100,
  } : null;
}

machine-learning-color-sample's People

Contributors

alec0123321 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.