简体   繁体   中英

Changing the menu color based on the hue of the slider image

Another update I found a way to get the RGB of the image using https://github.com/lokesh/color-thief,like @Ivan Arambula suggested.

now I am trying to compare the RGB of the color with the RGB of white and black then changing the menu color depending on the result I am using a code from an answer from @cyang answer but I have the following error

(index):240 Uncaught TypeError: Cannot read property 'substr' of undefined
    at getContrastYIQ ((index):240)
    at HTMLElement.<anonymous> ((index):247)
    at HTMLElement.dispatch (jquery-3.2.1.min.js:3)
    at HTMLElement.q.handle (jquery-3.2.1.min.js:3)
    at Object.trigger (jquery-3.2.1.min.js:4)
    at HTMLElement.<anonymous> (jquery-3.2.1.min.js:4)
    at Function.each (jquery-3.2.1.min.js:2)
    at r.fn.init.each (jquery-3.2.1.min.js:2)
    at r.fn.init.trigger (jquery-3.2.1.min.js:4)
    at $.Unslider.self.animate (unslider.js:439)

the JS code

<script type="text/javascript">

$(function() {
  var colorThief = new ColorThief();
  var slider = $('.my-slider').unslider();
  slider.on('unslider.change', function(event, index, slide) {
    image = slide.find('img')[0];
    color = colorThief.getColor(image,5);

    // color logic here
    function getContrastYIQ(color){
      var r = parseInt(color.substr(0,2),16);
      var g = parseInt(color.substr(2,2),16);
      var b = parseInt(color.substr(4,2),16);
      var yiq = ((r*299)+(g*587)+(b*114))/1000;
      return yiq ;
    }

    var colors = getContrastYIQ();
    if (colors >= 128) {
      $('.marker').css('color', 'black');
    }else {
      $('.marker').css('color', 'white');
    }
  })
});

</script>

my original question

I made a slider using A very simple jQuery slider. [ http://idiot.github.io/unslider/][1] . and I have a fixed hamburger menu, which I would like to change colors (white and blue) depending on the hue of the image, I have searched a lot and I couldn't find a solution, the libraries that I used till now are https://github.com/kennethcachia/background-check (with this one I succeed to change the color but only on scrolling not sliding and it doesn't switch to the other color). https://github.com/jamiebrittain/colourBrightness.js (no luck)
https://github.com/lokesh/color-thief (no luck)

If you could tell me the best technic or js library to do that it will be great

I am using laravel 5.4

My header code is

    <header>
      <div class="logo">
          <h3>Petronius 1926</h3>
      </div>
    </header>

    <!-- menu -->
      <button type="button" class="navbar-toggle js-navToggleButton is-open">
        <span class="icon-bar icon-bar--first"></span>
        <span class="icon-bar icon-bar--middle"></span>
        <span class="icon-bar icon-bar--second"></span>
        <span class="navbar-toggleTitle">Menu</span>
      </button>
      <span class="marker-left"><i class="fa fa-chevron-left" aria-hidden="true"></i></span>
      <span class="marker"><i class="fa fa-square" aria-hidden="true"></i></span>
      <span class="marker-right"><i class="fa fa-chevron-right" aria-hidden="true"></i></span>

      <nav class="pushy pushy-left">

        <div class="pushy-content">

          <div class="diamond">
            <button type="button" class="navbar-toggle navbar-toggle--innerNav js-navToggleButton close">
              <span class="icon-bar icon-bar--first"></span>
              <span class="icon-bar icon-bar--middle"></span>
              <span class="icon-bar icon-bar--second"></span>
            </button>
          </div>

          <span class="navbar-toggleTitle">Menu</span>
          <div class="marker-inner outer"><i class="fa fa-square" aria-hidden="true"></i></div>

          <ul>
              <li class="pushy-link"><a href="#home" class="navbar-nav-links-item-link">Home</a></li>
              <li class="pushy-link"><a href="#about-us" class="navbar-nav-links-item-link">Who we are</a></li>
              <li class="pushy-link"><a href="#story" class="navbar-nav-links-item-link">How is it made</a></li>
              <li class="pushy-link"><a href="#collection" class="navbar-nav-links-item-link">Collection</a></li>
              <li class="pushy-link"><a href="#contact" class="navbar-nav-links-item-link">Contact</a></li>
          </ul>
          <div class="marker-inner inverse"><i class="fa fa-square" aria-hidden="true"></i></div>

      </div>
    </nav>
    <div class="site-overlay"></div>

My home page code is 

    @extends('layouts.master')

    @section('styles')
    <!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> -->
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="/css/unslider.css">
    <link rel="stylesheet" href="/css/unslider-dots.css">
    @endsection

    @section('title')
      Home Page
    @endsection

    @section('content')
    <div id="fullpage" class="fullpage-wrapper">
      <section id="home" class="my-slider">
        <ul>
          @foreach( $gallery as $image )
            <li><img id="slider-image" src="{!! '/images/'.$image->image !!}" alt="Home page image slider"></li>
          @endforeach
        </ul>
      </section>

    </div>

    @endsection

    @section('scripts')
    <!-- Slider -->
    <script src="js/unslider.js"></script>
    <script src="js/script.js"></script>
  $(function() {
      var colorThief = new ColorThief();
      var slider = $('.my-slider').unslider();
      slider.on('unslider.change', function(event, index, slide) {
        image = slide.find('img')[0];
        color = colorThief.getColor(image,5);

        // color logic here
        function getContrastYIQ(color){
          var r = parseInt(color.substr(0,2),16);
          var g = parseInt(color.substr(2,2),16);
          var b = parseInt(color.substr(4,2),16);
          var yiq = ((r*299)+(g*587)+(b*114))/1000;
          return yiq ;
        }

        var colors = getContrastYIQ();
        if (colors >= 128) {
          $('.marker').css('color', 'black');
        }else {
          $('.marker').css('color', 'white');
        }


    </script>

    @endsection

Using Color Thief something like this might work.

$(function() {
  var colorThief = new ColorThief();
  var slider = $('.my-slider').unslider();
  slider.on('unslider.change', function(event, index, slide) {
    image = slide.find('img')[0];
    color = colorThief.getColor(image);

    // color logic here
  })
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM