Support

Account

Home Forums Front-end Issues Display color name with color picker

Solved

Display color name with color picker

  • Hello,
    i’m using color picker in a repetear field and I want to display to my users the “name of color” for each color in frontend.
    I find a JS for the name : http://chir.ag/projects/ntc/

    can you help me ?

    thank you.

  • Hi @mikealkeal

    The library you have linked is used for JS, so you need to get the name and apply it using JS too. Here’s an example how you can integrate PHP code and JS script:

    <script type="text/javascript">
    
      var n_match  = ntc.name("<?php the_field('color_field_name') ?>");
      n_rgb        = n_match[0]; // RGB value of closest match
      n_name       = n_match[1]; // Text string: Color name
      n_exactmatch = n_match[2]; // True if exact color match
    
      alert(n_match);
    
    </script>

    If you want to do it using PHP code, kindly check this thread: http://stackoverflow.com/questions/2993970/function-that-converts-hex-color-values-to-an-approximate-color-name.

    I hope this helps 🙂

  • hi
    Thank you !

    I’ll try this 🙂

  • I share my code for those who want

    PHP

     <?php if( have_rows('field_name_repeater') ): ?>
    <?php while( have_rows('field_name_repeater') ): the_row(); 
    $color = get_sub_field('fied_colorpicker');
    ?>
    
    <div class="color-repeat" data-color="<?php  echo $color; ?>">
    <div class="colorName"></div>
    </div>
    
    <?php endwhile; ?>
    <?php endif; ?>

    JS :

    $('.color-repeat').each(function( ) {
    	var color = $(this).data( "color" );
    	var n_match  = ntc.name(color);
    	n_name       = n_match[1]; // Text string: Color name
    	$(this).find(".colorName").text(n_name);
    });
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Display color name with color picker’ is closed to new replies.