Need help with Tooltip display?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Roger Breton
el 29 de Feb. de 2024
Comentada: Roger Breton
el 1 de Mzo. de 2024
I have a script that plots CIE Lab values in 3D:
Works great. But as you can see, the tooltip displays the XYZ value.
Instead, I would like it to display the "name of the color".
I asked chatGPT and it suggested the following code:
dcm = datacursormode(gcf);
set(dcm, 'UpdateFcn', @customDataCursor);
function txt = customDataCursor(~, event_obj)
pos = event_obj.Position;
color_name = getColorNameFromRGB(pos, colors);
txt = {['Color: ' color_name]};
end
function color_name = getColorNameFromRGB(rgb, color_struct)
% Implement logic to find the closest color name based on RGB value
% (e.g., Euclidean distance or other methods).
% Return the corresponding color name.
% For simplicity, you can use a lookup table or hard-coded mapping.
% Example:
color_name = 'PamplemousseRose'; % Replace with actual logic
end
I'm kind of lost. Upon initialization of my script, I use the following statements:
T = readtable('SICO Accents de couleur (2023 09 25).xlsx');
colorNames = T(:, 1); % Extract the color names (column 1)
labValues = T(:, 2:4); % Extract the CIE Lab values (columns 2 to 4)
Can I use a command such as "find"?
0 comentarios
Respuesta aceptada
Voss
el 29 de Feb. de 2024
Here is a script you can run, along with an xlsx file containing random colors and names.
You can run it as-is, and you can also substitute your own xlsx file and see how it does with that.
% a scatter plot
figure
scatter3(200*rand(10,1)-100,200*rand(10,1)-100,100*rand(10,1),[],rand(10,1),'filled')
xlabel('b* (X)')
ylabel('a* (Y)')
zlabel('L* (Z)')
% color table
colors = readtable('colors.xlsx');
% data cursor with custom update function
dcm = datacursormode(gcf);
set(dcm, 'UpdateFcn', {@customDataCursor,colors});
% data cursor update function
function txt = customDataCursor(~, event_obj, colors)
pos = event_obj.Position;
color_name = getColorNameFromLAB(pos, colors);
txt = {['Color: ' color_name{1}]};
end
% helper function
function color_name = getColorNameFromLAB(pos, colors)
% pos is in (X,Y,Z) = (b,a,L) order and colors are in L,a,b order (I assume),
% so use colors{:,[4 3 2]}, i.e., b,a,L order to compare with pos
d2 = sum((pos-colors{:,[4 3 2]}).^2,2); % squared Euclidean distance from pos to colors
[~,idx] = min(d2); % index of minimum squared distance
color_name = colors{idx,1}; % color name at that index
end
4 comentarios
Voss
el 29 de Feb. de 2024
You're welcome! Looks good. The best code is code that: (1) works, and (2) makes sense to the programmer.
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!