TLDR: I am afraid that too many pieces of this puzzle are inaccessible in the FrontEnd to make custom toolbars the way they are done by WRI.
Edit: my concerns were confirmed in comments below this post.
Ok, to make it more than a pessimistic comment here is what I know:
How to write the code for the attached cell generally, without knowing what the parent cell/expression will be?
Surprisingly it is not a big problem, your toolbar would look like this:
attachToolbar[box_] := AttachCell[ box, DynamicModule[{bgInit}, ButtonBar[{ "Black" :> (CurrentValue[box, Background] = Black), "White" :> (CurrentValue[box, Background] = White), "Reset" :> (CurrentValue[box, Background] = bgInit) }], Initialization :> ( bgInit = CurrentValue[box, Background]; ) ], RemovalConditions -> {"OutsideMouseClick", "ParentChanged"} ]
You can even make those actions performed FE-side. With more complex operations you would need to NotebookRead -> modify in kernel -> NotebookWrite the content but still possible.
What's an unexpected issue here is that is though to obtain an appropriate BoxObject to work with. All we have is EvaluationBox/ParentBox.
You can ignore that issue and attach cell to the parent cell. Can be good enough but already worse what WRI is doing. But wait, you'd have to have a way to find a way to target the desired box in the cell. SelectionMove does not give much hope in descending within the cell box structure. Nor does NotebookFind in searching within box structure. You could try to attach BoxID and follow How to set focus of a dialog window? but it has several pitfalls. There's ridiculous solution for some cases, like Graphics, you'd have to inject Dynamic to each created Graphics expression, something like:
Graphics[{ Dynamic[Print@ParentBox@EvaluationBox[]; {}, DestroyAfterEvaluation -> True], Disk[]} ]
Click on the printed BoxObject. How and when to store/use this BoxObject is another story but I hope it already is crazy enough not to do this.
How to set the attached cell to appear whenever a particular expression (e.g. Graphics) appears in the frontend? I rather not specify Format definitions for Graphics objects.
I don't know the mechanism behind e.g. images toolbar and I am afraid we don't have access to that. You could attach CellEventActions to Output cells and monitor FrontEnd`UndocumentedGetSelectionPacket on mouse click but that cumbersome way is only part of a solution you'd have to implement.
How to make the attached cell only appear when mouse hovers over the expression and disappear when mouse leaves the cells? "MouseExited" :> NotebookDelete@c is no good, as it exits when the mouse leaves g but is still over c, which is unwanted.
I don't know how you could to this with 'native' toolbars but I think we've already established that we won't have those. Otherwise the idea is simple, both the parent and the attached cell should have EventHandlers attached, and you should keep track of where the mouse is with MouseEntered/Exited. If it isn't in any of them on exit then delete the attached cell.