473,718 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Colors Sorting Light to Dark or Sorting By hue

1 New Member
I have list of color hex code, now i want it to rearrange so that light color comes first, after that color which is darker than previous one comes and so on..
Feb 15 '21 #1
3 24497
644 Recognized Expert Contributor
I have list of color hex code, now i want it to rearrange so that light color comes first, after that color which is darker than previous one comes and so on..
Extract the individual RGB components and analyze.
Feb 15 '21 #2
2 New Member
and there are different types of sorting algorithms too, depending on the size and numbers of data and the wanted speed of sorting.

Here's a clip of how I separate A R G B from a color (from a pixel). bad coding guaranteed but it works for me :)

Expand|Select|Wrap|Line Numbers
  2.             for (int i = 0; i < MG.bmp.Width; i++)
  3.             {
  4. for (int j = 0; j < MG.bmp.Height; j++)
  5.                 {
  6.                     Color picColor = MG.bmp.GetPixel(i, j);
  7.                     int alfa = picColor.A;
  8.                     int red = picColor.R;
  9.                     int green = picColor.G;
  10.                     int blue = picColor.B;
  11.                     int gray = (byte)(.2989 * red + .5870 * green + .1140 * blue);
  12.                     work1[i, j] = gray;
  13. }
  14. }

Good luck
Apr 14 '22 #3
1 New Member
To rearrange a list of color hex codes so that lighter colors come first, followed by progressively darker colors, you can compare the brightness or luminosity values of each color. One way to achieve this is by converting the hex codes to the HSL (Hue, Saturation, Lightness) color model and sorting based on the lightness value.

Here's an example in Python to illustrate the process:

import colorsys

def get_brightness( hex_code):
r, g, b = tuple(int(hex_c ode[i:i+2], 16) for i in (0, 2, 4))
hls = colorsys.rgb_to _hls(r / 255, g / 255, b / 255)
return hls[1] # Lightness value

def rearrange_color s(colors):
return sorted(colors, key=get_brightn ess)

# Example usage
color_list = ["#FFFFFF", "#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#FF00FF", "#00FFFF", "#000000"]
rearranged_colo rs = rearrange_color s(color_list)
print(rearrange d_colors)

In this example, the `get_brightness ` function takes a hex code, converts it to the RGB color space, and then converts it to the HSL color space using the `colorsys` module. It returns the lightness value (ranging from 0 to 1) of the color.

The `rearrange_colo rs` function takes a list of color hex codes and uses the `sorted` function with a `key` parameter, which specifies the function to extract the lightness value from each color. The `sorted` function will then sort the colors based on Drift Boss this extracted value.

The resulting `rearranged_col ors` list will contain the colors sorted from light to dark based on their perceived brightness.

Note that this example assumes you are using Python, but the concept can be adapted to other programming languages as well. Additionally, keep in mind that color perception can be subjective, so depending on your specific use case, you may need to consider other factors such as saturation or adjust the sorting criteria accordingly.
Sep 14 '23 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys() sorted_feature_vector.sort() #feature_vector.keys()=sorted_feature_vector
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) CType(local4, Short) = CType(src, Short)
by: Gareth Gale | last post by:
I'm trying to implement a way of allowing a user to sort a HTML table via Javascript on the client. I've seen lots of samples where single column sorting (asc or desc) is shown, but I'd like nested sorting i.e. sort by col1 asc, then by col2 desc etc. Can anyone help ?
by: jrhoads23 | last post by:
If you look at a standard Button in a .NET Windows Forms app, you will notice its default BackColor is "Control" and it has a 3D raised border which is 2 pixels wide. The outer edge on the left and top is "ControlLightLight" color and the inner edge on the left and top is "ControlLight". The outer edge on the right and bottom is "ControlDarkDark" and the inner edge on the right and bottom is "ControlDark". All of these colors are defined...
by: JoelW | last post by:
Let's say you're programming in VB.NET, and you have: Dim str1 As String Dim str2 As String Dim str3 As String str1 = "I am red" str2 = "I am yellow" str3 = "I am dark green"
by: Chris Hills | last post by:
A lesson in Posting How many C.L.C group posters does it take to change a C light bulb? 1 to change the light bulb and to post that the light bulb has been changed 14 to share similar experiences of changing light bulbs and how the light bulb could have been changed differently
by: Umesh | last post by:
I don't understand it. Help me by writing a program while i try myself to sort an array of numbers. Thanks.
by: shijith | last post by:
Hi, I am using MS-SQL 2000. I had a varchar column with data formated as Numeric portion + Alpha portion. Id ---- 1 2AA 2DF 2AB
by: Plater | last post by:
I am having trouble determining when my DataGridView object is sorting (based on a column header click). The idea is, in a large table, sorting the columns takes time, so I show a splash screen. When it is done sorting I want the splash screen dissapear. What I had been doing was using the CellClick and Sorted events: private void myDGV_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex == -1) {//sorting
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.