# ColorPicker (Javadocs: controlP5)
Date: 2014-04-27

controlP5

## Class ColorPicker

- java.lang.Object
- - controlP5.ControllerGroup<T>
  - - controlP5.ControlGroup<ColorPicker>
    - - controlP5.ColorPicker

All Implemented Interfaces:
  ControllerInterface<ColorPicker>, ControlListener, ControlP5Constants

    public class ColorPicker
    extends ControlGroup<ColorPicker>

A simple color picker using sliders to adjust RGBA values.

+Example
  `

      /**
       * ControlP5 Color picker. a simple color picker,
       * 4 horizontal sliders controlling the RGBA channels of a color.
       * to grab the current color value, use function getColorValue() of
       * the color picker.
       *
       * find a list of public methods available for the ColorPicker Controller
       * at the bottom of this sketch's source code
       *
       * by Andreas Schlegel, 2012
       * www.sojamo.de/libraries/controlP5
       *
       */
      import controlP5.*;

      ControlP5 cp5;

      ColorPicker cp;

      void setup() {
        size(400, 600);
        noStroke();
        cp5 = new ControlP5(this);
        cp = cp5.addColorPicker("picker")
                .setPosition(60, 100)
                .setColorValue(color(255, 128, 0, 128))
                ;
      }

      void draw() {
        background(cp.getColorValue());
        fill(0, 80);
        rect(50, 90, 275, 80);
      }

      public void controlEvent(ControlEvent c) {
        // when a value change from a ColorPicker is received, extract the ARGB values
        // from the controller's array value
        if(c.isFrom(cp)) {
          int r = int(c.getArrayValue(0));
          int g = int(c.getArrayValue(1));
          int b = int(c.getArrayValue(2));
          int a = int(c.getArrayValue(3));
          color col = color(r,g,b,a);
          println("event\talpha:"+a+"\tred:"+r+"\tgreen:"+g+"\tblue:"+b+"\tcol"+col);
        }
      }

      // color information from ColorPicker 'picker' are forwarded to the picker(int) function
      void picker(int col) {
        println("picker\talpha:"+alpha(col)+"\tred:"+red(col)+"\tgreen:"+green(col)+"\tblue:"+blue(col)+"\tcol"+col);
      }

      void keyPressed() {
        switch(key) {
          case('1'):
          // method A to change color
          cp.setArrayValue(new float[] {120, 0, 120, 255});
          break;
          case('2'):
          // method B to change color
          cp.setColorValue(color(255, 0, 0, 255));
          break;
        }
      }

      /*
       a list of all methods available for the ColorPicker Controller
       use ControlP5.printPublicMethodsFor(ColorPicker.class);
       to print the following list into the console.

       You can find further details about class ColorPicker in the javadoc.

       Format:
       ClassName : returnType methodName(parameter type)

       controlP5.ColorPicker : ColorPicker setArrayValue(float[])
       controlP5.ColorPicker : ColorPicker setColorValue(int)
       controlP5.ColorPicker : String getInfo()
       controlP5.ColorPicker : int getColorValue()
       controlP5.ControlGroup : ColorPicker activateEvent(boolean)
       controlP5.ControlGroup : ColorPicker addListener(ControlListener)
       controlP5.ControlGroup : ColorPicker hideBar()
       controlP5.ControlGroup : ColorPicker removeListener(ControlListener)
       controlP5.ControlGroup : ColorPicker setBackgroundColor(int)
       controlP5.ControlGroup : ColorPicker setBackgroundHeight(int)
       controlP5.ControlGroup : ColorPicker setBarHeight(int)
       controlP5.ControlGroup : ColorPicker showBar()
       controlP5.ControlGroup : ColorPicker updateInternalEvents(PApplet)
       controlP5.ControlGroup : String getInfo()
       controlP5.ControlGroup : String toString()
       controlP5.ControlGroup : boolean isBarVisible()
       controlP5.ControlGroup : int getBackgroundHeight()
       controlP5.ControlGroup : int getBarHeight()
       controlP5.ControlGroup : int listenerSize()
       controlP5.ControllerGroup : CColor getColor()
       controlP5.ControllerGroup : ColorPicker add(ControllerInterface)
       controlP5.ControllerGroup : ColorPicker bringToFront()
       controlP5.ControllerGroup : ColorPicker bringToFront(ControllerInterface)
       controlP5.ControllerGroup : ColorPicker close()
       controlP5.ControllerGroup : ColorPicker disableCollapse()
       controlP5.ControllerGroup : ColorPicker enableCollapse()
       controlP5.ControllerGroup : ColorPicker hide()
       controlP5.ControllerGroup : ColorPicker moveTo(ControlWindow)
       controlP5.ControllerGroup : ColorPicker moveTo(PApplet)
       controlP5.ControllerGroup : ColorPicker open()
       controlP5.ControllerGroup : ColorPicker registerProperty(String)
       controlP5.ControllerGroup : ColorPicker registerProperty(String, String)
       controlP5.ControllerGroup : ColorPicker remove(CDrawable)
       controlP5.ControllerGroup : ColorPicker remove(ControllerInterface)
       controlP5.ControllerGroup : ColorPicker removeCanvas(ControlWindowCanvas)
       controlP5.ControllerGroup : ColorPicker removeProperty(String)
       controlP5.ControllerGroup : ColorPicker removeProperty(String, String)
       controlP5.ControllerGroup : ColorPicker setAddress(String)
       controlP5.ControllerGroup : ColorPicker setArrayValue(float[])
       controlP5.ControllerGroup : ColorPicker setColor(CColor)
       controlP5.ControllerGroup : ColorPicker setColorActive(int)
       controlP5.ControllerGroup : ColorPicker setColorBackground(int)
       controlP5.ControllerGroup : ColorPicker setColorForeground(int)
       controlP5.ControllerGroup : ColorPicker setColorLabel(int)
       controlP5.ControllerGroup : ColorPicker setColorValue(int)
       controlP5.ControllerGroup : ColorPicker setHeight(int)
       controlP5.ControllerGroup : ColorPicker setId(int)
       controlP5.ControllerGroup : ColorPicker setLabel(String)
       controlP5.ControllerGroup : ColorPicker setMouseOver(boolean)
       controlP5.ControllerGroup : ColorPicker setMoveable(boolean)
       controlP5.ControllerGroup : ColorPicker setOpen(boolean)
       controlP5.ControllerGroup : ColorPicker setPosition(PVector)
       controlP5.ControllerGroup : ColorPicker setPosition(float, float)
       controlP5.ControllerGroup : ColorPicker setStringValue(String)
       controlP5.ControllerGroup : ColorPicker setUpdate(boolean)
       controlP5.ControllerGroup : ColorPicker setValue(float)
       controlP5.ControllerGroup : ColorPicker setVisible(boolean)
       controlP5.ControllerGroup : ColorPicker setWidth(int)
       controlP5.ControllerGroup : ColorPicker show()
       controlP5.ControllerGroup : ColorPicker update()
       controlP5.ControllerGroup : ColorPicker updateAbsolutePosition()
       controlP5.ControllerGroup : ControlWindow getWindow()
       controlP5.ControllerGroup : ControlWindowCanvas addCanvas(ControlWindowCanvas)
       controlP5.ControllerGroup : Controller getController(String)
       controlP5.ControllerGroup : ControllerProperty getProperty(String)
       controlP5.ControllerGroup : ControllerProperty getProperty(String, String)
       controlP5.ControllerGroup : Label getCaptionLabel()
       controlP5.ControllerGroup : Label getValueLabel()
       controlP5.ControllerGroup : PVector getPosition()
       controlP5.ControllerGroup : String getAddress()
       controlP5.ControllerGroup : String getInfo()
       controlP5.ControllerGroup : String getName()
       controlP5.ControllerGroup : String getStringValue()
       controlP5.ControllerGroup : String toString()
       controlP5.ControllerGroup : Tab getTab()
       controlP5.ControllerGroup : boolean isCollapse()
       controlP5.ControllerGroup : boolean isMouseOver()
       controlP5.ControllerGroup : boolean isMoveable()
       controlP5.ControllerGroup : boolean isOpen()
       controlP5.ControllerGroup : boolean isUpdate()
       controlP5.ControllerGroup : boolean isVisible()
       controlP5.ControllerGroup : boolean setMousePressed(boolean)
       controlP5.ControllerGroup : float getValue()
       controlP5.ControllerGroup : float[] getArrayValue()
       controlP5.ControllerGroup : int getHeight()
       controlP5.ControllerGroup : int getId()
       controlP5.ControllerGroup : int getWidth()
       controlP5.ControllerGroup : void remove()
       java.lang.Object : String toString()
       java.lang.Object : boolean equals(Object)

       */

  `

### Field Summary

### Fields inherited from interface controlP5.ControlP5Constants

`acceptClassList, ACTION_BROADCAST, ACTION_CLICK, ACTION_DRAG, ACTION_ENTER, ACTION_EXIT, ACTION_LEAVE, ACTION_MOVE, ACTION_PRESS, ACTION_PRESSED, ACTION_RELEASE, ACTION_RELEASED, ACTION_RELEASEDOUTSIDE, ACTION_WHEEL, ACTIVE, ALL, ALT, AQUA, ARC, ARRAY, BACKSPACE, BASELINE, BITFONT, BLACK, BLUE, BOOLEAN, BOTTOM, BOTTOM_OUTSIDE, CAPTIONLABEL, CENTER, CHECKBOX, COMMANDKEY, CONTROL, controlEventClass, CUSTOM, DECREASE, DEFAULT, DELETE, DONE, DOWN, DROPDOWN, ELLIPSE, ENTER, ESCAPE, EVENT, eventMethod, FADEIN, FADEOUT, FIELD, FLOAT, FUCHSIA, GRAY, GREEN, grixel, HALF_PI, HIDE, HIGHLIGHT, HORIZONTAL, IDLE, IMAGE, INACTIVE, INCREASE, INTEGER, INVALID, J2D, KEYCONTROL, LEFT, LEFT_OUTSIDE, LIME, LINE, LIST, LOAD, MAROON, MENU, METHOD, MOVE, MULTI, MULTIPLES, NAVY, OLIVE, ORANGE, OVER, P2D, P3D, PI, PRESSED, PRINT, PURPLE, RED, RELEASE, RESET, RIGHT, RIGHT_OUTSIDE, SAVE, SHIFT, SILVER, SINGLE, SINGLE_COLUMN, SINGLE_ROW, SPRITE, standard56, standard58, STRING, SWITCH, SWITCH_BACK, SWITCH_FORE, synt24, TAB, TEAL, THEME_A, THEME_CP52014, THEME_CP5BLUE, THEME_RED, THEME_RETRO, THEME_WHITE, TOP, TOP_OUTSIDE, TRANSITION_WAIT_FADEIN, TREE, TWO_PI, UP, VALUELABEL, VERBOSE, VERTICAL, WAIT, WHITE, YELLOW`

### Constructor Summary

Constructors
Constructor and Description
`ColorPicker(ControlP5 theControlP5, java.lang.String theName)` Convenience constructor to extend ColorPicker.

### Method Summary

Methods
`void` `controlEvent(ControlEvent theEvent)` controlEvent is called by controlP5's ControlBroadcaster to inform available listeners about value changes.
`int` `getColorValue()`
`java.lang.String` `getInfo()`
`ColorPicker` `plugTo(java.lang.Object theObject)`
`ColorPicker` `plugTo(java.lang.Object theObject, java.lang.String thePlugName)`
`ColorPicker` `setArrayValue(float[] theArray)` Requires an array of size 4 for RGBA
`ColorPicker` `setColorValue(int theColor)`

### Methods inherited from class controlP5.ControlGroup

`activateEvent, addListener, getBackgroundHeight, getBarHeight, listenerSize, mousePressed, removeListener, setBackgroundColor, setBackgroundHeight, setBarHeight, setSize, stringValue, toString, updateInternalEvents`

### Methods inherited from class controlP5.ControllerGroup

`absolutePosition, add, addCanvas, addCloseButton, addDrawable, arrayValue, bringToFront, bringToFront, close, color, continuousUpdateEvents, controller, disableCollapse, draw, enableCollapse, getAbsolutePosition, getAddress, getArrayValue, getArrayValue, getCaptionLabel, getColor, getController, getHeight, getId, getName, getParent, getPickingColor, getPosition, getProperty, getProperty, getStringValue, getTab, getValue, getValueLabel, getWidth, getWindow, hide, hideArrow, hideBar, id, init, isBarVisible, isCollapse, isMouseOver, isMoveable, isOpen, isUpdate, isVisible, keyEvent, moveTo, moveTo, moveTo, moveTo, moveTo, moveTo, moveTo, moveTo, moveTo, name, open, parent, position, registerProperty, registerProperty, remove, remove, remove, removeCanvas, removeCloseButton, removeProperty, removeProperty, setAbsolutePosition, setAddress, setArrayValue, setCaptionLabel, setColor, setColorActive, setColorBackground, setColorForeground, setColorLabel, setGroup, setGroup, setHeight, setId, setLabel, setMouseOver, setMousePressed, setMoveable, setOpen, setPosition, setPosition, setStringValue, setTab, setTab, setTab, setTitle, setUpdate, setValue, setVisible, setWidth, show, showArrow, showBar, update, updateAbsolutePosition, updateEvents, value`

### Methods inherited from class java.lang.Object

`equals, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Constructor Detail

#### ColorPicker

    public ColorPicker(ControlP5 theControlP5,
               java.lang.String theName)

Convenience constructor to extend ColorPicker.

Parameters:
  `theControlP5` -
  `theName` -

+Example
  `

      /**
       * ControlP5 extending Controllers
       *
       * the following example shows how to extend the Controller class to
       * create customizable Controllers. You can either extend the Controller class itself,
       * or any class that extends Controller itself like the Slider, Button, DropdownList, etc.
       *
       * How to:
       *
       * 1) do a super call to the convenience constructor requiring
       * 2 parameter (ControlP5 instance, name)
       *
       * 2) the Controller class has a set of empty methods that allow you to capture
       * inputs from the mouse including
       * onEnter(), onLeave(), onPress(), onRelease(), onClick(), onScroll(int), onDrag()
       * These you can override and include functionality as needed.
       *
       * 3) use method getPointer() to return the local (relative)
       * xy-coordinates of the controller
       *
       * 4) after instantiation custom controllers are treated the same
       * as default controlP5 controllers.
       *
       * by Andreas Schlegel, 2012
       * www.sojamo.de/libraries/controlp5
       *
       */

      import controlP5.*;

      ControlP5 cp5;
      PApplet p;

      void setup() {
        size(400, 400);
        cp5 = new ControlP5(this);

        // create 2 groups to show nesting of custom controllers and
        //
        Group g1 = cp5.addGroup("a").setPosition(0,100).setWidth(180);
        Group g2 = cp5.addGroup("b").setPosition(0,10).setWidth(180);
        g2.moveTo(g1);

        // create 2 custom Controllers from class MyButton
        // MyButton extends Controller and inherits all methods accordingly.
        new MyButton(cp5, "b1").setPosition(0, 0).setSize(180, 200).moveTo(g2);
        new MyButton(cp5, "b2").setPosition(205, 15).setSize(180, 200);

      }

      void draw() {
        background(0);
      }

      // b1 will be called from Controller b1
      public void b1(float theValue) {
        println("yay button "+theValue);
      }

      public void controlEvent(ControlEvent theEvent) {
        println("controlEvent : "+theEvent);
      }

      // Create a custom Controller, please not that
      // MyButton extends Controller<MyButton>, <MyButton>
      // is an indicator for the super class about the type of
      // custom controller to be created.

      class MyButton extends Controller<MyButton> {

        int current = 0xffff0000;

        float a = 128;

        float na;

        int y;

        // use the convenience constructor of super class Controller
        // MyButton will automatically registered and move to the
        // default controlP5 tab.

        MyButton(ControlP5 cp5, String theName) {
          super(cp5, theName);

          // replace the default view with a custom view.
          setView(new ControllerView() {
            public void display(PApplet p, Object b) {
              // draw button background
              na += (a-na) * 0.1;
              p.fill(current,na);
              p.rect(0, 0, getWidth(), getHeight());

              // draw horizontal line which can be moved on the x-axis
              // using the scroll wheel.
              p.fill(0,255,0);
              p.rect(0,y,width,10);

              // draw the custom label
              p.fill(128);
              translate(0,getHeight()+14);
              p.text(getName(),0,0);
              p.text(getName(),0,0);

            }
          }
          );
        }

        // override various input methods for mouse input control
        void onEnter() {
          cursor(HAND);
          println("enter");
          a = 255;
        }

        void onScroll(int n) {
          println("scrolling");
          y -= n;
          y = constrain(y,0,getHeight()-10);
        }

        void onPress() {
          println("press");
          current = 0xffffff00;
        }

        void onClick() {
          Pointer p1 = getPointer();
          println("clicked at "+p1.x()+", "+p1.y());
          current = 0xffffff00;
          setValue(y);
        }

        void onRelease() {
          println("release");
          current = 0xffffffff;
        }

        void onMove() {
          println("moving "+this+" "+_myControlWindow.getMouseOverList());
        }

        void onDrag() {
          current = 0xff0000ff;
          Pointer p1 = getPointer();
          float dif = dist(p1.px(),p1.py(),p1.x(),p1.y());
          println("dragging at "+p1.x()+", "+p1.y()+" "+dif);
        }

        void onReleaseOutside() {
          onLeave();
        }

        void onLeave() {
          println("leave");
          cursor(ARROW);
          a = 128;
        }
      }

  `

### Method Detail

#### controlEvent

    public void controlEvent(ControlEvent theEvent)

Description copied from interface: `ControlListener`
controlEvent is called by controlP5's ControlBroadcaster to inform available listeners about value changes. Use the CallbackListener to get informed when actions such as pressed, release, drag, etc are performed.

Specified by:
  `controlEvent` in interface `ControlListener`
Overrides:
  `controlEvent` in class `ControlGroup<ColorPicker>`
Parameters:
  `theEvent` - ControlEvent
See Also:
  `CallbackListener`, `CallbackEvent`

#### getColorValue

    public int getColorValue()

#### getInfo

    public java.lang.String getInfo()

Description copied from class: `ControlGroup`

Overrides:
  `getInfo` in class `ControlGroup<ColorPicker>`

#### plugTo

    public ColorPicker plugTo(java.lang.Object theObject)

#### plugTo

    public ColorPicker plugTo(java.lang.Object theObject,
                     java.lang.String thePlugName)

#### setArrayValue

    public ColorPicker setArrayValue(float[] theArray)

Requires an array of size 4 for RGBA

Specified by:
  `setArrayValue` in interface `ControllerInterface<ColorPicker>`
Overrides:
  `setArrayValue` in class `ControllerGroup<ColorPicker>`
Returns:
  ColorPicker

#### setColorValue

    public ColorPicker setColorValue(int theColor)

Specified by:
  `setColorValue` in interface `ControllerInterface<ColorPicker>`
Overrides:
  `setColorValue` in class `ControllerGroup<ColorPicker>`