You can achieve this by importing the colors as a color ramp. First categorize your layer the way you want. Then:
- Save an new (empty or just create some random values. This step is just to get an empty, working .gpl-file) color ramp from the QGIS Layer styling (.gpl-file)
- In a spreadsheet, order your legend entries with their r,g,b-values in the same order as your categories in QGIS (i.e. by increasing stratigraphic order). Copy the color values to clipboard.
- In a text editor, paste the r g b-values (space between r g b) into the empty color ramp-file.
- Open this new color ramp in the QGIS Layer styling for that layer
- You have correct colors in the legend. Also negates the need for data defined colors. You can save the .qml style together with your data for easy re-use.
This does not work if several units have the same color.