spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Color Gradient Algorithm in Lab color space


To generate a gradient between two color values, you can just linearly interpolate between them. This will basically work in any color space, although if the transformation between two color spaces is not linear, the gradient obtained by linearly interpolating in one space will not be linear in the other. Thus, generating gradients in Lab color space is exactly like generating them in, say, RGB space.

(For color spaces like HSV or HSL which have a hue coordinate that "loops around", some extra care may be needed to choose the right direction to interpolate in; fortunately, you're not asking about those color spaces here, so I don't need to go into such details.)


Just as a demonstration, here's how you'd generate an n-sample gradient between the colors c1 and c2 (each given as a LabColor object with properties L, a and b):

public static LabColor[]
makeGradient(LabColor c1, LabColor c2, int n) {
    LabColor gradient = new LabColor[n];
    for (int i = 0; i < n; i++) {
        float alpha = (float)i / (n-1);  // 0.0
<= alpha <= 1.0 
        float L = (1-alpha) * c1.L + alpha * c2.L;
        float a = (1-alpha) * c1.a + alpha * c2.a;
        float b = (1-alpha) * c1.b + alpha * c2.b;
        gradient[i] = new LabColor(L, a, b);
    }
    return gradient;
}

This will return a gradient with n color samples, where the first color is equal to c1, the last color is equal to c2, and the rest are interpolated between them.


However, based on the remark at the end of your question, I suspect you don't actually need to generate any gradients. Rather, to find the color in your chart that most closely matches the one in your sample, you just need to calculate the perceptual distance of each chart color from the sample (which, in the Lab color space, can be well approximated simply by their Euclidian distance) and select the closest one:

public static LabColor
findNearest(LabColor sample, LabColor[] chart) {
    LabColor nearest = null;
    float minDistanceSquared =
Float.POSITIVE_INFINITY;
    for (int i = 0; i < chart.length; i++) {
        float dL = sample.L - chart[i].L;
        float da = sample.a - chart[i].a;
        float db = sample.b - chart[i].b;
        float distanceSquared = dL*dL + da*da +
db*db;
        if (distanceSquared <
minDistanceSquared) {
            nearest = chart[i];
            minDistanceSquared = distanceSquared;
        }
    }
    return nearest;
}

Categories : Java

Related to : Color Gradient Algorithm in Lab color space
Primefaces Button color uniform not gradient
Set css-attribute "background-color" to whatever you want... override the .ui-button-text definition from primefaces. You can do that by implementing you own style-libraray, or your own css-file and reference this in your xhtml.

Categories : CSS
How can I use CSS to create a horizontal gradient with multiple color stops?
Try this background: linear-gradient(to left, red, green, blue); only miss out the to on browser specifics h1{background: linear-gradient(to left, red, green, blue); } <h1>Hello</h1>

Categories : HTML
IOS- How to change text color and selected date border color in UIdatepicker?
you may consider to look at the following links for custom date picker using NSCalendar class https://github.com/mwermuth/MWDatePicker http://blog.deeplink.me/post/81386967477/how-to-easily-customize-uidatepicker-for-ios

Categories : Misc
Raphael js-onclick action that changes color?remain that color until returning to click in other
let me just give you a brief example on how you could do it: http://jsfiddle.net/x0pv7580/ I create all needed elements... var rect1 = paper.rect(20,30,100,12).attr({fill: "orange"}); ... and push them into an array: var elements = [rect1, rect2, rect3]; .... After that, every elements gets the mouse actions: for(var i = 0; i< elements.length; i++) { appendActionToElement(elements[

Categories : Javascript
How to change Android Notification bar, but color system bar at the bottom a different color
I figured it out, this may not color the nav bar, but it gives me the desired result: tintManager.setNavigationBarTintEnabled(false); This line set the nav bar to clear, which was my desired result. I also found: tintManager.setNavigationBarTintColor(Color.BLACK); which can set the nav bar to any color. I am closing this question with this answer.

Categories : Java
Recently Add
How to convert this for loop into an enhanced loop
Do-while, try-catch loop error
JavaFx use String with Double on table column
Equal Spacing In print statements
Android Poor Image Quality When Saving Image From takePicture callback
Why ConcurrentHashMap put() return null but the item is put successfully?
Executing the java program from command prompt
Query multiple Entity attributes with List of values
Android Collections.sort - doesn't work properly
Gdx.files.internal(...) wrapper not working correctly
How to do character math?
Android - How to stack the views added dynamically?
Java: implement a loop with duplicate values in Array
Graphics paint component and loop trouble
Java: Transfer a file from server to client and from client to server
Problems with struts2 interceptor
Beginners Java Blue J returning a String in a method
How to pass Array of complex objects as a IN parameter in a stored procedure using JPA 2
Android: Finding fundamental frequency of audio input
Generic Java Logger output null.null for class and member name
How to use semaphores correctly
Hystrix: HystrixBadRequestException for failed validations
Sending a file over java socket
How can I make a variable work in more that one class? (Java)
Java - What are the layout managers available in awt.* and swing.* packages?
Reading ints from file and storing value to an array
random integers between 2 values
Maven -- is there a command to download the parent poms of all dependency jars?
Null Pointer Exception in Array when adding ActionListener
Hystrix Request Caching by Example
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.