How to create a trackbar as the RGB color palette using OpenCV Python?

In OpenCV, a trackbar can be created using cv2.createTrackbar() function. To access the value of the selected trackbar position, we use cv2.getTrackbarPos() function.

Using these two functions, we create a window that contains the trackbars for R, G, B colors and a color window to display the selected color. By changing the position of trackbars RGB colors change between 0 and 255.

Syntax

cv2.createTrackbar(trackbar_name, window_name, default_value, max_value, callback_func)
cv2.getTrackbarPos(trackbar_name, window_name)

Parameters

  • trackbar_name ? It's the trackbar name. This name is used to access the trackbar position value.

  • window_name ? It is the name of the window to which the trackbar is attached.

  • default_value ? default value set for the trackbar.

  • max_value ? maximum value for the trackbar.

  • callback_func ? function executed when trackbar value changes.

Creating a Simple RGB Color Palette

To create a trackbar as RGB color palette, we need to create three trackbars for Red, Green, and Blue values and update the display in real?time based on their positions.

import cv2
import numpy as np

def nothing(x):
    pass
    
# Create a black image and window
img = np.zeros((300, 650, 3), np.uint8)
window_name = 'Trackbar Color Palette'
cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)

# Create trackbars for color change
cv2.createTrackbar('R', window_name, 0, 255, nothing)
cv2.createTrackbar('G', window_name, 0, 255, nothing)
cv2.createTrackbar('B', window_name, 0, 255, nothing)

while True:
    cv2.imshow(window_name, img)
    k = cv2.waitKey(1) & 0xFF
    if k == ord('q'):
        break
        
    # Get current positions of trackbars
    r = cv2.getTrackbarPos('R', window_name)
    g = cv2.getTrackbarPos('G', window_name)
    b = cv2.getTrackbarPos('B', window_name)
    
    # Update image with BGR values (OpenCV uses BGR format)
    img[:] = [b, g, r]
    
cv2.destroyAllWindows()

When you run this program, you get a window with three trackbars for Red, Green, and Blue. The window displays the resulting color as you slide the trackbars. Press 'q' to exit the program.

RGB Color Palette with Switch Control

This enhanced version includes a switch button to control when colors are displayed ?

import cv2
import numpy as np

def nothing(x):
    pass
    
# Create a black image and window
img = np.zeros((300, 650, 3), np.uint8)
window_name = 'Trackbar Color Palette'
cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)

# Create trackbars for color change
cv2.createTrackbar('R', window_name, 0, 255, nothing)
cv2.createTrackbar('G', window_name, 0, 255, nothing)
cv2.createTrackbar('B', window_name, 0, 255, nothing)

# Create switch for ON/OFF functionality
cv2.createTrackbar("Switch", window_name, 0, 1, nothing)

while True:
    cv2.imshow(window_name, img)
    key = cv2.waitKey(1) & 0xFF
    if key == ord('q'):
        break
        
    # Get current positions of trackbars
    r = cv2.getTrackbarPos('R', window_name)
    g = cv2.getTrackbarPos('G', window_name)
    b = cv2.getTrackbarPos('B', window_name)
    s = cv2.getTrackbarPos("Switch", window_name)
    
    # Display color only when switch is ON
    if s == 0:
        img[:] = 0  # Black when switch is OFF
    else:
        img[:] = [b, g, r]  # Show RGB color when switch is ON
        
cv2.destroyAllWindows()

This version adds a switch trackbar that controls whether colors are displayed. When the switch is OFF (0), the window remains black regardless of RGB values. When the switch is ON (1), the selected RGB color is displayed.

How It Works

The trackbar color palette works by:

  • Creating trackbars with values ranging from 0 to 255 for each RGB component

  • Reading trackbar positions continuously in the main loop

  • Updating the image array with BGR values (OpenCV's color format)

  • Displaying the updated image in real?time

Conclusion

OpenCV trackbars provide an interactive way to create RGB color palettes. Use cv2.createTrackbar() to create sliders and cv2.getTrackbarPos() to read their values for real?time color manipulation.

Updated on: 2026-03-26T21:58:12+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements