Jump to content

[SOLVED] Solving for points within an sphere centered on a user defined point?


mrln68

Recommended Posts

I'm not quite sure how best to approach this beast.

 

I have a number of colors which are predefined (they are physical paints colors) and I want to be able to allow the user to enter an RGB color and find the paint colors which match most closely to that color - or alternately choose a paint color from one manufacturer and see similiar colors from other manufacturers.

 

To this end, I need to convert the RGB data to the Lab color space format (easy enough...simple math).  The Lab color space is a representation of a 3 dimensional grid where all the colors can be plotted.  If they choose an RGB color like #FEEA00, the color space location ends up being (91.6, -11.8, 90.46) - effectively XYZ in a 3D grid.  Now I want to return every color which is within 2 from that point (a sphere with a radius of 2).

 

I could run each color through a test to determine if it is within the area of interest...possibly using an additional function to automatically disqualify anything which is obviously out of range in order to prevent the full formula from being applied.  However with the current number of data points being around 450 and the final number to rise well above 2000 that seems like a lot of SQL calls which wouldn't yield much.

 

The other option which I had been toying with is to use a local program to predetermine which colors are comparable and store that in the db with the colors themselves.  However this would then limit users to only selecting existing colors, and it would require much larger changes to the db when a single datapoint is added/changed.

 

The colors are stored in a MySQL db, and the server currently has PHP 4.4.4...not sure if any new toys have been added in PHP 5 that would help this though...but I figured I would make that be known.

 

Are there any other options?  Anyone have a trick which might make the realtime calculations more effecient?

Link to comment
Share on other sites

If i understood correctly, you should just be able to appply pythagoras in 3 dimensions:

 

<?php
$sql = "SELECT SQRT(POW(`x`-$x),2+POW(`y`-$y),2+POW(`z`-$z),2) as distance WHERE distance <= 2";
?>

 

 

Thats's given an x,y and z co-ordinate thats in the database, and a corresponding variable.

Link to comment
Share on other sites

Figures...

 

I need to take a closer look at what I can do with the actual SQL querry as opposed to doing it on the PHP side.  Right now the XYZ (or L.a.b. as the case may be) data is derived from the RGB data after it is called from the database, but it is easy enough to precalculate those values and store them in the database.  I'll need to double check to make sure that everything will work though.

Link to comment
Share on other sites

SELECT * 
FROM `color_data` 
WHERE SQRT(POW(`color_L`-25,2+POW(`color_a`-10,2)+POW(`color_b`-7,2)) <= 4

 

I took a stab at getting something working, but for some reason I am not getting any results (actually an SQL error...though according to what I can determine everything is correct).  I tried that querry both through PHP and directly as an SQL call through phpMyAdmin.  Any idea what might be foul?

Link to comment
Share on other sites

Thanks, for the obvious...Initially I was going to go with 2...but when I was double checking the actual math versus the colors...4 seemed to be the better option - once I get it up and running though, I may end up having the color range be user selectable though.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.