Learning Objectives

In this tutorial, you’ll learn how to:

  • Change barcode background color
  • Customize the color of barcode bars/modules
  • Modify text color in barcodes
  • Set custom border colors
  • Apply different colors to caption text
  • Create visually distinctive barcodes for various use cases

Prerequisites

Before starting this tutorial, make sure you have:

  • An Aspose Cloud account (sign up for a free trial if needed)
  • Your Client ID and Client Secret from the Aspose Cloud Dashboard
  • Basic knowledge of REST APIs
  • Completed the basic barcode generation tutorial or have equivalent knowledge
  • A development environment for your preferred language (C#, Java, PHP, Python, Node.js, or Go)
  • Basic understanding of color values (RGB, hex codes)

Introduction

While traditional black-and-white barcodes are the most common, there are situations where colored barcodes can be beneficial for branding, categorization, or improved visual appeal. Aspose.BarCode Cloud API provides comprehensive options to customize the colors of various barcode elements.

In this tutorial, we’ll explore how to modify colors for:

  • Barcode bars/modules
  • Background
  • Text labels
  • Borders
  • Captions

Understanding Barcode Color Components

A barcode image consists of several elements, each of which can have its own color:

  1. Bars/Modules - The actual data-carrying elements of the barcode
  2. Background - The area behind the bars and text
  3. Text - The human-readable representation of the encoded data
  4. Border - The outline surrounding the barcode
  5. Captions - Additional text above or below the barcode

Step 1: Changing Background Color

The default background color is white, but you can customize it:

cURL Example for Background Color

curl -v "https://api.aspose.cloud/v3.0/barcode/green-background.png/generate?text=ASPOSE-CLOUD&type=Code128&format=png&backColor=Green" \
     -X PUT \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json"

Key parameter:

  • backColor=Green: Sets the background to green

Available color options:

  • Standard color names (Red, Green, Blue, Yellow, etc.)
  • Hex color codes (e.g., #FF5733)
  • RGB values (e.g., rgb(255,87,51))

Try it yourself:

  1. Generate barcodes with different background colors
  2. Test the scannability of each barcode to ensure the color combination works well

Step 2: Customizing Bar Color

By default, barcode bars are black, but you can change this:

cURL Example for Bar Color

curl -v "https://api.aspose.cloud/v3.0/barcode/blue-bars.png/generate?text=ASPOSE-CLOUD&type=Code128&format=png&barColor=Blue" \
     -X PUT \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json"

Key parameter:

  • barColor=Blue: Sets the bar color to blue

Important Note on Contrast

When customizing bar colors, ensure sufficient contrast between bars and background. Poor contrast can make barcodes difficult or impossible to scan.

Try it yourself:

  1. Create barcodes with different bar colors
  2. Test their scannability with various scanner applications

Step 3: Setting Text Color

Customize the color of the human-readable text:

cURL Example for Text Color

curl -v "https://api.aspose.cloud/v3.0/barcode/red-text.png/generate?text=ASPOSE-CLOUD&type=Code128&format=png&codeTextColor=Red" \
     -X PUT \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json"

Key parameter:

  • codeTextColor=Red: Sets the text color to red

Learning checkpoint:

  • What combinations of text, bar, and background colors would provide good readability?
  • Would very light text colors work well against a white background?

Step 4: Customizing Border Color

Change the color of the barcode border:

cURL Example for Border Color

curl -v "https://api.aspose.cloud/v3.0/barcode/purple-border.png/generate?text=ASPOSE-CLOUD&type=Code128&format=png&borderVisible=true&borderColor=Purple&borderWidth=3" \
     -X PUT \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json"

Key parameters:

  • borderVisible=true: Ensures the border is displayed
  • borderColor=Purple: Sets the border color to purple
  • borderWidth=3: Makes the border 3 pixels wide for better visibility

Step 5: Setting Caption Colors

Customize the colors of caption text:

cURL Example for Caption Color

curl -v "https://api.aspose.cloud/v3.0/barcode/orange-caption.png/generate?text=ASPOSE-CLOUD&type=Code128&format=png&captionAboveText=SCAN%20HERE&captionAboveVisible=true&captionAboveTextColor=Orange" \
     -X PUT \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json"

Key parameters:

  • captionAboveText=SCAN%20HERE: Sets the caption text
  • captionAboveVisible=true: Makes the caption visible
  • captionAboveTextColor=Orange: Sets the caption text color to orange

Step 6: Creating Color Schemes for Different Purposes

Different color combinations can serve various purposes:

Branding Color Scheme

curl -v "https://api.aspose.cloud/v3.0/barcode/branded-barcode.png/generate?text=ASPOSE-CLOUD&type=Code128&format=png&backColor=%23EAEAEA&barColor=%23203864&codeTextColor=%23203864&borderVisible=true&borderColor=%23203864" \
     -X PUT \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json"

This example uses a corporate color scheme with:

  • Light gray background (#EAEAEA)
  • Dark blue bars and text (#203864)
  • Matching border color

Category Color Scheme

curl -v "https://api.aspose.cloud/v3.0/barcode/category-barcode.png/generate?text=PRODUCT-A123&type=Code128&format=png&backColor=%23FFF9C4&barColor=%23FB8C00&captionAboveText=CATEGORY%3A%20PREMIUM&captionAboveVisible=true&captionAboveTextColor=%23FB8C00" \
     -X PUT \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json"

This example creates a visually distinctive category-based barcode using:

  • Light yellow background (#FFF9C4)
  • Orange bars (#FB8C00)
  • Orange caption indicating the product category

Step 7: Implementing Color Customization in C#

Let’s implement color customization in C#:

// Tutorial Code Example: Customize barcode colors in C#
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace AsposeBarcodeCloudTutorial
{
    class Program
    {
        // Replace with your actual credentials
        const string ClientId = "YOUR_CLIENT_ID";
        const string ClientSecret = "YOUR_CLIENT_SECRET";
        const string ApiBaseUrl = "https://api.aspose.cloud/v3.0/barcode/";
        const string AuthUrl = "https://api.aspose.cloud/oauth2/token";

        static async Task Main(string[] args)
        {
            // Get the access token
            var accessToken = await GetAccessToken();
            
            // Generate barcodes with different color schemes
            await GenerateBrandingBarcode(accessToken);
            await GenerateCategoryBarcodes(accessToken);
            
            Console.WriteLine("All colored barcodes generated successfully!");
        }

        static async Task<string> GetAccessToken()
        {
            using (var client = new HttpClient())
            {
                // Prepare the form data for token request
                var formContent = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("grant_type", "client_credentials"),
                    new KeyValuePair<string, string>("client_id", ClientId),
                    new KeyValuePair<string, string>("client_secret", ClientSecret)
                });

                // Make the token request
                var response = await client.PostAsync(AuthUrl, formContent);
                response.EnsureSuccessStatusCode();
                
                // Parse the JSON response
                var jsonResponse = await response.Content.ReadAsStringAsync();
                // Simple parsing - in production, use proper JSON parsing
                var accessToken = jsonResponse.Split('"')[3];
                
                return accessToken;
            }
        }

        static async Task GenerateBrandingBarcode(string accessToken)
        {
            using (var client = new HttpClient())
            {
                // Set authorization header
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                
                // Build the request URL with parameters for a branded barcode
                string fileName = "company-branded-barcode.png";
                string requestUrl = $"{ApiBaseUrl}{fileName}/generate?text=COMPANY-PRODUCT-123" +
                                   $"&type=Code128&format=png" +
                                   $"&backColor=%23E8F5E9" +  // Light green background
                                   $"&barColor=%23388E3C" +    // Dark green bars
                                   $"&codeTextColor=%23388E3C" + // Dark green text
                                   $"&borderVisible=true" +
                                   $"&borderColor=%23388E3C" + // Dark green border
                                   $"&captionAboveText=COMPANY%20NAME" +
                                   $"&captionAboveVisible=true" +
                                   $"&captionAboveTextColor=%23388E3C"; // Dark green caption
                
                // Make the PUT request to save to cloud
                var response = await client.PutAsync(requestUrl, null);
                response.EnsureSuccessStatusCode();
                
                Console.WriteLine("Generated company branding barcode");
            }
        }

        static async Task GenerateCategoryBarcodes(string accessToken)
        {
            // Create barcodes for different product categories with color coding
            var categories = new[]
            {
                new { Name = "Electronics", BgColor = "%23E3F2FD", BarColor = "%231565C0" }, // Blue theme
                new { Name = "Clothing", BgColor = "%23F3E5F5", BarColor = "%239C27B0" },    // Purple theme
                new { Name = "Grocery", BgColor = "%23E8F5E9", BarColor = "%232E7D32" }       // Green theme
            };
            
            using (var client = new HttpClient())
            {
                // Set authorization header
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                
                foreach (var category in categories)
                {
                    // Build the request URL with parameters
                    string fileName = $"category-{category.Name.ToLower()}-barcode.png";
                    string requestUrl = $"{ApiBaseUrl}{fileName}/generate?text={category.Name}-ITEM-123" +
                                      $"&type=Code128&format=png" +
                                      $"&backColor={category.BgColor}" +
                                      $"&barColor={category.BarColor}" +
                                      $"&codeTextColor={category.BarColor}" +
                                      $"&captionAboveText=Category%3A%20{category.Name}" +
                                      $"&captionAboveVisible=true" +
                                      $"&captionAboveTextColor={category.BarColor}";
                    
                    // Make the PUT request to save to cloud
                    var response = await client.PutAsync(requestUrl, null);
                    response.EnsureSuccessStatusCode();
                    
                    Console.WriteLine($"Generated {category.Name} category barcode");
                }
            }
        }
    }
}

Step 8: Implementing in Python

Let’s see how to implement color customization in Python:


import requests
import json

# Replace with your actual credentials
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
auth_url = "https://api.aspose.cloud/oauth2/token"
api_base_url = "https://api.aspose.cloud/v3.0/barcode/"

def get_access_token():
    """Get OAuth2 access token"""
    payload = {
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret
    }
    
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
    }
    
    response = requests.post(auth_url, data=payload, headers=headers)
    response.raise_for_status()
    
    return response.json()['access_token']

def generate_colored_barcode(access_token, file_name, params):
    """Generate barcode with custom color parameters"""
    # Build request URL with filename
    request_url = f"{api_base_url}generate"
    
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
    
    # Add filename to parameters
    params['outPath'] = file_name
    
    # Make POST request to generate barcode
    response = requests.post(request_url, json=params, headers=headers)
    response.raise_for_status()
    
    return response.json()

def download_barcode(access_token, file_name, local_path):
    """Download generated barcode from cloud storage"""
    request_url = f"{api_base_url}storage/file/{file_name}"
    
    headers = {
        'Authorization': f'Bearer {access_token}',
    }
    
    response = requests.get(request_url, headers=headers, stream=True)
    response.raise_for_status()
    
    with open(local_path, 'wb') as f:
        for chunk in response.iter_content(chunk_size=8192):
            f.write(chunk)
    
    return local_path

if __name__ == "__main__":
    # Get access token
    token = get_access_token()
    
    # Example 1: Basic color customization
    params_basic = {
        'barcodeType': 'Code128',
        'text': 'COLOR-DEMO',
        'format': 'png',
        'parameters': {
            'barcode': {
                'xDimension': 1.5,
                'barHeight': 50,
                'barColor': {
                    'r': 0,
                    'g': 0,
                    'b': 205
                }
            },
            'backColor': {
                'r': 173,
                'g': 216,
                'b': 230
            },
            'border': {
                'visible': True,
                'width': 1,
                'color': {
                    'r': 0,
                    'g': 0,
                    'b': 0
                }
            },
            'codeText': {
                'visible': True,
                'color': {
                    'r': 0,
                    'g': 0,
                    'b': 205
                }
            }
        }
    }
    
    result1 = generate_colored_barcode(token, "basic-color-demo.png", params_basic)
    print("Generated basic colored barcode")
    local_file1 = download_barcode(token, "basic-color-demo.png", "./basic-color-demo.png")
    print(f"Downloaded to {local_file1}")
    
    # Example 2: Branding colors
    # Convert hex colors to RGB
    brand_blue_rgb = {'r': 0, 'g': 71, 'b': 171}  # #0047AB
    light_gray_rgb = {'r': 240, 'g': 244, 'b': 248}  # #F0F4F8
    
    params_branding = {
        'barcodeType': 'Code128',
        'text': 'BRAND-BARCODE',
        'format': 'png',
        'parameters': {
            'barcode': {
                'xDimension': 1.5,
                'barHeight': 50,
                'barColor': brand_blue_rgb
            },
            'backColor': light_gray_rgb,
            'border': {
                'visible': True,
                'width': 2,
                'color': brand_blue_rgb
            },
            'codeText': {
                'visible': True,
                'color': brand_blue_rgb
            }
        }
    }
    
    result2 = generate_colored_barcode(token, "brand-color-demo.png", params_branding)
    print("Generated branding colored barcode")
    local_file2 = download_barcode(token, "brand-color-demo.png", "./brand-color-demo.png")
    print(f"Downloaded to {local_file2}")
    
    # Example 3: Category colors with hex color conversion
    def hex_to_rgb(hex_color):
        # Remove '#' if present
        hex_color = hex_color.lstrip('#')
        # Convert hex to RGB
        return {
            'r': int(hex_color[0:2], 16),
            'g': int(hex_color[2:4], 16),
            'b': int(hex_color[4:6], 16)
        }
    
    categories = [
        {"name": "Electronics", "bg": "#E3F2FD", "color": "#1565C0"},
        {"name": "Clothing", "bg": "#F3E5F5", "color": "#9C27B0"},
        {"name": "Food", "bg": "#E8F5E9", "color": "#2E7D32"}
    ]
    
    for idx, category in enumerate(categories):
        bg_rgb = hex_to_rgb(category['bg'])
        color_rgb = hex_to_rgb(category['color'])
        
        params_category = {
            'barcodeType': 'Code128',
            'text': f"{category['name']}-ITEM-123",
            'format': 'png',
            'parameters': {
                'barcode': {
                    'xDimension': 1.5,
                    'barHeight': 50,
                    'barColor': color_rgb
                },
                'backColor': bg_rgb,
                'codeText': {
                    'visible': True,
                    'color': color_rgb
                },
                'caption': {
                    'text': f"Category: {category['name']}",
                    'visible': True,
                    'alignment': 'Center',
                    'textColor': color_rgb,
                    'position': 'Above'
                }
            }
        }
        
        file_name = f"category-{category['name'].lower()}.png"
        result = generate_colored_barcode(token, file_name, params_category)
        print(f"Generated {category['name']} category barcode")
        local_file = download_barcode(token, file_name, f"./{file_name}")
        print(f"Downloaded to {local_file}")

Troubleshooting Common Issues

Issue: Colors not displaying as expected

  • Possible cause: Incorrect color format or unsupported color name
  • Solution: Use standard color names (e.g., “Red”, “Blue”) or hex codes (e.g., “#FF0000”) that are widely supported

Issue: Poor barcode scanning performance

  • Possible cause: Insufficient contrast between bar and background colors
  • Solution: Ensure high contrast between bars and background (dark bars on light background work best)

Issue: Color parameters not being applied

  • Possible cause: Parameter names may be incorrect
  • Solution: Double-check parameter names (e.g., barColor, not barcodeColor)

Best Practices for Colored Barcodes

  1. Maintain high contrast - Always ensure there’s substantial contrast between bars and background for optimal scanning
  2. Test thoroughly - Test colored barcodes with multiple scanners and in different lighting conditions
  3. Consider color blindness - Avoid color combinations that might be difficult for color-blind individuals to distinguish
  4. Limit color usage for critical applications - For mission-critical applications, stick to traditional black and white for maximum compatibility
  5. Use color for categorization - Color-code barcodes by category, department, or product line for easy visual identification

What You’ve Learned

Congratulations! In this tutorial, you’ve learned how to:

  • Customize the background color of barcodes
  • Change the color of barcode bars/modules
  • Modify the text color in barcodes
  • Set custom border colors
  • Apply different colors to caption text
  • Create visually distinctive barcodes for branding and categorization
  • Implement color customization programmatically in C# and Python

Further Practice

To reinforce your learning:

  1. Create a series of barcodes with different color schemes and test their scannability
  2. Design a color-coded system for an inventory management application
  3. Create branded barcodes that match your company’s color scheme
  4. Test the limits of contrast to determine what color combinations still scan reliably
  5. Compare how different scanners and mobile apps perform with colored barcodes

Next Steps

Now that you’ve mastered barcode color customization, you might want to explore generating multiple barcodes in batch operations:

Tutorial: Generate Multiple Barcodes

Helpful Resources

Have questions about this tutorial? Feel free to post them on our support forum!