Skip to sidebar Skip to content
Ipercube Technical
Jack of all trades, master of none
Date: Time: - Stardate:
Welcome, Log in by clicking  Here!
SS64
MDN
Stack
Code
GitHub
Wiki
Convert
V Studio
  • Home
  • VB Functions
    • Access Functions
    • Assembly Info
    • Color/Image Functions
    • Data Table Functions
    • Email Functions
    • Active Directory Functions
    • LDAP Functions
    • Excel Functions
    • Logging Functions
    • Source Code Retrieval
    • String Functions
    • Sql Functions
    • Object Utilities Functions
    • System and Utilities
    • Utility Forms
  • VB Classes
    • Environment Variables Helper
  • ABAP
    • ABAP Reports
    • ABAP HTTP Functions
Ipercube Technical
  • Home
  • VB Functions
    • Access Functions
    • Assembly Info
    • Color/Image Functions
    • Data Table Functions
    • Email Functions
    • Active Directory Functions
    • LDAP Functions
    • Excel Functions
    • Logging Functions
    • Source Code Retrieval
    • String Functions
    • Sql Functions
    • Object Utilities Functions
    • System and Utilities
    • Utility Forms
  • VB Classes
    • Environment Variables Helper
  • ABAP
    • ABAP Reports
    • ABAP HTTP Functions

Get Foreground Color

Determines a suitable foreground color (black or white) for optimal contrast against the specified background color.

The function calculates the relative luminance of the background color using the formula: 0.299 * R + 0.587 * G + 0.114 * B . This ensures the foreground text remains readable regardless of background brightness.

VB
    Public Function GetForegroundColor(backgroundColor As Color) As Color

        'Calculate the perceived luminance (using the relative luminance formula)
        Dim luminance As Double = (0.299 * backgroundColor.R + 0.587 * backgroundColor.G + 0.114 * backgroundColor.B) / 255

        'If the brightness is greater than 0.5, use black; otherwise, use white.
        If luminance > 0.5 Then
            Return Color.Black
        Else
            Return Color.White
        End If
    End Function

Posted by ipercube


© 2026 Ipercube Design